【问题标题】:How do parse some files in a tar.bz2 archive with Java如何使用 Java 解析 tar.bz2 存档中的一些文件
【发布时间】:2015-01-15 17:59:10
【问题描述】:

所以我已经编写了用于解析单个文件的解析器,但是我可以读取存档中的每个文件,而不必实际将存档提取到磁盘

【问题讨论】:

  • 除了 Apache Commons Compress,这里有两个 bzip2 在 Java 中的实现:code.google.com/p/jbzip2kohsuke.org/bzip2
  • 但是我该如何处理 bz2 和 tar,这些答案似乎没有解决这个问题?
  • @PaulTaylor 您在这里寻找什么样的答案?有用于 bzip 解码和 tarfile 解析的库。你说你有自己的解析器,但我们并不确切知道它做了什么或它用作输入。
  • @Kenster 我的解析器可以接受输入流,所以我正在寻找一个答案,可以将输入流传递给我的解析器,用于压缩 bzip 中 tar 存档中的每个文件

标签: java tar


【解决方案1】:

按照http://commons.apache.org/proper/commons-compress/examples.html 中的示例,您必须将一个 InputStream 与另一个包装起来

// 1st InputStream from your compressed file
FileInputStream in = new FileInputStream(tarbz2File);
// wrap in a 2nd InputStream that deals with compression
BZip2CompressorInputStream bzIn = new BZip2CompressorInputStream(in);
// wrap in a 3rd InputStream that deals with tar
TarArchiveInputStream tarIn = new TarArchiveInputStream(bzIn);
ArchiveEntry entry = null;

while (null != (entry = tarIn.getNextEntry())){
    if (entry.getSize() < 1){
        continue;
    }
    // use your parser here, the tar inputStream deals with the size of the current entry
    parser.parse(tarIn);
}
tarIn.close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-24
    • 2014-01-11
    • 2020-08-31
    • 1970-01-01
    • 2017-07-27
    • 2014-03-21
    相关资源
    最近更新 更多