Open In App
Related Articles

GZIPOutputStream Class in Java

Improve Article
Save Article
Like Article

The package provides classes to compress and decompress the file contents. FileInputStream, FileOutputStream, and GZIPOutputStream classes are provided in Java to compress and decompress the files. The GZIPOutputStream class is useful for writing compressed data in GZIP file format. However, GZIP is not a zip tool, it only use to compress a file into a “.gz” format, not compress several files into a single archive.

The constructors and the corresponding action performed is as follows: 

  • GZIPOutputStream(OutputStream out): Creates a new output stream with a default buffer size
  • GZIPOutputStream(OutputStream out, boolean syncFlush): Creates a new output stream with a default buffer size and the specified flush mode.
  • GZIPOutputStream(OutputStream out, int size): Creates a new output stream with the specified buffer size
  • GZIPOutputStream(OutputStream out, int size, boolean syncFlush): Creates a new output stream with the specified buffer size and flush mode

Let us discuss the important methods involved, which are as follows:

  • void write(byte[] buf, int off, int len): Writes an array of bytes to the compressed output stream.

Parameters:  It takes 3 parameters, namely as follows:

  • buf: Data to be written
  • off: Start offset of the data
  • len: Length of the data

Exception: IOException: If an I/O error has occurred

Note: finish() method finishes writing compressed data to the output stream without closing the underlying stream.

Implementation: We have a text file in D:/Myfolder/New.txt, and ” Hello World ” is written in this text file. We are compressing this text file (New.txt)  and generating the GZip file in the same folder. It is also pictorially depicted below as follows:



// Java Program to Illustrate GZIPOutputStream class
// Importing required classes
// Main class
class GFG {
    // Main driver method
    public static void main(String[] args) throws Exception
        // Existing file path
        String file = "D:/Myfolder/New.txt";
        // Path where we want the compression of the file
        String gzipFile = "D:/Myfolder/compress.gz";
        // Reading the text file
        FileInputStream fis = new FileInputStream(file);
        // Creating the compressed file
        FileOutputStream fos
            = new FileOutputStream(gzipFile);
        // Object of Fileoutstream passed
        GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
        byte[] buffer = new byte[1024];
        int len;
        // Writing the data to file until -1 reached(End of
        // file)
        while ((len = != -1) {
            gzipOS.write(buffer, 0, len);
        // Closing the resources
        // using standard close() method
        // Display message on the console in order to
        // illustrate successful execution of the program
        System.out.println("File successfully compressed");


File successfully compressed

After running the above program, it will compress the file generated that can be depicted from snapshot below by comparing it with the above sampling snapshot.

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 26 Nov, 2022
Like Article
Save Article
Similar Reads
Related Tutorials