Class GzipCompressorInputStream
- All Implemented Interfaces:
Closeable
,AutoCloseable
,InputStreamStatistics
This supports decompressing concatenated .gz files which is important when decompressing standalone .gz files.
GZIPInputStream
doesn't decompress concatenated .gz
files: it stops after the first member and silently ignores the rest.
It doesn't leave the read position to point to the beginning of the next
member, which makes it difficult workaround the lack of concatenation
support.
Instead of using GZIPInputStream
, this class has its own .gz
container format decoder. The actual decompression is done with
Inflater
.
If you use the constructor GzipCompressorInputStream(in)
or GzipCompressorInputStream(in, false)
with some
InputStream
in
then read()
will return -1 as soon
as the first internal member has been read completely. The stream
in
will be positioned at the start of the second gzip
member if there is one.
If you use the constructor GzipCompressorInputStream(in,
true)
with some InputStream
in
then read()
will return -1 once the stream in
has been exhausted. The
data read from a stream constructed this way will consist of the
concatenated data of all gzip members contained inside
in
.
- See Also:
-
- "https://tools.ietf.org/html/rfc1952"
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final byte[]
private int
private final CountingInputStream
private final CRC32
private final boolean
private boolean
private static final int
private static final int
private static final int
private static final int
private static final int
private final InputStream
private Inflater
private final byte[]
private final GzipParameters
-
Constructor Summary
ConstructorsConstructorDescriptionGzipCompressorInputStream
(InputStream inputStream) Constructs a new input stream that decompresses gzip-compressed data from the specified input stream.GzipCompressorInputStream
(InputStream inputStream, boolean decompressConcatenated) Constructs a new input stream that decompresses gzip-compressed data from the specified input stream. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes the input stream (unless it is System.in).long
Provides the stream's meta data - may change with each stream when decompressing concatenated streams.private boolean
init
(boolean isFirstMember) static boolean
matches
(byte[] signature, int length) Checks if the signature matches what is expected for a .gz file.int
read()
int
read
(byte[] b, int off, int len) private static byte[]
readToNull
(DataInput inData) Methods inherited from class org.apache.commons.compress.compressors.CompressorInputStream
count, count, getBytesRead, getCount, getUncompressedCount, pushedBackBytes
Methods inherited from class java.io.InputStream
available, mark, markSupported, read, readAllBytes, readNBytes, reset, skip, transferTo
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.apache.commons.compress.utils.InputStreamStatistics
getUncompressedCount
-
Field Details
-
FHCRC
private static final int FHCRC- See Also:
-
FEXTRA
private static final int FEXTRA- See Also:
-
FNAME
private static final int FNAME- See Also:
-
FCOMMENT
private static final int FCOMMENT- See Also:
-
FRESERVED
private static final int FRESERVED- See Also:
-
countingStream
-
in
-
decompressConcatenated
private final boolean decompressConcatenated -
buf
private final byte[] buf -
bufUsed
private int bufUsed -
inf
-
crc
-
endReached
private boolean endReached -
oneByte
private final byte[] oneByte -
parameters
-
-
Constructor Details
-
GzipCompressorInputStream
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
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
isfalse
: This decompressor might read more input than it will actually use. IfinputStream
supportsmark
andreset
, then the input position will be adjusted so that it is right after the last byte of the compressed stream. Ifmark
isn't supported, the input position will be undefined.- Parameters:
inputStream
- the InputStream from which this object should be created ofdecompressConcatenated
- 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
-
getMetaData
Provides the stream's meta data - may change with each stream when decompressing concatenated streams.- Returns:
- the stream's meta data
- Since:
- 1.8
-
init
- Throws:
IOException
-
readToNull
- Throws:
IOException
-
read
- Specified by:
read
in classInputStream
- Throws:
IOException
-
read
- Overrides:
read
in classInputStream
- Throws:
IOException
- Since:
- 1.1
-
matches
public static boolean matches(byte[] signature, int length) Checks if the signature matches what is expected for a .gz file.- Parameters:
signature
- the bytes to checklength
- the number of bytes to check- Returns:
- true if this is a .gz stream, false otherwise
- Since:
- 1.1
-
close
Closes the input stream (unless it is System.in).- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classInputStream
- Throws:
IOException
- Since:
- 1.2
-
getCompressedCount
public long getCompressedCount()- Specified by:
getCompressedCount
in interfaceInputStreamStatistics
- Returns:
- the amount of raw or compressed bytes read by the stream
- Since:
- 1.17
-