如何使用 Java 解析 tar.bz2 存档中的某些文件

How do parse some files in a tar.bz2 archive with Java

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

按照 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();