Class ZipArchiveInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, InputStreamStatistics
    Direct Known Subclasses:
    JarArchiveInputStream

    public class ZipArchiveInputStream
    extends ArchiveInputStream
    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 65536 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
    • Constructor Summary

      Constructors 
      Constructor Description
      ZipArchiveInputStream​(java.io.InputStream inputStream)
      Create an instance using UTF-8 encoding
      ZipArchiveInputStream​(java.io.InputStream inputStream, java.lang.String encoding)
      Create an instance using the specified encoding
      ZipArchiveInputStream​(java.io.InputStream inputStream, java.lang.String encoding, boolean useUnicodeExtraFields)
      Create an instance using the specified encoding
      ZipArchiveInputStream​(java.io.InputStream inputStream, java.lang.String encoding, boolean useUnicodeExtraFields, boolean allowStoredEntriesWithDataDescriptor)
      Create an instance using the specified encoding
      ZipArchiveInputStream​(java.io.InputStream inputStream, java.lang.String encoding, boolean useUnicodeExtraFields, boolean allowStoredEntriesWithDataDescriptor, boolean skipSplitSig)
      Create an instance using the specified encoding
    • Constructor Detail

      • ZipArchiveInputStream

        public ZipArchiveInputStream​(java.io.InputStream inputStream)
        Create an instance using UTF-8 encoding
        Parameters:
        inputStream - the stream to wrap
      • ZipArchiveInputStream

        public ZipArchiveInputStream​(java.io.InputStream inputStream,
                                     java.lang.String encoding)
        Create 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​(java.io.InputStream inputStream,
                                     java.lang.String encoding,
                                     boolean useUnicodeExtraFields)
        Create 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​(java.io.InputStream inputStream,
                                     java.lang.String encoding,
                                     boolean useUnicodeExtraFields,
                                     boolean allowStoredEntriesWithDataDescriptor)
        Create 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​(java.io.InputStream inputStream,
                                     java.lang.String encoding,
                                     boolean useUnicodeExtraFields,
                                     boolean allowStoredEntriesWithDataDescriptor,
                                     boolean skipSplitSig)
        Create 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 Detail

      • getNextZipEntry

        public ZipArchiveEntry getNextZipEntry()
                                        throws java.io.IOException
        Throws:
        java.io.IOException
      • getNextEntry

        public ArchiveEntry getNextEntry()
                                  throws java.io.IOException
        Description copied from class: ArchiveInputStream
        Returns the next Archive Entry in this Stream.
        Specified by:
        getNextEntry in class ArchiveInputStream
        Returns:
        the next entry, or null if there are no more entries
        Throws:
        java.io.IOException - if the next entry could not be read
      • canReadEntryData

        public boolean canReadEntryData​(ArchiveEntry ae)
        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
        Parameters:
        ae - the entry to test
        Returns:
        This implementation always returns true.
        Since:
        1.1
      • read

        public int read​(byte[] buffer,
                        int offset,
                        int length)
                 throws java.io.IOException
        Overrides:
        read in class java.io.InputStream
        Throws:
        java.io.IOException
      • getCompressedCount

        public long getCompressedCount()
        Specified by:
        getCompressedCount in interface InputStreamStatistics
        Returns:
        the amount of raw or compressed bytes read by the stream
        Since:
        1.17
      • getUncompressedCount

        public long getUncompressedCount()
        Specified by:
        getUncompressedCount in interface InputStreamStatistics
        Returns:
        the amount of decompressed bytes returned by the stream
        Since:
        1.17
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Overrides:
        close in class java.io.InputStream
        Throws:
        java.io.IOException
      • skip

        public long skip​(long value)
                  throws java.io.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 java.io.InputStream
        Parameters:
        value - the number of bytes to be skipped.
        Returns:
        the actual number of bytes skipped.
        Throws:
        java.io.IOException - - if an I/O error occurs.
        java.lang.IllegalArgumentException - - if value is negative.
      • 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