Back to Java
2026-03-226 min read

File Compression (Java)

Learn File Compression (Java) step by step with clear examples and exercises.

Why This Matters

In today's data-driven world, file compression plays a crucial role in managing storage space, reducing bandwidth usage, and speeding up data transfer rates. By compressing files using Java, developers can create efficient data backup systems, optimize web servers, and handle large datasets effectively. The java.util.zip package offers essential classes for implementing popular file compression algorithms like DEFLATE, GZIP, and ZIP.

Prerequisites

To fully grasp the concepts of file compression in Java, it is crucial to have a solid understanding of:

  1. Basic Java programming concepts (variables, control structures, methods, classes)
  2. File I/O operations in Java (reading from files and writing to files)
  3. Understanding of data structures like arrays and lists
  4. Familiarity with algorithms and data compression techniques
  5. Knowledge of exception handling in Java
  6. Understanding the concept of streams and how they are used for I/O operations
  7. Basic understanding of the DEFLATE algorithm, LZ77 (Sliding Window), and Huffman coding

Core Concept

The java.util.zip package contains vital classes for compressing and decompressing files using the DEFLATE algorithm, which is a combination of LZ77 (a sliding window technique) and Huffman coding. The main classes in this package are:

  1. DeflaterOutputStream: A stream that writes compressed data to an output stream
  2. InflaterInputStream: A stream that reads decompressed data from an input stream
  3. ZipOutputStream: A stream that writes ZIP archives containing multiple compressed files
  4. GZIPOutputStream: A stream that writes GZIP-compressed data to an output stream
  5. ZipEntry: Represents a single file or directory within a ZIP archive
  6. Deflater: A class used for configuring the DEFLATE algorithm's compression level and strategy
  7. Inflater: A class used for decompressing data compressed with the DEFLATE algorithm

DEFLATE Algorithm

The DEFLATE algorithm reduces redundancy in data by using two components: LZ77 (Sliding Window) and Huffman coding.

  1. LZ77 (Sliding Window): It looks for repeated patterns within the input data, called "matches," and replaces them with a reference to an earlier occurrence of that pattern. The sliding window technique allows finding matches efficiently.
  2. Huffman Coding: It is a lossless data compression method that assigns variable-length codes to symbols based on their frequency. Frequently occurring symbols receive shorter codes, resulting in more efficient storage.

Compressing Files with Java

To compress a file using the DEFLATE algorithm, follow these steps:

  1. Create an OutputStream object for the compressed data (e.g., GZIPOutputStream or DeflaterOutputStream).
  2. Set the compression level (0-9, where 0 is no compression and 9 is maximum compression) and other configuration options like strategy (DEFLATE_NO_COMPRESSION, DEFLATE_BEST_SPEED, DEFLATE_BEST_COMPRESSION).
  3. Wrap the output stream with a BufferedOutputStream to improve write performance.
  4. Write the input file content to the output stream using a FileInputStream.
  5. Close the streams in reverse order of creation (output, buffered, and input).

Here's an example of compressing a file using GZIP:

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

public class CompressFile {
public static void main(String[] args) throws IOException {
String inputFilePath = "input.txt";
String outputFilePath = "output.gz";

try (
FileInputStream fis = new FileInputStream(inputFilePath);
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outputFilePath));
BufferedOutputStream bos = new BufferedOutputStream(gzos);
) {
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
bos.write(buffer, 0, length);
}
}
}
}

Worked Example

Let's compress a sample text file named input.txt using GZIP and then decompress it back to its original form:

Creating the input file (input.txt)

This is an example of file compression in Java. We will be using GZIP for this demonstration.

Compression techniques like DEFLATE, GZIP, and ZIP are essential for saving storage space and reducing bandwidth usage. They help speed up data transfer rates in real-world scenarios such as developing efficient data backup systems, optimizing web servers, and handling large datasets.

Compressing the input file (CompressFile.java)

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

public class CompressFile {
public static void main(String[] args) throws IOException {
String inputFilePath = "input.txt";
String outputFilePath = "output.gz";

try (
FileInputStream fis = new FileInputStream(inputFilePath);
GZIPOutputStream gzos = new GZIPOutputStream(new FileOutputStream(outputFilePath));
BufferedOutputStream bos = new BufferedOutputStream(gzos);
) {
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
bos.write(buffer, 0, length);
}
}
}
}

