0

我一直致力于从不同类型的文件格式中解压缩,例如"zip,tar,tbz,tgz"。除了 tbz 之外,我什么都能做,因为apache 通用压缩库提供 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;
}
4

1 回答 1

2

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

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

此处显示了一些用于解压 Bzip 文件的 C 代码(gist.github.com 镜像)。

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

于 2019-06-04T09:38:10.227 回答