As of Apache Commons Compress it transparently supports Zip64 extensions and thus individual entries and archives larger than 4 GB or with more than 65,536 entries.
The ZipFile class is preferred when reading from files as ZipArchiveInputStream is limited by not being able to read the central directory
header before returning entries. In particular ZipArchiveInputStream
ZipFile
| Modifier and Type | Class and Description |
|---|---|
| private class | ZipArchiveInputStream.BoundCountInputStream
Input stream adapted from commons-io. |
| private static class | ZipArchiveInputStream.CurrentEntry
Structure collecting information for the entry that is currently being read. |
| Modifier and Type | Field and Description |
|---|---|
| private final boolean | allowStoredEntriesWithDataDescriptor
Whether the stream will try to read STORED entries that use a data descriptor. |
| private static final byte[] | |
| private final ByteBuffer | buf
Buffer used to read from the wrapped stream. |
| private static final byte[] | |
| private static final int | |
| private boolean | closed
Whether the stream has been closed. |
| private ZipArchiveInputStream. | current
The entry that is currently being read. |
| private static final byte[] | |
| private int | |
| private Function | extraFieldSupport
The factory for extra fields or null. |
| private boolean | hitCentralDirectory
Whether the stream has reached the central directory - and thus found all entries. |
| private final Inflater | inf
Inflater used for all deflated entries. |
| private ByteArrayInputStream | lastStoredEntry
When reading a stored entry that uses the data descriptor this stream has to read the full entry and caches it. |
| private static final byte[] | |
| private static final int | |
| private final byte[] | lfhBuf
Cached buffers - must only be used locally in the class (COMPRESS-172 - reduce garbage collection). |
| private static final BigInteger | |
| public static final int | |
| private final byte[] | |
| private final byte[] | |
| private final boolean | skipSplitSig
Whether the stream will try to skip the ZIP split signature(08074B50) at the beginning |
| private static final long | |
| private final byte[] | |
| private long | uncompressedCount
Count decompressed bytes for current entry |
| private static final String | |
| private final boolean | useUnicodeExtraFields
Whether to look for and use Unicode extra fields. |
| private final byte[] | |
| private final ZipEncoding | zipEncoding
The ZIP encoding to use for file names and the file comment. |
| Access | Constructor and Description |
|---|---|
| public | ZipArchiveInputStream(final InputStream
the stream to wrap inputStream)Constructs an instance using UTF-8 encoding |
| public | ZipArchiveInputStream(final InputStream
the stream to wrap inputStream, final String the encoding to use for file names, use null for the platform's default encoding encoding)Constructs an instance using the specified encoding |
| public | ZipArchiveInputStream(final InputStream
the stream to wrap inputStream, final String the encoding to use for file names, use null for the platform's default encoding encoding, final boolean whether to use InfoZIP Unicode Extra Fields (if present) to set the file names. useUnicodeExtraFields)Constructs an instance using the specified encoding |
| public | ZipArchiveInputStream(final InputStream
the stream to wrap inputStream, final String the encoding to use for file names, use null for the platform's default encoding encoding, final boolean whether to use InfoZIP Unicode Extra Fields (if present) to set the file names. useUnicodeExtraFields, final boolean whether the stream will try to read STORED entries that use a data descriptor allowStoredEntriesWithDataDescriptor)Constructs an instance using the specified encoding |
| public | ZipArchiveInputStream(final InputStream
the stream to wrap inputStream, final String the encoding to use for file names, use null for the platform's default encoding encoding, final boolean whether to use InfoZIP Unicode Extra Fields (if present) to set the file names. useUnicodeExtraFields, final boolean whether the stream will try to read STORED entries that use a data descriptor allowStoredEntriesWithDataDescriptor, final boolean Whether the stream will try to skip the zip split signature(08074B50) at the beginning. You will need to set
this to true if you want to read a split archive. skipSplitSig)Constructs an instance using the specified encoding |
| Modifier and Type | Method and Description |
|---|---|
| private boolean | bufferContainsSignature(final ByteArrayOutputStream bos, final int offset, final int lastRead, final int expectedDDLen)
Checks whether the current buffer contains the signature of a "data descriptor", "local file header" or "central directory entry". |
| private int | cacheBytesRead(final ByteArrayOutputStream bos, int offset, final int lastRead, final int expectedDDLen)
If the last read bytes could hold a data descriptor and an incomplete signature then save the last bytes to the front of the buffer and cache everything in front of the potential data descriptor into the given ByteArrayOutputStream. |
| public boolean | canReadEntryData(final ArchiveEntry
the entry to test ae)Overrides org. |
| private static boolean | |
| public void | close()
Overrides java. Implements java. |
| private void | closeEntry()
Closes the current ZIP archive entry and positions the underlying stream to the beginning of the next entry. |
| private boolean | Returns: true, if current entry is determined to have outstanding bytes, false otherwiseIf the compressed size of the current entry is included in the entry header and there are any outstanding bytes in the underlying stream, then this returns true. |
| private void | drainCurrentEntryData()
Read all data of the current entry from the underlying stream that hasn't been read, yet. |
| private int | |
| private boolean | findEocdRecord()
Reads forward until the signature of the "End of central directory" record is found. |
| private long | |
| public long | getCompressedCount()
Implements org. |
| public ZipArchiveEntry | getNextEntry()
Implements abstract org. |
| public ZipArchiveEntry | |
| public long | getUncompressedCount()
Implements org. |
| private boolean | Returns: true if this looks like an APK signing blockthe bytes read from the underlying stream in the expectation that they would hold the local file header of the next entry. suspectLocalFileHeader)Checks whether this might be an APK Signing Block. |
| private boolean | |
| public static boolean | Returns: true, if this stream is a ZIP archive stream, false otherwisethe bytes to check signature, final int the number of bytes to check length)Checks if the signature matches what is expected for a ZIP file. |
| private void | processZip64Extra(final ZipLong size, final ZipLong cSize)
Records whether a Zip64 extra is present and sets the size information from it if sizes are 0xFFFFFFFF and the entry doesn't use a data descriptor. |
| private void | |
| public int | read(final byte[]
the buffer into which the data is read. buffer, final int the start offset in array offset, final int b
at which the data is written.the maximum number of bytes to read. length)Overrides java. len bytes of data from this input stream
into an array of bytes.
|
| private void | |
| private int | readDeflated(final byte[] buffer, final int offset, final int length)
Implementation of read for DEFLATED entries. |
| private boolean | readFirstLocalFileHeader()
Fills the given array with the first local file header and deals with splitting/spanning markers that may prefix the first LFH. |
| private int | readFromInflater(final byte[] buffer, final int offset, final int length)
Potentially reads more bytes to fill the inflater's buffer and reads from it. |
| private void | |
| private void | |
| private int | readOneByte()
Reads bytes by reading from the underlying stream rather than the (potentially inflating) archive stream - which |
| private byte[] | |
| private int | readStored(final byte[] buffer, final int offset, final int length)
Implementation of read for STORED entries. |
| private void | |
| private void | |
| public ZipArchiveInputStream | Returns: the archive.the lookup function based on extra field header id. extraFieldSupport)Enable custom extra fields factory. |
| public long | Returns: the actual number of bytes skipped.the number of bytes to be skipped. value)Overrides java. |
| private void | skipRemainderOfArchive(final boolean read)
Reads the stream until it find the "End of central directory record" and consumes it as well. |
| private boolean | supportsCompressedSizeFor(final ZipArchiveEntry entry)
Whether the compressed size for the entry is either known or not required by the compression method being used. |
| private boolean | Returns: true if allowStoredEntriesWithDataDescriptor is true, the entry doesn't require any data descriptor or the method is DEFLATED or ENHANCED_DEFLATED.Whether this entry requires a data descriptor this library can work with. |
| allowStoredEntriesWithDataDescriptor | back to summary |
|---|---|
| private final boolean allowStoredEntriesWithDataDescriptor Whether the stream will try to read STORED entries that use a data descriptor. Setting it to true means we will not stop reading an entry with the compressed size, instead we will stop reading an entry when a data descriptor is met (by finding the Data Descriptor Signature). This will completely break down in some cases - like JARs in WARs. See also : https://issues.apache.org/jira/projects/COMPRESS/issues/COMPRESS-555 https://github.com/apache/commons-compress/pull/137#issuecomment-690835644 | |
| APK_SIGNING_BLOCK_MAGIC | back to summary |
|---|---|
| private static final byte[] APK_SIGNING_BLOCK_MAGIC | |
| buf | back to summary |
|---|---|
| private final ByteBuffer buf Buffer used to read from the wrapped stream. | |
| CFH | back to summary |
|---|---|
| private static final byte[] CFH | |
| CFH_LEN | back to summary |
|---|---|
| private static final int CFH_LEN | |
| closed | back to summary |
|---|---|
| private boolean closed Whether the stream has been closed. | |
| current | back to summary |
|---|---|
| private ZipArchiveInputStream. The entry that is currently being read. | |
| DD | back to summary |
|---|---|
| private static final byte[] DD | |
| entriesRead | back to summary |
|---|---|
| private int entriesRead | |
| extraFieldSupport | back to summary |
|---|---|
| private Function<ZipShort, ZipExtraField> extraFieldSupport The factory for extra fields or null. | |
| hitCentralDirectory | back to summary |
|---|---|
| private boolean hitCentralDirectory Whether the stream has reached the central directory - and thus found all entries. | |
| inf | back to summary |
|---|---|
| private final Inflater inf Inflater used for all deflated entries. | |
| lastStoredEntry | back to summary |
|---|---|
| private ByteArrayInputStream lastStoredEntry When reading a stored entry that uses the data descriptor this stream has to read the full entry and caches it. This is the cache. | |
| LFH | back to summary |
|---|---|
| private static final byte[] LFH | |
| LFH_LEN | back to summary |
|---|---|
| private static final int LFH_LEN | |
| lfhBuf | back to summary |
|---|---|
| private final byte[] lfhBuf Cached buffers - must only be used locally in the class (COMPRESS-172 - reduce garbage collection). | |
| LONG_MAX | back to summary |
|---|---|
| private static final BigInteger LONG_MAX | |
| PREAMBLE_GARBAGE_MAX_SIZE | back to summary |
|---|---|
| public static final int PREAMBLE_GARBAGE_MAX_SIZE | |
| shortBuf | back to summary |
|---|---|
| private final byte[] shortBuf | |
| skipBuf | back to summary |
|---|---|
| private final byte[] skipBuf | |
| skipSplitSig | back to summary |
|---|---|
| private final boolean skipSplitSig Whether the stream will try to skip the ZIP split signature(08074B50) at the beginning | |
| TWO_EXP_32 | back to summary |
|---|---|
| private static final long TWO_EXP_32 | |
| twoDwordBuf | back to summary |
|---|---|
| private final byte[] twoDwordBuf | |
| uncompressedCount | back to summary |
|---|---|
| private long uncompressedCount Count decompressed bytes for current entry | |
| USE_ZIPFILE_INSTEAD_OF_STREAM_DISCLAIMER | back to summary |
|---|---|
| private static final String USE_ZIPFILE_INSTEAD_OF_STREAM_DISCLAIMER | |
| useUnicodeExtraFields | back to summary |
|---|---|
| private final boolean useUnicodeExtraFields Whether to look for and use Unicode extra fields. | |
| wordBuf | back to summary |
|---|---|
| private final byte[] wordBuf | |
| zipEncoding | back to summary |
|---|---|
| private final ZipEncoding zipEncoding The ZIP encoding to use for file names and the file comment. | |
| ZipArchiveInputStream | back to summary |
|---|---|
| public ZipArchiveInputStream(final InputStream inputStream) Constructs an instance using UTF-8 encoding
| |
| ZipArchiveInputStream | back to summary |
|---|---|
| public ZipArchiveInputStream(final InputStream inputStream, final String encoding) Constructs an instance using the specified encoding
| |
| ZipArchiveInputStream | back to summary |
|---|---|
| public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields) Constructs an instance using the specified encoding
| |
| ZipArchiveInputStream | back to summary |
|---|---|
| public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields, final boolean allowStoredEntriesWithDataDescriptor) Constructs an instance using the specified encoding
| |
| ZipArchiveInputStream | back to summary |
|---|---|
| public ZipArchiveInputStream(final InputStream inputStream, final String encoding, final boolean useUnicodeExtraFields, final boolean allowStoredEntriesWithDataDescriptor, final boolean skipSplitSig) Constructs an instance using the specified encoding
| |
| bufferContainsSignature | back to summary |
|---|---|
| private boolean bufferContainsSignature(final ByteArrayOutputStream bos, final int offset, final int lastRead, final int expectedDDLen) throws IOException Checks whether the current buffer contains the signature of a "data descriptor", "local file header" or "central directory entry". If it contains such a signature, reads the data descriptor and positions the stream right after the data descriptor. | |
| cacheBytesRead | back to summary |
|---|---|
| private int cacheBytesRead(final ByteArrayOutputStream bos, int offset, final int lastRead, final int expectedDDLen) If the last read bytes could hold a data descriptor and an incomplete signature then save the last bytes to the front of the buffer and cache everything in front of the potential data descriptor into the given ByteArrayOutputStream. Data descriptor plus incomplete signature (3 bytes in the worst case) can be 20 bytes max. | |
| canReadEntryData | back to summary |
|---|---|
| public boolean canReadEntryData(final ArchiveEntry ae) Overrides org. Whether this class is able to read the given entry. May return false if it is set up to use encryption or a compression method that hasn't been implemented yet.
| |
| checksig | back to summary |
|---|---|
| private static boolean checksig(final byte[] expected, final byte[] signature) | |
| close | back to summary |
|---|---|
| public void close() throws IOException Overrides java. Implements java. Doc from java. Closes this input stream and releases any system resources associated with the stream.
| |
| closeEntry | back to summary |
|---|---|
| private void closeEntry() throws IOException Closes the current ZIP archive entry and positions the underlying stream to the beginning of the next entry. All per-entry variables and data structures are cleared. If the compressed size of this entry is included in the entry header, then any outstanding bytes are simply skipped from the underlying stream without uncompressing them. This allows an entry to be safely closed even if the compression method is unsupported. In case we don't know the compressed size of this entry or have already buffered too much data from the underlying stream to support uncompression, then the uncompression process is completed and the end position of the stream is adjusted based on the result of that process.
| |
| currentEntryHasOutstandingBytes | back to summary |
|---|---|
| private boolean currentEntryHasOutstandingBytes() If the compressed size of the current entry is included in the entry header and there are any outstanding bytes in the underlying stream, then this returns true.
| |
| drainCurrentEntryData | back to summary |
|---|---|
| private void drainCurrentEntryData() throws IOException Read all data of the current entry from the underlying stream that hasn't been read, yet. | |
| fill | back to summary |
|---|---|
| private int fill() throws IOException | |
| findEocdRecord | back to summary |
|---|---|
| private boolean findEocdRecord() throws IOException Reads forward until the signature of the "End of central directory" record is found. | |
| getBytesInflated | back to summary |
|---|---|
| private long getBytesInflated() Gets the number of bytes Inflater has actually processed. for Java < Java7 the getBytes* methods in Inflater/Deflater seem to return unsigned ints rather than longs that start over with 0 at 2^32. The stream knows how many bytes it has read, but not how many the Inflater actually consumed - it should be between the total number of bytes read for the entry and the total number minus the last read operation. Here we just try to make the value close enough to the bytes we've read by assuming the number of bytes consumed must be smaller than (or equal to) the number of bytes read but not smaller by more than 2^32. | |
| getCompressedCount | back to summary |
|---|---|
| public long getCompressedCount() Implements org. Doc from org. Gets the amount of raw or compressed bytes read by the stream.
| |
| getNextEntry | back to summary |
|---|---|
| public ZipArchiveEntry getNextEntry() throws IOException Implements abstract org. Doc from org. Gets the next Archive Entry in this Stream.
| |
| getNextZipEntry | back to summary |
|---|---|
| public ZipArchiveEntry getNextZipEntry() throws IOException
Deprecated Use Gets the next entry.
| |
| getUncompressedCount | back to summary |
|---|---|
| public long getUncompressedCount() Implements org. Gets the uncompressed count.
| |
| isApkSigningBlock | back to summary |
|---|---|
| private boolean isApkSigningBlock(final byte[] suspectLocalFileHeader) throws IOException Checks whether this might be an APK Signing Block. Unfortunately the APK signing block does not start with some kind of signature, it rather ends with one. It starts with a length, so what we do is parse the suspect length, skip ahead far enough, look for the signature and if we've found it, return true.
| |
| isFirstByteOfEocdSig | back to summary |
|---|---|
| private boolean isFirstByteOfEocdSig(final int b) | |
| matches | back to summary |
|---|---|
| public static boolean matches(final byte[] signature, final int length) Checks if the signature matches what is expected for a ZIP file. Does not currently handle self-extracting ZIPs which may have arbitrary leading content.
| |
| processZip64Extra | back to summary |
|---|---|
| private void processZip64Extra(final ZipLong size, final ZipLong cSize) throws ZipException Records whether a Zip64 extra is present and sets the size information from it if sizes are 0xFFFFFFFF and the entry doesn't use a data descriptor. | |
| pushback | back to summary |
|---|---|
| private void pushback(final byte[] buf, final int offset, final int length) throws IOException | |
| read | back to summary |
|---|---|
| public int read(final byte[] buffer, final int offset, final int length) throws IOException Overrides java. Doc from java. Reads up to
| |
| readDataDescriptor | back to summary |
|---|---|
| private void readDataDescriptor() throws IOException | |
| readDeflated | back to summary |
|---|---|
| private int readDeflated(final byte[] buffer, final int offset, final int length) throws IOException Implementation of read for DEFLATED entries. | |
| readFirstLocalFileHeader | back to summary |
|---|---|
| private boolean readFirstLocalFileHeader() throws IOException Fills the given array with the first local file header and deals with splitting/spanning markers that may prefix the first LFH. | |
| readFromInflater | back to summary |
|---|---|
| private int readFromInflater(final byte[] buffer, final int offset, final int length) throws IOException Potentially reads more bytes to fill the inflater's buffer and reads from it. | |
| readFully | back to summary |
|---|---|
| private void readFully(final byte[] b) throws IOException | |
| readFully | back to summary |
|---|---|
| private void readFully(final byte[] b, final int off) throws IOException | |
| readOneByte | back to summary |
|---|---|
| private int readOneByte() throws IOException Reads bytes by reading from the underlying stream rather than the (potentially inflating) archive stream - which | |
| readRange | back to summary |
|---|---|
| private byte[] readRange(final int len) throws IOException | |
| readStored | back to summary |
|---|---|
| private int readStored(final byte[] buffer, final int offset, final int length) throws IOException Implementation of read for STORED entries. | |
| readStoredEntry | back to summary |
|---|---|
| private void readStoredEntry() throws IOException Caches a stored entry that uses the data descriptor.
After calling this method the entry should know its size, the entry's data is cached and the stream is positioned at the next local file or central directory header. | |
| realSkip | back to summary |
|---|---|
| private void realSkip(final long value) throws IOException Skips bytes by reading from the underlying stream rather than the (potentially inflating) archive stream - which | |
| setExtraFieldSupport | back to summary |
|---|---|
| public ZipArchiveInputStream setExtraFieldSupport(final Function<ZipShort, ZipExtraField> extraFieldSupport) Enable custom extra fields factory.
| |
| skip | back to summary |
|---|---|
| public long skip(final long value) throws IOException Overrides java. Skips over and discards value bytes of data from this input stream. This implementation may end up skipping over some smaller number of bytes, possibly 0, if and only if it reaches the end of the underlying stream. The actual number of bytes skipped is returned.
| |
| skipRemainderOfArchive | back to summary |
|---|---|
| private void skipRemainderOfArchive(final boolean read) throws IOException Reads the stream until it find the "End of central directory record" and consumes it as well. | |
| supportsCompressedSizeFor | back to summary |
|---|---|
| private boolean supportsCompressedSizeFor(final ZipArchiveEntry entry) Whether the compressed size for the entry is either known or not required by the compression method being used. | |
| supportsDataDescriptorFor | back to summary |
|---|---|
| private boolean supportsDataDescriptorFor(final ZipArchiveEntry entry) Whether this entry requires a data descriptor this library can work with.
| |
| Access | Constructor and Description |
|---|---|
| pack-priv | BoundCountInputStream(final InputStream
The wrapped input stream in, final long The maximum number of bytes to return max)Creates a new |
| Modifier and Type | Method and Description |
|---|---|
| private boolean | |
| public int | read()
Overrides org. read() method if
the current position is less than the limit.
|
| public int | read(final byte[]
the buffer to read the bytes into b, final int The start offset off, final int The number of bytes to read len)Overrides org. read(byte[], int, int) method.
|
| private int |
| BoundCountInputStream | back to summary |
|---|---|
| pack-priv BoundCountInputStream(final InputStream in, final long max) Creates a new
| |
| atMaxLength | back to summary |
|---|---|
| private boolean atMaxLength() | |
| read | back to summary |
|---|---|
| public int read() throws IOException Overrides org. Doc from org. Invokes the delegate's
| |
| read | back to summary |
|---|---|
| public int read(final byte[] b, final int off, final int len) throws IOException Overrides org. Doc from org. Invokes the delegate's
| |
| readCount | back to summary |
|---|---|
| private int readCount(final int bytesRead) | |
| Modifier and Type | Field and Description |
|---|---|
| private long | bytesRead
Number of bytes of entry content read by the client if the entry is STORED. |
| private long | bytesReadFromStream
Number of bytes of entry content read from the stream. |
| private final CRC32 | crc
The checksum calculated as the current entry is read. |
| private final ZipArchiveEntry | entry
Current ZIP entry. |
| private boolean | hasDataDescriptor
Does the entry use a data descriptor? |
| private InputStream | inputStream
The input stream decompressing the data for shrunk and imploded entries. |
| private boolean | usesZip64
Does the entry have a ZIP64 extended information extra field. |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method and Description |
|---|---|
| private <T extends InputStream> T |
| bytesRead | back to summary |
|---|---|
| private long bytesRead Number of bytes of entry content read by the client if the entry is STORED. | |
| bytesReadFromStream | back to summary |
|---|---|
| private long bytesReadFromStream Number of bytes of entry content read from the stream. This may be more than the actual entry's length as some stuff gets buffered up and needs to be pushed back when the end of the entry has been reached. | |
| crc | back to summary |
|---|---|
| private final CRC32 crc The checksum calculated as the current entry is read. | |
| entry | back to summary |
|---|---|
| private final ZipArchiveEntry entry Current ZIP entry. | |
| hasDataDescriptor | back to summary |
|---|---|
| private boolean hasDataDescriptor Does the entry use a data descriptor? | |
| inputStream | back to summary |
|---|---|
| private InputStream inputStream The input stream decompressing the data for shrunk and imploded entries. | |
| usesZip64 | back to summary |
|---|---|
| private boolean usesZip64 Does the entry have a ZIP64 extended information extra field. | |
| CurrentEntry | back to summary |
|---|---|
| private CurrentEntry() | |
| checkInputStream | back to summary |
|---|---|
private <T extends InputStream> T checkInputStream()
| |