Reputation: 67
I am trying to uncompress the tar files using commons-compress.jar for the first time. Here is my initial code which is throwing the error.
TarArchiveInputStream myTarFile=new TarArchiveInputStream(
(new GZIPInputStream
(new FileInputStream("C:/Users/abc/xyz_20151010.tar")));
System.out.println(myTarFile.getCurrentEntry());
The tar file has set of files with extension .dat.gz.bak
I need to read and process data from .dat file .
Upvotes: 0
Views: 1729
Reputation: 20885
If your input file were a .tar.gz
you should have wrapped the file in a TarInputStream
, and the tar in a GZip
. But from the filename it seems you have a plain tar archive.
So, if I understand your input format, you need something like this:
public class MyDataReader {
private final TarArchiveInputStream tar;
public boolean hasNextData() {
return tar.getNextTarEntry() != null;
}
public MyData nextData() {
byte[] buff = new byte[tar.getCurrentEntry().getSize()];
// loop over tar until all entry has been read
InputStream entry = new ByteArrayInputStream(buff);
GZIPInputStream gzip = new GZipInputStream(entry);
// process gzip input stream
}
}
Upvotes: 1
Reputation: 18825
You're reading the tar file as gzip compressed while it's plain tar and only the items inside are compressed.
So avoid this GZIPInputStream and instead go item by item, read it using read() and then process with GZIPInputstream(ByteArrayInputStream(content)). You may consider creating input stream which reads the content on the fly.
Upvotes: 1