Open In App

Compressing and Decompressing files using GZIP Format in java

Last Updated : 14 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.




// 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.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads