Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Compressing and Decompressing files using GZIP Format in java

  • Last Updated : 02 Jun, 2017

The java.util.zip package provides classes compress and decompress the file contents. FileInputStream, FileOutputStream and GZIPOutputStream classes are provided in Java to compress and decompress the files.

Compressing a File using GZIPOutputStream

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Methods used in the program

  1. read(): Reads a byte of data. Present in FileInputStream.
    int read()
  2. write(): Writes a byte of data, Present in FileOutputStream.
    void write(int b) 

In this example, we have a text file in /home/saket/Desktop/GeeksforGeeks/compress.java drive under “GeeksforGeeks” Folder and we are compressing and generating the GZip file in the same folder.






// Java program to compress a File
// using GZIPOutputStream class
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
  
public class GeeksForGeeks
{
    static final String OUTPUT_FILE
            = "/home/saket/Desktop/GeeksforGeeks/compress.gz";
    static final String INPUT_FILE
            = "/home/saket/Desktop//GeeksforGeeks/compress.java";
      
    static void compress()
    {
        byte[] buffer = new byte[1024];
        try
        {
            GZIPOutputStream os = 
                    new GZIPOutputStream(new FileOutputStream(OUTPUT_FILE));
                      
            FileInputStream in =
                    new FileInputStream(INPUT_FILE);
              
            int totalSize;
            while((totalSize = in.read(buffer)) > 0 )
            {
                os.write(buffer, 0, totalSize);
            }
              
            in.close();
            os.finish();
            os.close();
              
            System.out.println("File Successfully compressed");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
          
    }
      
    public static void main (String[] args)
    {
        compress();
          
    }
}

Output:

File Successfully compressed

After Running the above program, It will compress the compress.java file:

Decompressing a File using GZIPOutputStream




// Java program to illustrate 
// Decompressing a File using GZIPOutputStream
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
  
class GeeksForGeeks
{
    static final String INPUT_FILE 
            = "/home/saket/Desktop/GeeksforGeeks/compress.gz";
    static final String OUTPUT_FILE 
            = "/home/saket/Desktop//GeeksforGeeks/decompress.java";
      
    static void decompress()
    {
        byte[] buffer = new byte[1024];
        try
        {
            GZIPInputStream is = 
                    new GZIPInputStream(new FileInputStream(INPUT_FILE));
                      
            FileOutputStream out =
                    new FileOutputStream(OUTPUT_FILE);
              
            int totalSize;
            while((totalSize = is.read(buffer)) > 0 )
            {
                out.write(buffer, 0, totalSize);
            }
              
            out.close();
            is.close();
              
            System.out.println("File Successfully decompressed");
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
          
    }
      
    public static void main (String[] args)
    {
        decompress();
          
    }
}

Output :

File Successfully decompressed

Current State after compiling:

Note : Here decom.java contains the above code.
After Running the above program, It will decompress the compress.gz and a file named decompress.java will be created.

This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!