Extract tar.gz file
874785Jul 11 2011 — edited Jul 12 2011Hi,
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.