Chapter 5. gzip compression

Table of Contents

Standalone gzip tools

gzip is a compression format that was created by Jean-Loup Gailly and Mark Adler in the early nineties. See the Wikipedia article on gzip.

gzip is supported through Java's GZipOutputStream and GZipInputStream. They filter the data sent to or read from another stream to compress it and decompress it, respectively.

EntityFS has with the GZipReadableFile and GZipWritableFile classes. They can transparently decompress data read from a file and compress data written to a file.

Example 5.1. Working with gzip using readable and writable gzip files

// Data will be compressed to the File f

String toCompress = "Compress me!";

// Wrap the File in a ReadWritableFileAdapter to make it a
// ReadWritableFile
ReadWritableFile fa = new ReadWritableFileAdapter(f);

// Write the data using the EntityFS utility class Files and a
// GZipWritableFile with default compression settings.
Files.writeText(new GZipWritableFile(fa), toCompress);

// Read the data, again using Files. The data is read from a
// GZipReadableFile.
// This will print out "Compress me!"
System.out.println(
  Files.readTextFile(
    new GZipReadableFile(fa)));


The GZip and GUnzip classes have runnable main methods that emulate the behavior of the gzip and gunzip commands. See their API documentation for details on how to use them.