Package org.apache.commons.compress.archivers.zip

# Class ZipArchiveInputStream

java.lang.Object
java.io.InputStream
java.io.FilterInputStream
org.apache.commons.compress.archivers.ArchiveInputStream<ZipArchiveEntry>
org.apache.commons.compress.archivers.zip.ZipArchiveInputStream

All Implemented Interfaces:
  `Closeable`, `AutoCloseable`, `InputStreamStatistics`

Direct Known Subclasses:
  `JarArchiveInputStream`

public class ZipArchiveInputStream extends ArchiveInputStream<ZipArchiveEntry> implements InputStreamStatistics
Implements an input stream that can read Zip archives.

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`

- may return entries that are not part of the central directory at all and shouldn't be considered part of the archive.
- may return several entries with the same name.
- will not return internal or external attributes.
- may return incomplete extra field data.
- may return unknown sizes and CRC values for entries until the next entry has been reached if the archive uses the data descriptor feature.

See Also:
  - `ZipFile`
This class is not thread-safe

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final int`
  `PREAMBLE_GARBAGE_MAX_SIZE`
  Maximum size of data in the first local file header.

  ### Fields inherited from class java.io.FilterInputStream

  `in`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ZipArchiveInputStream(InputStream inputStream)`
  Constructs an instance using UTF-8 encoding
  `ZipArchiveInputStream(InputStream inputStream, String encoding)`
  Constructs an instance using the specified encoding
  `ZipArchiveInputStream(InputStream inputStream, String encoding, boolean useUnicodeExtraFields)`
  Constructs an instance using the specified encoding
  `ZipArchiveInputStream(InputStream inputStream, String encoding, boolean useUnicodeExtraFields, boolean allowStoredEntriesWithDataDescriptor)`
  Constructs an instance using the specified encoding
  `ZipArchiveInputStream(InputStream inputStream, String encoding, boolean useUnicodeExtraFields, boolean allowStoredEntriesWithDataDescriptor, boolean skipSplitSig)`
  Constructs an instance using the specified encoding

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `canReadEntryData(ArchiveEntry ae)`
  Tests whether this class is able to read the given entry.
  `void`
  `close()`
  `protected InputStream`
  `createZstdInputStream(InputStream in)`
  Creates the appropriate InputStream for the Zstd compression method.
  `long`
  `getCompressedCount()`
  Gets the amount of raw or compressed bytes read by the stream.
  `ZipArchiveEntry`
  `getNextEntry()`
  Gets the next Archive Entry in this Stream.
  `ZipArchiveEntry`
  `getNextZipEntry()`
  Deprecated.
  Use `getNextEntry()`.
  `long`
  `getUncompressedCount()`
  Gets the uncompressed count.
  `static boolean`
  `matches(byte[] signature, int length)`
  Checks if the signature matches what is expected for a ZIP file.
  `int`
  `read(byte[] buffer, int offset, int length)`
  `ZipArchiveInputStream`
  `setExtraFieldSupport(Function<ZipShort,ZipExtraField> extraFieldSupport)`
  Currently unused.
  `long`
  `skip(long value)`
  Skips over and discards value bytes of data from this input stream.

  ### Methods inherited from class org.apache.commons.compress.archivers.ArchiveInputStream

  `count, count, forEach, getBytesRead, getCharset, getCount, iterator, mark, markSupported, pushedBackBytes, read, reset`

  ### Methods inherited from class java.io.FilterInputStream

  `available, read`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Field Details

  - ### PREAMBLE_GARBAGE_MAX_SIZE

    public static final int PREAMBLE_GARBAGE_MAX_SIZE
    Maximum size of data in the first local file header.
    See Also:
      - Constant Field Values

- ## Constructor Details

  - ### ZipArchiveInputStream

    public ZipArchiveInputStream(InputStream inputStream)
    Constructs an instance using UTF-8 encoding
    Parameters:
      `inputStream` - the stream to wrap

  - ### ZipArchiveInputStream

    public ZipArchiveInputStream(InputStream inputStream, String encoding)
    Constructs an instance using the specified encoding
    Parameters:
      `inputStream` - the stream to wrap
      `encoding` - the encoding to use for file names, use null for the platform's default encoding
    Since:
      1.5

  - ### ZipArchiveInputStream

    public ZipArchiveInputStream(InputStream inputStream, String encoding, boolean useUnicodeExtraFields)
    Constructs an instance using the specified encoding
    Parameters:
      `inputStream` - the stream to wrap
      `encoding` - the encoding to use for file names, use null for the platform's default encoding
      `useUnicodeExtraFields` - whether to use InfoZIP Unicode Extra Fields (if present) to set the file names.

  - ### ZipArchiveInputStream

    public ZipArchiveInputStream(InputStream inputStream, String encoding, boolean useUnicodeExtraFields, boolean allowStoredEntriesWithDataDescriptor)
    Constructs an instance using the specified encoding
    Parameters:
      `inputStream` - the stream to wrap
      `encoding` - the encoding to use for file names, use null for the platform's default encoding
      `useUnicodeExtraFields` - whether to use InfoZIP Unicode Extra Fields (if present) to set the file names.
      `allowStoredEntriesWithDataDescriptor` - whether the stream will try to read STORED entries that use a data descriptor
    Since:
      1.1

  - ### ZipArchiveInputStream

    public ZipArchiveInputStream(InputStream inputStream, String encoding, boolean useUnicodeExtraFields, boolean allowStoredEntriesWithDataDescriptor, boolean skipSplitSig)
    Constructs an instance using the specified encoding
    Parameters:
      `inputStream` - the stream to wrap
      `encoding` - the encoding to use for file names, use null for the platform's default encoding
      `useUnicodeExtraFields` - whether to use InfoZIP Unicode Extra Fields (if present) to set the file names.
      `allowStoredEntriesWithDataDescriptor` - whether the stream will try to read STORED entries that use a data descriptor
      `skipSplitSig` - 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.
    Since:
      1.20

- ## Method Details

  - ### matches

    public static boolean matches(byte[] signature, 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.
    Parameters:
      `signature` - the bytes to check
      `length` - the number of bytes to check
    Returns:
      true, if this stream is a ZIP archive stream, false otherwise

  - ### canReadEntryData

    public boolean canReadEntryData(ArchiveEntry ae)
    Tests 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.

    Overrides:
      `canReadEntryData` in class `ArchiveInputStream<ZipArchiveEntry>`
    Parameters:
      `ae` - the entry to test.
    Returns:
      This implementation always returns true.
    Since:
      1.1

  - ### close

    public void close() throws IOException
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`
    Overrides:
      `close` in class `FilterInputStream`
    Throws:
      `IOException`

  - ### createZstdInputStream

    protected InputStream createZstdInputStream(InputStream in) throws IOException
    Creates the appropriate InputStream for the Zstd compression method.
    Parameters:
      `in` - the input stream which should be used for compression.
    Returns:
      the `InputStream` for handling the Zstd compression.
    Throws:
      `IOException` - if an I/O error occurs.
    Since:
      1.28.0

  - ### getCompressedCount

    public long getCompressedCount()
    Description copied from interface: `InputStreamStatistics`
    Gets the amount of raw or compressed bytes read by the stream.
    Specified by:
      `getCompressedCount` in interface `InputStreamStatistics`
    Returns:
      the amount of raw or compressed bytes read by the stream.
    Since:
      1.17

  - ### getNextEntry

    public ZipArchiveEntry getNextEntry() throws IOException
    Description copied from class: `ArchiveInputStream`
    Gets the next Archive Entry in this Stream.
    Specified by:
      `getNextEntry` in class `ArchiveInputStream<ZipArchiveEntry>`
    Returns:
      the next entry, or `null` if there are no more entries.
    Throws:
      `IOException` - if the next entry could not be read.

  - ### getNextZipEntry

    @Deprecated public ZipArchiveEntry getNextZipEntry() throws IOException
    Deprecated.
    Use `getNextEntry()`.
    Gets the next entry.
    Returns:
      the next entry.
    Throws:
      `IOException` - if an I/O error occurs.

  - ### getUncompressedCount

    public long getUncompressedCount()
    Gets the uncompressed count.
    Specified by:
      `getUncompressedCount` in interface `InputStreamStatistics`
    Returns:
      the amount of decompressed bytes returned by the stream.
    Since:
      1.17

  - ### read

    public int read(byte[] buffer, int offset, int length) throws IOException
    Overrides:
      `read` in class `FilterInputStream`
    Throws:
      `IOException`

  - ### setExtraFieldSupport

    public ZipArchiveInputStream setExtraFieldSupport(Function<ZipShort,ZipExtraField> extraFieldSupport)
    Currently unused. Sets the custom extra fields factory.
    Parameters:
      `extraFieldSupport` - the lookup function based on extra field header id.
    Returns:
      the archive.

  - ### skip

    public long skip(long value) throws IOException
    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.

    Overrides:
      `skip` in class `FilterInputStream`
    Parameters:
      `value` - the number of bytes to be skipped.
    Returns:
      the actual number of bytes skipped.
    Throws:
      `IOException` - - if an I/O error occurs.
      `IllegalArgumentException` - - if value is negative.