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
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;
class GFG {
public static void main(String[] args) throws Exception
{
String file = "D:/Myfolder/New.txt" ;
String gzipFile = "D:/Myfolder/compress.gz" ;
FileInputStream fis = new FileInputStream(file);
FileOutputStream fos
= new FileOutputStream(gzipFile);
GZIPOutputStream gzipOS = new GZIPOutputStream(fos);
byte [] buffer = new byte [ 1024 ];
int len;
while ((len = fis.read(buffer)) != - 1 ) {
gzipOS.write(buffer, 0 , len);
}
gzipOS.close();
fos.close();
fis.close();
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.

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!