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

时间:2015-01-15 17:59:11

标签: java tar

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

1 个答案:

答案 0 :(得分:3)

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