How to decompress gzip file in java


 

How to decompress gzip file in java

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

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

Ads