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

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

问题描述

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


  1. 他们are肿,有糟糕的API设计。我必须写入50行锅炉板字节数组输出,zip输入,文件输出流和关闭相关流,捕获异常和移动字节缓冲区我自己?为什么我不能有一个简单的API,看起来像这样 Zipper.unzip(InputStream zipFile,File targetDirectory,String password = null) Zipper。


  2. 它似乎正在解压缩破坏文件元数据和密码

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


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

解决方案

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

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

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


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

  2. It seems zipping unzipping destroys file meta-data and password handling is broken.

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

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

解决方案

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();
    }
}

The Maven dependency is:

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

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

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