首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Apache解压缩BZIP (而不是BZIP2)

如何使用Apache解压缩BZIP (而不是BZIP2)
EN

Stack Overflow用户
提问于 2019-06-04 09:09:12
回答 1查看 516关注 0票数 0

我一直在努力从不同类型的文件格式(如"zip、tar、tbz、tgz" )中解压缩。除了tbz之外,我可以做所有的事情,因为apache公共压缩库提供了BZIP2压缩器。但是我需要解压一个旧的BZIP而不是BZIP2。有什么方法可以做到吗,java。我已经添加了到目前为止使用提取不同tar文件存档的代码。

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-04 09:38:10

最初的Bzip应该是使用专利算法,所以Bzip2诞生时使用的算法和技术没有专利。

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

有一些C代码用于解压缩这里 (gist.github.com镜)所示的Bzip文件。

您可能需要用Java来读取和重写它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56440808

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档