Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Extract tar.gz file

874785Jul 11 2011 — edited Jul 12 2011
Hi,

I have a file with extension tar.gz and i need to extract it. I tried with GZIPInputStream class of java.util.zip package but could not do it. Below is the code snippet for the same. Please suggest the way we can handle this problem in Java.

String filename = "C:\\TarFile\\Test.20110519_123033_00001.tar.gz";

final int BUFFER_SIZE = 2048;
try {
BufferedOutputStream dest = null;
FileInputStream fis = new FileInputStream(filename);
GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(fis));
int len;
FileOutputStream fos = new FileOutputStream("C:\\TarFile\\Test.20110519_123033_00001.tar");
dest = new BufferedOutputStream(fos, BUFFER_SIZE);
byte data[] = new byte[BUFFER_SIZE];
while ((len = zis.read()) != -1) {
//int count;

// write the files to the disk


dest.write(data, 0, len );

}

//dest.flush();
dest.close();
zis.close();
}catch(Exception e) {
}

Thanks.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 9 2011
Added on Jul 11 2011
6 comments
3,183 views