Class GzipCompressorInputStream

java.lang.Object
java.io.InputStream
org.apache.commons.compress.compressors.CompressorInputStream
org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream
All Implemented Interfaces:
Closeable, AutoCloseable, InputStreamStatistics

public class GzipCompressorInputStream extends CompressorInputStream implements InputStreamStatistics
Input stream that decompresses GZIP (.gz) files.

This supports decompressing concatenated GZIP files which is important when decompressing standalone GZIP files.

Instead of using java.util.zip.GZIPInputStream, this class has its own GZIP member decoder. Internally, decompression is done using Inflater.

If you use the constructor GzipCompressorInputStream(in), Builder.setDecompressConcatenated(false), or GzipCompressorInputStream(in, false), then read() will return -1 as soon as the first encoded GZIP member has been completely read. In this case, if the underlying input stream supports mark() and reset(), then it will be left positioned just after the end of the encoded GZIP member; otherwise, some indeterminate number of extra bytes following the encoded GZIP member will have been consumed and discarded.

If you use the Builder.setDecompressConcatenated(true) or GzipCompressorInputStream(in, true) then read() will return -1 only after the entire input stream has been exhausted; any bytes that follow an encoded GZIP member must constitute a new encoded GZIP member, otherwise an IOException is thrown. The data read from a stream constructed this way will consist of the concatenated data of all of the encoded GZIP members in order.

To build an instance, use GzipCompressorInputStream.Builder.

See Also:
  • Field Details

    • NOOP

      private static final org.apache.commons.io.function.IOConsumer<GzipCompressorInputStream> NOOP
    • buf

      private final byte[] buf
      Buffer to hold the input data.
    • bufUsed

      private int bufUsed
      Amount of data in buf.
    • countingStream

      private final org.apache.commons.io.input.BoundedInputStream countingStream
    • crc

      private final CRC32 crc
      CRC32 from uncompressed data.
    • decompressConcatenated

      private final boolean decompressConcatenated
      True if decompressing multi-member streams.
    • endReached

      private boolean endReached
      True once everything has been decompressed.
    • fileNameCharset

      private final Charset fileNameCharset
    • in

      private final InputStream in
      Compressed input stream, possibly wrapped in a BufferedInputStream, always wrapped in countingStream above
    • inflater

      private Inflater inflater
      Decompressor.
    • oneByte

      private final byte[] oneByte
      Buffer for no-argument read method.
    • parameters

      private GzipParameters parameters
    • onMemberStart

      private final org.apache.commons.io.function.IOConsumer<GzipCompressorInputStream> onMemberStart
    • onMemberEnd

      private final org.apache.commons.io.function.IOConsumer<GzipCompressorInputStream> onMemberEnd
  • Constructor Details

    • GzipCompressorInputStream

      private GzipCompressorInputStream(GzipCompressorInputStream.Builder builder) throws IOException
      Throws:
      IOException
    • GzipCompressorInputStream

      public GzipCompressorInputStream(InputStream inputStream) throws IOException
      Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.

      This is equivalent to GzipCompressorInputStream(inputStream, false) and thus will not decompress concatenated .gz files.

      Parameters:
      inputStream - the InputStream from which this object should be created of
      Throws:
      IOException - if the stream could not be created
    • GzipCompressorInputStream

      @Deprecated public GzipCompressorInputStream(InputStream inputStream, boolean decompressConcatenated) throws IOException
      Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.

      If decompressConcatenated is false: This decompressor might read more input than it will actually use. If inputStream supports mark and reset, then the input position will be adjusted so that it is right after the last byte of the compressed stream. If mark isn't supported, the input position will be undefined.

      Parameters:
      inputStream - the InputStream from which this object should be created of
      decompressConcatenated - if true, decompress until the end of the input; if false, stop after the first .gz member
      Throws:
      IOException - if the stream could not be created
  • Method Details