So I have written the parser for parsing an individual file but can i read each file within the archive without having to actually extract the archive to disk
Asked
Active
Viewed 1,441 times
-1
-
Possible duplicate of http://stackoverflow.com/questions/2322944/uncompress-bzip2-archive or http://stackoverflow.com/questions/315618/how-do-i-extract-a-tar-file-in-java – David Conrad Jan 15 '15 at 18:15
-
Besides Apache Commons Compress, here are two implementations of bzip2 in Java: https://code.google.com/p/jbzip2/ and http://www.kohsuke.org/bzip2/ – David Conrad Jan 15 '15 at 18:17
-
But how do I deal with bz2 AND tar, these answers dont seem to address that ? – Paul Taylor Jan 15 '15 at 19:09
-
@PaulTaylor What kind of answer are you looking for here? There are libraries for bzip decoding and for tarfile parsing. You say you have your own parser, but we don't know exactly what it does or what it uses as an input. – Kenster Jan 15 '15 at 19:26
-
@Kenster My parser can take an input stream so Im looking for an answer that can pass an inputstream to my parser for each file within the tar archive within the compressed bzip – Paul Taylor Jan 15 '15 at 20:08
-
@PaulTaylor The Apache Commons Compress library specifically handles both bz2 AND tar. Did you bother clicking through to the page for the library from the first answer I linked in my first comment? Here's a quote: "The Apache Commons Compress library defines an API for working with ar, cpio, Unix dump, tar, zip, gzip, XZ, Pack200, bzip2, 7z, arj, lzma, snappy, DEFLATE and Z files." – David Conrad Jan 15 '15 at 20:43
-
Yes, but I couldnt work out work out how to do it,I thought someone could give me a code example for this clearly defined problem – Paul Taylor Jan 16 '15 at 07:43
1 Answers
4
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();

dtortola
- 768
- 4
- 6