Java GZIPOutputStream Tutorial

Jakob Jenkov
Last update: 2017-01-16

The Java GZIPOutputStream class (java.util.zip.GZIPOutStream) can be used to GZIP compress data and write it to an OutputStream.

Creating a GZIPOutputStream

Before you can use a GZIPOutputStream you must create a GZIPOutputStream instance. Here is an example of creating a GZIPOutputStream:

FileOutputStream outputStream     = new FileOutputStream("myfile.zip");
GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream);

This example creates a GZIPOutputStream that GZIP compresses all data you write to it, and write the compressed data to the underlying OutputStream (a FileOutputStream).

Writing Data to a GZIPOutputStream

You can write data to a Java GZIPOutputStream just like you write data to any other OutputStream. Here is an example of writing data to a GZIPOutputStream:

byte[] data = ... ; // get data from somewhere.

gzipOutputStream.write(data);

When you are done writing data to the GZIPOutputStream you have to close it. You close a GZIPOutputStream by calling its close() method. Here is an example of how that is done:

gzipOutputStream.close();

You can also close a GZIPOutputStream using the try-with-resources construct like this:

try(
    FileOutputStream outputStream     = new FileOutputStream("myfile.zip");
    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)
    ) {
        byte[] data = ... ; // get data from somewhere.
        gzipOutputStream.write(data);
}

When the try-with-resources block exits, the GZIPOutputStream and the FileOutputStream will be closed.

Jakob Jenkov

Featured Videos















Core Software Performance Optimization Principles




Advertisements

High-Performance
Java Persistence
Close TOC

All Trails

Trail TOC

Page TOC

Previous

Next