【问题标题】:How to decompress BZIP (not BZIP2) with Apache Commons如何使用 Apache Commons 解压缩 BZIP(不是 BZIP2)
【发布时间】:2019-10-19 19:15:29
【问题描述】:

我一直致力于解压缩不同类型的文件格式,例如 "zip,tar,tbz,tgz"。除了 tbz 之外,我可以做所有事情,因为 apache common compress 库提供 BZIP2 压缩器。但我需要解压缩旧的 BZIP 而不是 BZIP2。有没有办法做到这一点。我已经添加了到目前为止我已经完成的代码,用于使用下面的 apache commons 库提取不同的 tar 文件存档。

public List<ArchiveFile> processTarFiles(String compressedFilePath, String fileType) throws IOException {
    List<ArchiveFile> extractedFileList = null;
    TarArchiveInputStream is = null;
    FileOutputStream fos = null;
    BufferedOutputStream dest = null;
    try {
        if(fileType.equalsIgnoreCase("tar"))
        {
            is = new TarArchiveInputStream(new FileInputStream(new File(compressedFilePath)));
        }
        else if(fileType.equalsIgnoreCase("tbz")||fileType.equalsIgnoreCase("bz"))
        {
            is = new TarArchiveInputStream(new BZip2CompressorInputStream(new FileInputStream(new File(compressedFilePath))));
        }
        else if(fileType.equalsIgnoreCase("tgz")||fileType.equalsIgnoreCase("gz"))
        {
            is = new TarArchiveInputStream(new GzipCompressorInputStream(new FileInputStream(new File(compressedFilePath))));
        }
        TarArchiveEntry entry = is.getNextTarEntry();
        extractedFileList = new ArrayList<>();
        while (entry != null) {
            // grab a zip file entry
            String currentEntry = entry.getName();

            if (!entry.isDirectory()) {
                File destFile = new File(Constants.DEFAULT_ZIPOUTPUTPATH, currentEntry);
                File destinationParent = destFile.getParentFile();
                // create the parent directory structure if needed
                destinationParent.mkdirs();
                ArchiveFile archiveFile = new ArchiveFile();
                int currentByte;
                // establish buffer for writing file
                byte data[] = new byte[(int) entry.getSize()];
                // write the current file to disk
                fos = new FileOutputStream(destFile);
                dest = new BufferedOutputStream(fos, (int) entry.getSize());

                // read and write until last byte is encountered
                while ((currentByte = is.read(data, 0, (int) entry.getSize())) != -1) {
                    dest.write(data, 0, currentByte);
                }
                dest.flush();
                dest.close();
                archiveFile.setExtractedFilePath(destFile.getAbsolutePath());
                archiveFile.setFormat(destFile.getName().split("\\.")[1]);
                extractedFileList.add(archiveFile);
                entry = is.getNextTarEntry();
            } else {
                new File(Constants.DEFAULT_ZIPOUTPUTPATH, currentEntry).mkdirs();
                entry = is.getNextTarEntry();
            }

        }
    } catch (IOException e) {
        System.out.println(("ERROR: " + e.getMessage()));
    } catch (Exception e) {
        System.out.println(("ERROR: " + e.getMessage()));
    } finally {
        is.close();
        dest.flush();
        dest.close();
    }

    return extractedFileList;
}

【问题讨论】:

  • 要求我们推荐或查找书籍、工具、软件库、教程或其他场外资源的问题与 Stack Overflow 无关(请参阅 help center)。
  • 其实这不是题外话,因为我还需要一个示例代码和要使用的库
  • 您正在寻求图书馆推荐 - 这是题外话。我看不出您的理由不是“我要求使用图书馆”。要求提供示例代码并不能成为话题。
  • 好的,给我推荐一个平台,我要问一下
  • 我还编辑了我的问题,不是要求图书馆,而是要求 java 中的解决方案

标签: java compression apache-commons apache-commons-compress bzip


【解决方案1】:

最初的 Bzip 据说使用了专利算法,因此 Bzip2 诞生时使用了未获得专利的算法和技术。

这可能是它不再被广泛使用并且开源库忽略它的原因。

here (gist.github.com mirror) 显示了一些用于解压缩 Bzip 文件的 C 代码。

您可能想用 Java 阅读和重写它。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2011-06-14
  • 2017-05-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-04
  • 2011-01-20
  • 2015-06-08
相关资源
最近更新 更多