简体   繁体   中英

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

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

Following the examples in http://commons.apache.org/proper/commons-compress/examples.html you have to wrap one InputStream with another

// 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM