Support Online
Skip to main content

Java GZIP Guide: File Compression and Decompression Procedures

📘 What Will You Learn in This Guide?

In this guide, you'll learn how to compress and decompress files using the GZIPOutputStream and GZIPInputStream classes from Java's java.util.zip package.
GZIP is preferred to speed up data transfer, especially in Unix-based systems and web servers.

⚙️ What is GZIP?

GZIP (GNU Zip) is an open source format used to compress a single file.
The difference with ZIP is that it can compress only single files — it does not archive folders.

💡 Advantages:

  • Saves disk space by reducing file sizes.
  • Speeds up data transmission over the network.
  • It is simple and platform independent.

🧩 Step 1: Compressing the File with GZIP

The following method reads the contents of a file and writes it to a new file with the extension .gz.

private static void dosyayiGzipIleSikistir(String kaynakDosya, String gzipDosya) {
try {
FileInputStream giris = new FileInputStream(kaynakDosya);
FileOutputStream cikis = new FileOutputStream(gzipDosya);
GZIPOutputStream gzip = new GZIPOutputStream(cikis);

byte[] tampon = new byte[1024];
int uzunluk;

while ((uzunluk = giris.read(tampon)) != -1) {
gzip.write(tampon, 0, uzunluk);
}

gzip.close();
cikis.close();
giris.close();
} catch (IOException e) {
e.printStackTrace();
}
}

💬 Description: This method compresses data using GZIPOutputStream class. Here the Decorator Pattern is applied: GZIPOutputStream adds compression to the data stream by wrapping it over FileOutputStream.


🧩 Step 2: Decompressing the GZIP File

This method reads the .gz file and writes the decompressed data to a new file.


private static void gzipDosyasiniAc(String gzipDosya, String yeniDosya) {
try {
FileInputStream giris = new FileInputStream(gzipDosya);
GZIPInputStream gzip = new GZIPInputStream(giris);
FileOutputStream cikis = new FileOutputStream(yeniDosya);

byte[] tampon = new byte[1024];
int uzunluk;

while ((uzunluk = gzip.read(tampon)) != -1) {
cikis.write(tampon, 0, uzunluk);
}

cikis.close();
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}

💬 Description: GZIPInputStream decompresses the compressed data step by step (stream-based) and writes it to the new file.


🧩 Step 3: Use in the Master Class


import java.io.*;
import java.util.zip.*;

public class GenixNodeGZIPOrnek {

public static void main(String[] args) {
String kaynak = "/home/tr1-node01/yedek.log";
String gzip = "/home/tr1-node01/yedek.log.gz";
String yeni = "/home/tr1-node01/yedek_yeniden.log";

dosyayiGzipIleSikistir(kaynak, gzip);
gzipDosyasiniAc(gzip, yeni);
}
}

💬 Description: The main class calls compression and decompression operations sequentially. In this way, the file is both converted to .gz format and opened back.


ClassMission
GZIPOutputStreamCompresses the file in .gz format.
GZIPInputStreamOpens the .gz file.
FileInputStreamReads the file (provides input stream).
FileOutputStreamWrites data to disk.

🧾 Possible Errors and Solutions

Error:


java.util.zip.ZipException: Not in GZIP format

💡 Solution:

This error occurs if the file being tried to be opened is not in GZIP format. Use a .gz file compressed in the correct format.


⚡ Performance Note

Using a 1024-byte buffer improves memory efficiency for large files.

Larger buffers (8192 etc.) can improve read/write speed.


❓ Frequently Asked Questions (FAQ)

  1. Can GZIP compress folders?

No, it only compresses a single file. For folders, the .tar.gz combination should be used.

  1. Why is GZIPOutputStream wrapped over FileOutputStream?

This is the Decorator Design Pattern. It is applied to give extra functionality (compression) to the file stream.

  1. How do I test if I get an error?

You can check the integrity of the file with the gzip -t file.gz command on Linux/macOS.

  1. Are there any languages ​​other than Java that support GZIP?

Yes, many languages ​​offer GZIP support, such as Python (gzip module) and Node.js (zlib).


🎯 Result

You now know how to compress and decompress files in GZIP format in Java. This technique saves lives in many areas such as log management, data backup and network optimization.

💡 Extra: You can set up a test environment on GenixNode to optimize your Java-based projects and instantly see the performance difference.