Compressing and Decompressing files using GZIP Format in java

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

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
          
    }
}

chevron_right


Output:

File Successfully compressed

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

Decompressing a File using GZIPOutputStream

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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();
          
    }
}

chevron_right


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


Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.