java unzip 方法

第一次方式是apache的zip包


            org.apache.commons
            commons-compress
            1.16
        
ZipFile zipFile = null;
        int BUFFER_SIZE=1024;
        try {
            zipFile=new ZipFile(zip);
            Enumeration entries = zipFile.getEntries();
            ZipArchiveEntry entry = null;
            while (entries.hasMoreElements()) {
                entry = entries.nextElement();
                if (entry.isDirectory()) {
                    File directory = new File(zipPath, entry.getName());
                    directory.mkdirs();
                } else {
                    File file = new File(zipPath, entry.getName());
                    if (!file.getParentFile().exists()) {
                        file.getParentFile().mkdirs();
                    }
                    InputStream is = zipFile.getInputStream(entry);
                    OutputStream os = new BufferedOutputStream(new FileOutputStream(file), BUFFER_SIZE);
                    IOUtils.copy(is, os);
                    IOUtils.closeQuietly(os);
                    IOUtils.closeQuietly(is);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                zipFile.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

第二种是用zip4j


            net.lingala.zip4j
            zip4j
            1.3.2
        
public static void main(String[] args) {
        String file1 = "d:\\worktemp\\hfhhByUnitPaper.zip";
        String target = "D:\\worktemp";
            String password = "password";

            try {
                 ZipFile zipFile = new ZipFile(file1);
                 //zipFile.se
                 if (zipFile.isEncrypted()) {
                    zipFile.setPassword(password);
                 }
                 zipFile.extractAll(target);
            } catch (ZipException e) {
                e.printStackTrace();
            }
    }

不建设用apache的ZipArchiveInputStream,会有很多问题..详细情况可以参考源码中的说明..

你可能感兴趣的:(zip,zip4j)