Java GZIPInputStream Tutorial

Jakob Jenkov
Last update: 2017-01-11

The Java GZIPInputStream class ( can be used to decompress files that are compressed with the GZIP compression algorithm, for instance via the GZIPOutputStream class.

Creating a GZIPInputStream

To use the Java GZIPInputStream you must first create a GZIPInputStream instance. Here is an example of creating a GZIPInputStream instance:

InputStream     fileInputStream = new FileInputStream("");
GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);

The GZIPInputStream constructor takes an InputStream as parameter. In the example above I passed it a FileInputStream connected to a file named The GZIPInputStream will then read the GZIP'ed data in the file and decompress it.

Reading Data From a GZIPInputStream

After creating a GZIPInputStream you can read the decompressed data from it just like you would read data from any other InputStream. Here is an example of reading data from a Java GZIPInputStream:

int data =;
while(data != -1){
    //do something with data
    data =;

Closing a GZIPInputStream

When you are finished reading data from the GZIPInputStream you should close it using its close() method. Here is a Java example of closing a GZIPInputStream :


You can also open a GZIPInputStream using the try-with-resources construct, so the GZIPInputStream is closed automatically when you are done using it. Here is how that looks:

try(GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(""))) {
    int data =;
    while(data != -1){
        //do something with data
        data =;

Jakob Jenkov

Featured Videos

Java ForkJoinPool

P2P Networks Introduction


Java Persistence
Close TOC
All Tutorial Trails
All Trails
Table of contents (TOC) for this tutorial trail
Trail TOC
Table of contents (TOC) for this tutorial
Page TOC
Previous tutorial in this tutorial trail
Next tutorial in this tutorial trail