什么是压缩/解压缩文件的好 Java 库? [英] What is a good Java library to zip/unzip files?

查看:31
本文介绍了什么是压缩/解压缩文件的好 Java 库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I looked at the default Zip library that comes with the JDK and the Apache compression libs and I am unhappy with them for 3 reasons:

  1. 它们很臃肿,而且 API 设计很糟糕.我必须 写 50 行样板字节数组输出、zip 输入、文件输出流并关闭相关流并捕获异常并自行移动字节缓冲区?为什么我不能有一个像这样的简单 API Zipper.unzip(InputStream zipFile, File targetDirectory, String password = null)Zipper.zip(File targetDirectory, String password = null) 是否有效?

  1. 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.

此外,与我使用 UNIX 获得的命令行 zip 工具相比,我尝试的所有库都慢了 2-3 倍?

Also, all the libraries I tried were 2-3x slow compared to the command line zip tools I get with UNIX?

对我来说 (2) 和 (3) 是次要的,但我真的想要一个很好的测试库和一个单行界面.

For me (2) and (3) are minor points but I really want a good tested library with a one-line interface.

推荐答案

我知道已经晚了,有很多答案,但是这个 zip4j 是我用过的最好的压缩库之一.它简单(无锅炉代码)并且可以轻松处理受密码保护的文件.

I know its late and there are lots of answers but this zip4j is one of the best libraries for zipping I have used. Its simple (no boiler code) and can easily handle password protected files.

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 依赖项是:

The Maven dependency is:

<dependency>
    <groupId>net.lingala.zip4j</groupId>
    <artifactId>zip4j</artifactId>
    <version>1.3.2</version>
</dependency>

这篇关于什么是压缩/解压缩文件的好 Java 库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