集册 Java实例教程 解压拉链

解压拉链

欢马劈雪     最近更新时间:2020-01-02 10:19:05

576
取出ZIP


//package com.nowjava;

import java.io.BufferedInputStream;

import java.io.BufferedOutputStream;
/** 
来 自 
n o w    j a v a  . c o m
**/

import java.io.File;


import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Enumeration;

import java.util.zip.ZipEntry;


import java.util.zip.ZipFile;


public class Main {

    private static final int BUFFER = 2048;


    public static void unpackZip(String zipFile, String location) {

        try {

            File fSourceZip = new File(zipFile);

            // File temp = new File(zipPath);
            /** 
            来 自 
            时 代 J a v a 公 众 号
            **/

            // temp.mkdir();


            // Extract entries while creating required sub-directories

            ZipFile zf = new ZipFile(fSourceZip);

            Enumeration<?> e = zf.entries();


            while (e.hasMoreElements()) {

                ZipEntry entry = (ZipEntry) e.nextElement();

                File destinationFilePath = new File(location,

                        entry.getName());


                // create directories if required.

                destinationFilePath.getParentFile().mkdirs();


                // if the entry is directory, leave it. Otherwise extract it.

                if (entry.isDirectory()) {

                    continue;

                } else {

                    // Get the InputStream for current entry of the zip file

                    // using InputStream getInputStream(Entry entry) method.

                    BufferedInputStream bis = new BufferedInputStream(

                            zf.getInputStream(entry));


                    int b;

                    byte buffer[] = new byte[BUFFER];


                    // read the current entry from the zip file, extract it and

                    // write the extracted file.

                    FileOutputStream fos = new FileOutputStream(

                            destinationFilePath);

                    
展开阅读全文