解压缩 BZIP2 存档

发布于 2024-08-22 21:58:03 字数 152 浏览 2 评论 0原文

我可以解压缩 zip、gzip 和 rar 文件,但我还需要解压缩 bzip2 文件以及解压缩它们 (.tar)。我还没有遇到一个好的库可以使用。

我将 Java 与 Maven 一起使用,非常理想,我想将其作为依赖项包含在 POM 中。

你推荐哪些图书馆?

I can uncompress zip, gzip, and rar files, but I also need to uncompress bzip2 files as well as unarchive them (.tar). I haven't come across a good library to use.

I am using Java along with Maven so ideally, I'd like to include it as a dependency in the POM.

What libraries do you recommend?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱娆 2024-08-29 21:58:03

我能看到的最好的选择是 Apache Commons Compress 与此 Maven 依赖项。

<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);
最终字节[]缓冲区=新字节[缓冲区大小];
整数 n = 0;
while (-1 != (n = bzIn.read(buffer))) {
  out.write(缓冲区, 0, n);
}
关闭();
bzIn.close();

The best option I can see is Apache Commons Compress with this Maven dependency.

<dependency>
  <groupId>org.apache.commons</groupId>
  <artifactId>commons-compress</artifactId>
  <version>1.0</version>
</dependency>

From the 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();
羁绊已千年 2024-08-29 21:58:03

请不要忘记使用缓冲流来获得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")
}

Please don't forget to use buffered streams to gain up to 3x speedup!

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");

With build.gradle.kts:

dependencies {
    ...
    implementation("org.apache.commons:commons-compress:1.20")
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文