flexinIT
flexinIT

Reputation: 431

Accessing Java files within .tar files using Java

I am creating a Java application that will take in .tar files and retrieve all the files from these .tar files. I know you can use GZIPInputStream to get the files from the .tar files, but is it possible to get these files from the GZIPInputStream as a FileInputStream, something like below?

InputStream is = new GZIPInputStream(new FileInputStream(file)); 

Upvotes: 2

Views: 332

Answers (2)

Yair Zaslavsky
Yair Zaslavsky

Reputation: 4137

Yes, it is possible due to the CTOR of GZipInputStream that lets you accept an InputStream as parameter (GZipInputStream is a wrapper in that sense to InputStream). Then all you need to do is follow examples you can find on the internet such as this in order to extract the files.

Upvotes: 1

Adam Sznajder
Adam Sznajder

Reputation: 9206

GZIPInputStream extends InputStream (not directly) so in my opinion it's ok. Try this:

InputStream in = new java.util.GZIPInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
int read = 0;
while (( read = in.read(buffer, 0, 1024)) > 0)
{
     // do sth
}

Upvotes: 0

Related Questions