Decompressing the compressed file (DecompressFile.java)

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

public class DecompressFile {
public static void main(String[] args) throws IOException {
String inputFilePath = "output.gz";
String outputFilePath = "decompressed_output.txt";

try (
FileInputStream fis = new FileInputStream(inputFilePath);
GZIPInputStream gzis = new GZIPInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(outputFilePath));
) {
byte[] buffer = new byte[1024];
int length;
while ((length = gzis.read(buffer)) > 0) {
bos.write(buffer, 0, length);
}
}
}
}

Common Mistakes

  1. Not setting the compression level: Remember to set a compression level (0-9) when using DeflaterOutputStream.
  2. Not closing streams in reverse order: Always close output, buffered, and input streams in that order to ensure proper cleanup of resources.
  3. Using incorrect input or output file paths: Ensure the correct file paths for both the input and output files are provided.
  4. Not handling exceptions: Properly handle exceptions when working with I/O operations to avoid runtime errors.
  5. Ignoring buffer size: Setting an appropriate buffer size (e.g., 1024 bytes) can improve performance by reducing the number of read and write calls.
  6. Forgetting to flush the output stream: Flushing the output stream ensures that all buffered data is written to the underlying stream.
  7. Not checking for null input or output streams: Always check if the input and output streams are not null before using them.
  8. Not setting the strategy: When using DeflaterOutputStream, you can set a strategy (DEFLATE_NO_COMPRESSION, DEFLATE_BEST_SPEED, DEFLATE_BEST_COMPRESSION) to optimize compression based on specific requirements.
  9. Not properly configuring the Deflater: You can configure the Deflater object with various options like setInput and setLevel to fine-tune the compression process.
  10. Forgetting to use a ZipOutputStream for creating a zip archive: When compressing multiple files into a single archive, use the ZipOutputStream class instead of GZIP or DeflaterOutputStream.

Practice Questions

  1. Write a Java program that compresses a directory containing multiple files using the ZIP algorithm.
  2. Implement a Java program that decompresses a GZIP-compressed file.
  3. Create a Java program that measures the compression ratio of a given file using GZIP.
  4. Modify the CompressFile example to use the DEFLATE algorithm instead of GZIP and set the strategy to DEFLATE_BEST_COMPRESSION.
  5. Write a Java program that encrypts and compresses a file using AES encryption and GZIP compression.
  6. Implement a Java program that reads from a compressed file (e.g., ZIP or GZIP) and writes the decompressed content to another file.
  7. Create a Java program that checks if a given file is already compressed using either GZIP or ZIP format.
  8. Write a Java program that creates an archive of multiple files with different compression levels for each file using the ZIP algorithm.
  9. Implement a Java program that splits a large file into smaller chunks and compresses each chunk separately using GZIP.
  10. Create a Java program that merges multiple GZIP-compressed files into a single archive.

FAQ

  1. What is the difference between DEFLATE, GZIP, and ZIP? DEFLATE is an algorithm used for data compression; GZIP and ZIP are file formats based on the DEFLATE algorithm.
  2. Why use Java for file compression? Java provides built-in support for compressing files using popular algorithms like DEFLATE, GZIP, and ZIP through the java.util.zip package.
  3. What is LZ77, and how does it work in the context of file compression? LZ77 (Sliding Window) is a technique used within the DEFLATE algorithm to find repeated patterns in data for efficient compression by replacing them with references to earlier occurrences.
  4. How can I measure the compression ratio of a file using Java? You can calculate the compression ratio by comparing the size of the original file and the compressed file (compressedSize / originalSize).
  5. What are some common pitfalls when working with file compression in Java, and how can they be avoided? Common mistakes include not setting the compression level, not closing streams in reverse order, using incorrect input or output file paths, ignoring buffer size, forgetting to handle exceptions, and neglecting to flush the output stream. Additionally, it's essential to understand the differences between GZIP, ZIP, and DEFLATE, as well as how to properly configure the Deflater object for optimal compression results.
File Compression (Java) | Java | XQA Learn