Package java.util.zip

Class GZIPInputStream

All Implemented Interfaces:
Closeable, AutoCloseable

public class GZIPInputStream
extends InflaterInputStream
The GZIPInputStream class is used to read data stored in the GZIP format, reading and decompressing GZIP data from the underlying stream into its buffer.

Example

Using GZIPInputStream is easier than ZipInputStream because GZIP is only for compression, and is not a container for multiple files. This code decompresses the data from a GZIP stream, similar to the gunzip(1) utility.

 InputStream is = ...
 GZIPInputStream zis = new GZIPInputStream(new BufferedInputStream(is));
 try {
     // Reading from 'zis' gets you the uncompressed bytes...
     processStream(zis);
 } finally {
     zis.close();
 }
 
  • Field Details

    • GZIP_MAGIC

      public static final int GZIP_MAGIC
      The magic header for the GZIP format.
      See Also:
      Constant Field Values
    • crc

      protected CRC32 crc
      The checksum algorithm used when handling uncompressed data.
    • eos

      protected boolean eos
      Indicates the end of the input stream.
  • Constructor Details

    • GZIPInputStream

      public GZIPInputStream​(InputStream is) throws IOException
      Construct a GZIPInputStream to read from GZIP data from the underlying stream.
      Parameters:
      is - the InputStream to read data from.
      Throws:
      IOException - if an IOException occurs.
    • GZIPInputStream

      public GZIPInputStream​(InputStream is, int size) throws IOException
      Construct a GZIPInputStream to read from GZIP data from the underlying stream. Set the internal buffer size to size.
      Parameters:
      is - the InputStream to read data from.
      size - the internal read buffer size.
      Throws:
      IOException - if an IOException occurs.
  • Method Details

    • close

      public void close() throws IOException
      Closes this stream and any underlying streams.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Overrides:
      close in class InflaterInputStream
      Throws:
      IOException - If an error occurs closing the input stream.
    • read

      public int read​(byte[] buffer, int byteOffset, int byteCount) throws IOException
      Description copied from class: InflaterInputStream
      Reads up to byteCount bytes of decompressed data and stores it in buffer starting at byteOffset. Returns the number of uncompressed bytes read, or -1.
      Overrides:
      read in class InflaterInputStream
      Throws:
      IOException - if the stream is closed or another IOException occurs.