Tech and Media Labs
This site uses cookies to improve the user experience.




Java GZIPInputStream Tutorial

Jakob Jenkov
Last update: 2017-01-11

The Java GZIPInputStream class (java.util.zip.GZIPInputStream) 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("myfile.zip");
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 myfile.zip. 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 = gzipInputStream.read();
while(data != -1){
    //do something with data
    data = gzipInputStream.read();
}

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 :

gzipInputStream.close();

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("myfile.zip"))) {
    int data = gzipInputStream.read();
    while(data != -1){
        //do something with data
        data = gzipInputStream.read();
    }
}

Jakob Jenkov




Copyright  Jenkov Aps
Close TOC