Share on Google+Share on Google+

How to decompress gzip file in java

Advertisement
In this tutorial you will see the use of GZIPInputStream for decompress gzip file.

How to decompress a GZIP file.

In this tutorial, we will discuss about how to decompress a  GZIP file. The java.util.zip package provide a class named GZIPInputStream. The GZIPInputStream class create a output stream  for reading compress data and decompress GZIP. 

About GZIPInputStream API:

Return Type Method Description 
void close() The close() is use to close all stream.
int read() The read() method read and decompress GZIP data from associated stream.

Code

import java.util.zip.*;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.util.zip.GZIPInputStream;
import java.io.IOException;


public class UnGzip{
  public static void main(String args[]){
    try{
     
         String source = "dd.txt.gz";
   String outFilename = source +".txt";
     System.out.println("File for Extracting : "+source);
         FileInputStream instream= new FileInputStream(source);
         GZIPInputStream ginstream =new GZIPInputStream(instream);
         FileOutputStream outstream = new FileOutputStream(outFilename);
         byte[] buf = new byte[1024]
         int len;
         while ((len = ginstream.read(buf)) 0
        {
          outstream.write(buf, 0, len);
        }
   System.out.println("File Successfylly Extract");
     System.out.println("Extract file : "+outFilename);
        ginstream.close();
        outstream.close();
      }
    
    catch(IOException ioe){
      System.out.println("Exception has been thrown" + ioe);
    }
  }

Output

C:\>java UnGzip
File for Extracting : dd.txt.gz
File Successfylly Extract
Extract File : dd.txt.gz.txt
C:\Work\Bharat\Roseindi_Tutorial\Zip_api\GzipUncopresser>

Download this code

Advertisements

Posted on: July 13, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.