我查看了JDK和Apache压缩库附带的默认Zip库,我对它们不满意,原因有3个:

They are bloated and have bad API design. I have to write 50 lines of boiler plate byte array output, zip input, file out streams and close relevant streams and catch exceptions and move byte buffers on my own? Why can't I have a simple API that looks like this Zipper.unzip(InputStream zipFile, File targetDirectory, String password = null) and Zipper.zip(File targetDirectory, String password = null) that just works? It seems zipping unzipping destroys file meta-data and password handling is broken. Also, all the libraries I tried were 2-3x slow compared to the command line zip tools I get with UNIX?

对我来说(2)和(3)是次要的点,但我真的想要一个良好的测试库与一行接口。


当前回答

你看了http://commons.apache.org/vfs/吗?它声称可以为你简化很多事情。但我从未在项目中使用过。

除了JDK或Apache压缩之外,我也不知道Java-Native压缩库。

我记得有一次我们从Apache Ant中剥离了一些特性——它们有很多内置的压缩/解压utils。

VFS的示例代码如下所示:

File zipFile = ...;
File outputDir = ...;
FileSystemManager fsm = VFS.getManager();
URI zip = zipFile.toURI();
FileObject packFileObject = fsm.resolveFile(packLocation.toString());
FileObject to = fsm.toFileObject(destDir);
FileObject zipFS;
try {
    zipFS = fsm.createFileSystem(packFileObject);
    fsm.toFileObject(outputDir).copyFrom(zipFS, new AllFileSelector());
} finally {
    zipFS.close();
}

其他回答

另一个选项是zt-zip,可从Maven中心和项目页面https://github.com/zeroturnaround/zt-zip获得

它具有标准的打包和解包功能(在流和文件系统上)+许多辅助方法来测试存档中的文件或添加/删除条目。

TrueZip是一个非常好的项目。

TrueZIP是一个基于Java的虚拟文件系统(VFS)插件框架,它提供对归档文件的透明访问,就像它们只是普通的目录一样

例如(来自网站):

File file = new TFile("archive.tar.gz/README.TXT");
OutputStream out = new TFileOutputStream(file);
try {
   // Write archive entry contents here.
   ...
} finally {
   out.close();
}

另一个选择是JZlib。根据我的经验,它不像zip4J那样“以文件为中心”,所以如果您需要处理内存中的blobs而不是文件,那么您可能需要考虑一下它。

解压zip文件及其所有子文件夹,只使用JDK:

private void extractFolder(String zipFile,String extractFolder) 
{
    try
    {
        int BUFFER = 2048;
        File file = new File(zipFile);

        ZipFile zip = new ZipFile(file);
        String newPath = extractFolder;

        new File(newPath).mkdir();
        Enumeration zipFileEntries = zip.entries();

        // Process each entry
        while (zipFileEntries.hasMoreElements())
        {
            // grab a zip file entry
            ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
            String currentEntry = entry.getName();

            File destFile = new File(newPath, currentEntry);
            //destFile = new File(newPath, destFile.getName());
            File destinationParent = destFile.getParentFile();

            // create the parent directory structure if needed
            destinationParent.mkdirs();

            if (!entry.isDirectory())
            {
                BufferedInputStream is = new BufferedInputStream(zip
                .getInputStream(entry));
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[BUFFER];

                // write the current file to disk
                FileOutputStream fos = new FileOutputStream(destFile);
                BufferedOutputStream dest = new BufferedOutputStream(fos,
                BUFFER);

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                is.close();
            }


        }
    }
    catch (Exception e) 
    {
        Log("ERROR: "+e.getMessage());
    }

}

Zip文件及其所有子文件夹:

 private void addFolderToZip(File folder, ZipOutputStream zip, String baseName) throws IOException {
    File[] files = folder.listFiles();
    for (File file : files) {
        if (file.isDirectory()) {
            addFolderToZip(file, zip, baseName);
        } else {
            String name = file.getAbsolutePath().substring(baseName.length());
            ZipEntry zipEntry = new ZipEntry(name);
            zip.putNextEntry(zipEntry);
            IOUtils.copy(new FileInputStream(file), zip);
            zip.closeEntry();
        }
    }
}

你看了http://commons.apache.org/vfs/吗?它声称可以为你简化很多事情。但我从未在项目中使用过。

除了JDK或Apache压缩之外,我也不知道Java-Native压缩库。

我记得有一次我们从Apache Ant中剥离了一些特性——它们有很多内置的压缩/解压utils。

VFS的示例代码如下所示:

File zipFile = ...;
File outputDir = ...;
FileSystemManager fsm = VFS.getManager();
URI zip = zipFile.toURI();
FileObject packFileObject = fsm.resolveFile(packLocation.toString());
FileObject to = fsm.toFileObject(destDir);
FileObject zipFS;
try {
    zipFS = fsm.createFileSystem(packFileObject);
    fsm.toFileObject(outputDir).copyFrom(zipFS, new AllFileSelector());
} finally {
    zipFS.close();
}