Open In App

GZIPOutputStream Class in Java

Improve
Improve
Like Article
Like
Save
Share
Report

The java.util.zip 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:

Example 

Java




// Java Program to Illustrate GZIPOutputStream class
 
// Importing required classes
import java.io.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
 
// 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 = fis.read(buffer)) != -1) {
 
            gzipOS.write(buffer, 0, len);
        }
 
        // Closing the resources
        // using standard close() method
        gzipOS.close();
        fos.close();
        fis.close();
 
        // Display message on the console in order to
        // illustrate successful execution of the program
        System.out.println("File successfully compressed");
    }
}


Output:

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.



Last Updated : 26 Nov, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads