Java IO Tutorial

Java – Decompress a Gzip file

In previous article, we show how to compress a file in Gzip format. This article shows how to decompress a Gzip file.

1. Decompress a Gzip file – GZIPInputStream

1.1 This example decompress a Gzip file sitemap.xml.gz back to its original file sitemap.xml. We copy the GZIPInputStream into a FileOutputStream to decompress a Gzip file.

GZipExample.java

package com.mkyong.io.howto.compress;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.GZIPInputStream;

public class GZipExample {

    public static void main(String[] args) {

        Path source = Paths.get("/home/mkyong/test/sitemap.xml.gz");
        Path target = Paths.get("/home/mkyong/test/sitemap.xml");

        if (Files.notExists(source)) {
            System.err.printf("The path %s doesn't exist!", source);
            return;
        }

        try {

            GZipExample.decompressGzip(source, target);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void decompressGzip(Path source, Path target) throws IOException {

        try (GZIPInputStream gis = new GZIPInputStream(
                                      new FileInputStream(source.toFile()));
             FileOutputStream fos = new FileOutputStream(target.toFile())) {

            // copy GZIPInputStream to FileOutputStream
            byte[] buffer = new byte[1024];
            int len;
            while ((len = gis.read(buffer)) > 0) {
                fos.write(buffer, 0, len);
            }

        }

    }

}

2. Decompress a Gzip file, again.

2.1 This example is similar to example 1; instead, we use NIO File.copy to copy a file.


import java.nio.file.Files;
import java.util.zip.GZIPInputStream;

  //...
  public static void decompressGzipNio(Path source, Path target) throws IOException {

      try (GZIPInputStream gis = new GZIPInputStream(
                                    new FileInputStream(source.toFile()))) {

          Files.copy(gis, target);
      }

  }

3. Decompress a Gzip file to byte arrays

3.1 This example decompress a Gzip file into a byte[] directly without saving it into a local file.

GZipExample3.java

package com.mkyong.io.howto.compress;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.zip.GZIPInputStream;

public class GZipExample3 {

    public static void main(String[] args) {

        // decompress
        Path source = Paths.get("/home/mkyong/test/sitemap.xml.gz");

        if (Files.notExists(source)) {
            System.err.printf("The path %s doesn't exist!", source);
            return;
        }

        try {

            byte[] bytes = GZipExample.decompressGzipToBytes(source);
            // do task for the byte[]

            //convert byte[] to string for display
            System.out.println(new String(bytes, StandardCharsets.UTF_8));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // decompress a Gzip file into a byte arrays
    public static byte[] decompressGzipToBytes(Path source) throws IOException {

        ByteArrayOutputStream output = new ByteArrayOutputStream();

        try (GZIPInputStream gis = new GZIPInputStream(
                                      new FileInputStream(source.toFile()))) {

            // copy GZIPInputStream to ByteArrayOutputStream
            byte[] buffer = new byte[1024];
            int len;
            while ((len = gis.read(buffer)) > 0) {
                output.write(buffer, 0, len);
            }

        }

        return output.toByteArray();

    }


}

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-io

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
TA
11 years ago

It is a good idea to put the close() method calls within finally clause, prevents resource leakage 😉

Alexandru Prodan
10 years ago

Following your example I wrote below how to do same thing but to
retrieve gziped text files to a String.
Is there any better container then String for big xml files?

Blob blob = rs.getBlob("ACR_FILE_CONTENT");
byte[] bdata = blob.getBytes(1, (int) blob.length());
ByteArrayInputStream bais = new ByteArrayInputStream(bdata);
String text=gunzipIt(bais,"UTF-8");

public static String gunzipIt(ByteArrayInputStream bais, String format){
byte[] buffer = new byte[1024];
String str=null;

if(format==null||format.isEmpty()){
format="UTF-8";
}
try{
GZIPInputStream gzis = new GZIPInputStream(bais);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int len;
while ((len = gzis.read(buffer)) > 0)
{
out.write(buffer, 0, len);
}
str=out.toString(format);
gzis.close();
out.close();
}catch(IOException ex){
ex.printStackTrace();
}
return str;
}

Last edited 3 years ago by mkyong