Search code examples
javagziparchive

Decompress a Gzip archive in Java


I'm trying to decompress about 8000 files in gzip format in Java. My first try was to use GZIPInputStream but the performance was awful.

Anyone know any alternative to decompress gzip archives? I tried ZipInputStream but it's not recognizing the gzip format.

Thank you in advance.


Solution

  • You need to use buffering. Writing small pieces of data is going to be inefficient. The compression implementation is in native code in the Sun JDK. Even if it wasn't the buffered performance should usually exceed reasonable file or network I/O.

    OutputStream out = new BufferedOutputStream(new GZIPOutputStream(rawOut));
    
    InputStream in = new BufferedInputStream(new GZIPInputStream(rawIn));
    

    As native code is used to implement the decompression/compression algorithm, be very careful to close the stream (and not just the underlying stream) after use. I've found having loads of `Deflaters' hanging around is very bad for performance.

    ZipInputStream deals with archives of files, which is a completely different thing from compressing a stream.