我可以解压缩 zip、gzip 和 rar 文件,但我还需要解压缩 bzip2 文件以及解压缩它们 (.tar)。我还没有遇到一个好的图书馆来使用。
我非常理想地使用 Java 和 Maven,我想将它作为依赖项包含在 POM 中。
你推荐什么图书馆?
我可以解压缩 zip、gzip 和 rar 文件,但我还需要解压缩 bzip2 文件以及解压缩它们 (.tar)。我还没有遇到一个好的图书馆来使用。
我非常理想地使用 Java 和 Maven,我想将它作为依赖项包含在 POM 中。
你推荐什么图书馆?
我能看到的最好的选择是带有这个 Maven 依赖项的Apache Commons Compress 。
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.0</version>
</dependency>
从例子:
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")
}