【发布时间】:2011-01-20 08:17:02
【问题描述】:
我可以解压缩 zip、gzip 和 rar 文件,但我还需要解压缩 bzip2 文件以及解压缩它们 (.tar)。我还没有找到一个好的库来使用。
我非常理想地使用 Java 和 Maven,我想将它作为 POM 中的依赖项。
你推荐什么库?
【问题讨论】:
标签: java api compression bzip2
我可以解压缩 zip、gzip 和 rar 文件,但我还需要解压缩 bzip2 文件以及解压缩它们 (.tar)。我还没有找到一个好的库来使用。
我非常理想地使用 Java 和 Maven,我想将它作为 POM 中的依赖项。
你推荐什么库?
【问题讨论】:
标签: java api compression bzip2
我能看到的最佳选择是 Apache Commons Compress 与此 Maven 依赖项。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.0</version>
</dependency>
来自examples:
FileInputStream in = new FileInputStream("archive.tar.bz2"); FileOutputStream out = new FileOutputStream("archive.tar"); BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in); final byte[] buffer = new byte[buffersize]; int n = 0; while (-1 != (n = bzIn.read(buffer))) { out.write(buffer, 0, n); } out.close(); bzIn.close();
【讨论】:
请不要忘记使用 缓冲 流来获得高达 3 倍的加速!
public void decompressBz2(String inputFile, String outputFile) throws IOException {
var input = new BZip2CompressorInputStream(new BufferedInputStream(new FileInputStream(inputFile)));
var output = new FileOutputStream(outputFile);
try (input; output) {
IOUtils.copy(input, output);
}
}
decompressBz2("example.bz2", "example.txt");
build.gradle.kts:
dependencies {
...
implementation("org.apache.commons:commons-compress:1.20")
}
【讨论】: