首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >什么是一个很好的Java库来压缩/解压缩文件?

什么是一个很好的Java库来压缩/解压缩文件?
EN

Stack Overflow用户
提问于 2012-02-17 08:17:29
回答 9查看 248.6K关注 0票数 274

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

  1. 它们臃肿,API设计不好。我必须要编写50行锅炉板字节数组输出、压缩输入、文件输出、关闭相关流、捕获异常和移动字节缓冲区。吗?为什么我不能拥有一个简单的API,它看起来像这个Zipper.unzip(InputStream zipFile, File targetDirectory, String password = null)Zipper.zip(File targetDirectory, String password = null)
  2. 它似乎拉链,解压缩,破坏文件元-数据和密码处理被打破。
  3. 而且,与我在UNIX中获得的命令行zip工具相比,我尝试过的所有库都慢了2-3倍。

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

EN

回答 9

Stack Overflow用户

回答已采纳

发布于 2013-02-01 23:01:41

我知道现在很晚了,有很多答案,但是这个zip4j是我使用过的最好的压缩库之一。它简单(没有锅炉代码),可以轻松处理密码保护的文件。

代码语言:javascript
复制
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.core.ZipFile;


public static void unzip(){
    String source = "some/compressed/file.zip";
    String destination = "some/destination/folder";
    String password = "password";

    try {
         ZipFile zipFile = new ZipFile(source);
         if (zipFile.isEncrypted()) {
            zipFile.setPassword(password);
         }
         zipFile.extractAll(destination);
    } catch (ZipException e) {
        e.printStackTrace();
    }
}

Maven依赖项是:

代码语言:javascript
复制
<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>1.3.2</version>
</dependency>
票数 335
EN

Stack Overflow用户

发布于 2012-12-17 10:28:47

在Java8中,使用Apache Commons-IOIOUtils,您可以这样做:

代码语言:javascript
复制
try (java.util.zip.ZipFile zipFile = new ZipFile(file)) {
  Enumeration<? extends ZipEntry> entries = zipFile.entries();
  while (entries.hasMoreElements()) {
    ZipEntry entry = entries.nextElement();
    File entryDestination = new File(outputDir,  entry.getName());
    if (entry.isDirectory()) {
        entryDestination.mkdirs();
    } else {
        entryDestination.getParentFile().mkdirs();
        try (InputStream in = zipFile.getInputStream(entry);
             OutputStream out = new FileOutputStream(entryDestination)) {
            IOUtils.copy(in, out);
        }
    }
  }
}

它仍然是一些样板代码,但它只有一个非外来依赖:公-IO

在Java 11和更高版本中,可以找到更好的选项,请参见哲ZhekaKozlov的评论。

票数 95
EN

Stack Overflow用户

发布于 2012-02-17 08:25:55

仅使用JDK提取zip文件及其所有子文件夹:

代码语言:javascript
复制
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文件及其所有子文件夹:

代码语言:javascript
复制
 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();
        }
    }
}
票数 45
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9324933

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档