【问题标题】:Uncompress BZIP2 archive解压缩 BZIP2 存档
【发布时间】:2011-01-20 08:17:02
【问题描述】:

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

我非常理想地使用 Java 和 Maven,我想将它作为 POM 中的依赖项。

你推荐什么库?

【问题讨论】:

    标签: java api compression bzip2


    【解决方案1】:

    我能看到的最佳选择是 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();
    

    【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-21
      相关资源
      最近更新 更多