GZip File UnCompression
The following example shows how to unzip a file compressed using GZip compression.
To create uncompressed file we need to copy bytes
from GZIP input stream to output stream.
package com.bethecoder.tutorials.utils.zip;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
public class GZipFileUnCompression {
/**
* @param args
*/
public static void main ( String [] args ) {
String srcZipFilePath = "C:\\prop_file.txt.gzip" ;
String tgtFilePath = "C:\\prop_file.txt" ;
gzipUnCompressFile ( srcZipFilePath, tgtFilePath ) ;
}
public static void gzipUnCompressFile ( String srcZipFilePath, String tgtFilePath ) {
try {
//Open GZIP input stream
GZIPInputStream inStream = new GZIPInputStream ( new FileInputStream ( srcZipFilePath )) ;
//Open target stream
OutputStream outStream = new FileOutputStream ( tgtFilePath ) ;
//Copy bytes from source stream to target stream
byte [] buf = new byte [ 2048 ] ;
int len;
while (( len = inStream.read ( buf )) > 0 ) {
outStream.write ( buf, 0 , len ) ;
}
//Close all streams
inStream.close () ;
outStream.close () ;
System.out.println ( "File successfully uncompressed : " + tgtFilePath ) ;
} catch ( IOException e ) {
e.printStackTrace () ;
}
}
}
It gives the following output,
File successfully uncompressed : C:\prop_file.txt