Class ZipFile

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ZipFile
    extends java.lang.Object
    implements java.io.Closeable
    Replacement for java.util.ZipFile.

    This class adds support for file name encodings other than UTF-8 (which is required to work on ZIP files created by native zip tools and is able to skip a preamble like the one found in self extracting archives. Furthermore it returns instances of org.apache.commons.compress.archivers.zip.ZipArchiveEntry instead of java.util.zip.ZipEntry.

    It doesn't extend java.util.zip.ZipFile as it would have to reimplement all methods anyway. Like java.util.ZipFile, it uses SeekableByteChannel under the covers and supports compressed and uncompressed entries. As of Apache Commons Compress 1.3 it also transparently supports Zip64 extensions and thus individual entries and archives larger than 4 GB or with more than 65536 entries.

    The method signatures mimic the ones of java.util.zip.ZipFile, with a couple of exceptions:

    • There is no getName method.
    • entries has been renamed to getEntries.
    • getEntries and getEntry return org.apache.commons.compress.archivers.zip.ZipArchiveEntry instances.
    • close is allowed to throw IOException.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.channels.SeekableByteChannel archive
      The actual data source.
      private java.lang.String archiveName
      File name of actual source.
      (package private) static int BYTE_SHIFT  
      private static int CFD_LOCATOR_OFFSET
      Offset of the field that holds the location of the first central directory entry inside the "End of central directory record" relative to the start of the "End of central directory record".
      private static int CFH_LEN
      Length of a "central directory" entry structure without file name, extra fields or comment.
      private static long CFH_SIG  
      private java.nio.ByteBuffer cfhBbuf  
      private byte[] cfhBuf  
      private boolean closed
      Whether the file is closed.
      private java.nio.ByteBuffer dwordBbuf  
      private byte[] dwordBuf  
      private java.lang.String encoding
      The encoding to use for file names and the file comment.
      private java.util.List<ZipArchiveEntry> entries
      List of entries in the order they appear inside the central directory.
      private static int HASH_SIZE  
      private static long LFH_OFFSET_FOR_FILENAME_LENGTH
      Number of bytes in local file header up to the "length of file name" entry.
      private static int MAX_EOCD_SIZE
      Maximum length of the "End of central directory record" with a file comment.
      (package private) static int MIN_EOCD_SIZE
      Length of the "End of central directory record" - which is supposed to be the last structure of the archive - without file comment.
      private java.util.Map<java.lang.String,​java.util.LinkedList<ZipArchiveEntry>> nameMap
      Maps String to list of ZipArchiveEntrys, name -> actual entries.
      (package private) static int NIBLET_MASK  
      private java.util.Comparator<ZipArchiveEntry> offsetComparator
      Compares two ZipArchiveEntries based on their offset within the archive.
      private static byte[] ONE_ZERO_BYTE  
      private static int POS_0  
      private static int POS_1  
      private static int POS_2  
      private static int POS_3  
      private byte[] shortBuf  
      private boolean useUnicodeExtraFields
      Whether to look for and use Unicode extra fields.
      private java.nio.ByteBuffer wordBbuf  
      private byte[] wordBuf  
      private static int ZIP64_EOCD_CFD_LOCATOR_OFFSET
      Offset of the field that holds the location of the first central directory entry inside the "Zip64 end of central directory record" relative to the start of the "Zip64 end of central directory record".
      private static int ZIP64_EOCDL_LENGTH
      Length of the "Zip64 end of central directory locator" - which should be right in front of the "end of central directory record" if one is present at all.
      private static int ZIP64_EOCDL_LOCATOR_OFFSET
      Offset of the field that holds the location of the "Zip64 end of central directory record" inside the "Zip64 end of central directory locator" relative to the start of the "Zip64 end of central directory locator".
      private ZipEncoding zipEncoding
      The zip encoding to use for file names and the file comment.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        ZipFile​(java.io.File f)
      Opens the given file for reading, assuming "UTF8" for file names.
        ZipFile​(java.io.File f, java.lang.String encoding)
      Opens the given file for reading, assuming the specified encoding for file names and scanning for unicode extra fields.
        ZipFile​(java.io.File f, java.lang.String encoding, boolean useUnicodeExtraFields)
      Opens the given file for reading, assuming the specified encoding for file names.
        ZipFile​(java.io.File f, java.lang.String encoding, boolean useUnicodeExtraFields, boolean ignoreLocalFileHeader)
      Opens the given file for reading, assuming the specified encoding for file names.
        ZipFile​(java.lang.String name)
      Opens the given file for reading, assuming "UTF8".
        ZipFile​(java.lang.String name, java.lang.String encoding)
      Opens the given file for reading, assuming the specified encoding for file names, scanning unicode extra fields.
        ZipFile​(java.nio.channels.SeekableByteChannel channel)
      Opens the given channel for reading, assuming "UTF8" for file names.
        ZipFile​(java.nio.channels.SeekableByteChannel channel, java.lang.String encoding)
      Opens the given channel for reading, assuming the specified encoding for file names.
        ZipFile​(java.nio.channels.SeekableByteChannel channel, java.lang.String archiveName, java.lang.String encoding, boolean useUnicodeExtraFields)
      Opens the given channel for reading, assuming the specified encoding for file names.
        ZipFile​(java.nio.channels.SeekableByteChannel channel, java.lang.String archiveName, java.lang.String encoding, boolean useUnicodeExtraFields, boolean ignoreLocalFileHeader)
      Opens the given channel for reading, assuming the specified encoding for file names.
      private ZipFile​(java.nio.channels.SeekableByteChannel channel, java.lang.String archiveName, java.lang.String encoding, boolean useUnicodeExtraFields, boolean closeOnError, boolean ignoreLocalFileHeader)  
    • Field Detail

      • ONE_ZERO_BYTE

        private static final byte[] ONE_ZERO_BYTE
      • entries

        private final java.util.List<ZipArchiveEntry> entries
        List of entries in the order they appear inside the central directory.
      • nameMap

        private final java.util.Map<java.lang.String,​java.util.LinkedList<ZipArchiveEntry>> nameMap
        Maps String to list of ZipArchiveEntrys, name -> actual entries.
      • zipEncoding

        private final ZipEncoding zipEncoding
        The zip encoding to use for file names and the file comment.
      • archiveName

        private final java.lang.String archiveName
        File name of actual source.
      • archive

        private final java.nio.channels.SeekableByteChannel archive
        The actual data source.
      • useUnicodeExtraFields

        private final boolean useUnicodeExtraFields
        Whether to look for and use Unicode extra fields.
      • closed

        private volatile boolean closed
        Whether the file is closed.
      • dwordBuf

        private final byte[] dwordBuf
      • wordBuf

        private final byte[] wordBuf
      • cfhBuf

        private final byte[] cfhBuf
      • shortBuf

        private final byte[] shortBuf
      • dwordBbuf

        private final java.nio.ByteBuffer dwordBbuf
      • wordBbuf

        private final java.nio.ByteBuffer wordBbuf
      • cfhBbuf

        private final java.nio.ByteBuffer cfhBbuf
      • CFH_LEN

        private static final int CFH_LEN
        Length of a "central directory" entry structure without file name, extra fields or comment.
        See Also:
        Constant Field Values
      • CFH_SIG

        private static final long CFH_SIG
      • MIN_EOCD_SIZE

        static final int MIN_EOCD_SIZE
        Length of the "End of central directory record" - which is supposed to be the last structure of the archive - without file comment.
        See Also:
        Constant Field Values
      • MAX_EOCD_SIZE

        private static final int MAX_EOCD_SIZE
        Maximum length of the "End of central directory record" with a file comment.
        See Also:
        Constant Field Values
      • CFD_LOCATOR_OFFSET

        private static final int CFD_LOCATOR_OFFSET
        Offset of the field that holds the location of the first central directory entry inside the "End of central directory record" relative to the start of the "End of central directory record".
        See Also:
        Constant Field Values
      • ZIP64_EOCDL_LENGTH

        private static final int ZIP64_EOCDL_LENGTH
        Length of the "Zip64 end of central directory locator" - which should be right in front of the "end of central directory record" if one is present at all.
        See Also:
        Constant Field Values
      • ZIP64_EOCDL_LOCATOR_OFFSET

        private static final int ZIP64_EOCDL_LOCATOR_OFFSET
        Offset of the field that holds the location of the "Zip64 end of central directory record" inside the "Zip64 end of central directory locator" relative to the start of the "Zip64 end of central directory locator".
        See Also:
        Constant Field Values
      • ZIP64_EOCD_CFD_LOCATOR_OFFSET

        private static final int ZIP64_EOCD_CFD_LOCATOR_OFFSET
        Offset of the field that holds the location of the first central directory entry inside the "Zip64 end of central directory record" relative to the start of the "Zip64 end of central directory record".
        See Also:
        Constant Field Values
      • LFH_OFFSET_FOR_FILENAME_LENGTH

        private static final long LFH_OFFSET_FOR_FILENAME_LENGTH
        Number of bytes in local file header up to the "length of file name" entry.
        See Also:
        Constant Field Values
      • offsetComparator

        private final java.util.Comparator<ZipArchiveEntry> offsetComparator
        Compares two ZipArchiveEntries based on their offset within the archive.

        Won't return any meaningful results if one of the entries isn't part of the archive at all.

        Since:
        1.1
    • Constructor Detail

      • ZipFile

        public ZipFile​(java.io.File f)
                throws java.io.IOException
        Opens the given file for reading, assuming "UTF8" for file names.
        Parameters:
        f - the archive.
        Throws:
        java.io.IOException - if an error occurs while reading the file.
      • ZipFile

        public ZipFile​(java.lang.String name)
                throws java.io.IOException
        Opens the given file for reading, assuming "UTF8".
        Parameters:
        name - name of the archive.
        Throws:
        java.io.IOException - if an error occurs while reading the file.
      • ZipFile

        public ZipFile​(java.lang.String name,
                       java.lang.String encoding)
                throws java.io.IOException
        Opens the given file for reading, assuming the specified encoding for file names, scanning unicode extra fields.
        Parameters:
        name - name of the archive.
        encoding - the encoding to use for file names, use null for the platform's default encoding
        Throws:
        java.io.IOException - if an error occurs while reading the file.
      • ZipFile

        public ZipFile​(java.io.File f,
                       java.lang.String encoding)
                throws java.io.IOException
        Opens the given file for reading, assuming the specified encoding for file names and scanning for unicode extra fields.
        Parameters:
        f - the archive.
        encoding - the encoding to use for file names, use null for the platform's default encoding
        Throws:
        java.io.IOException - if an error occurs while reading the file.
      • ZipFile

        public ZipFile​(java.io.File f,
                       java.lang.String encoding,
                       boolean useUnicodeExtraFields)
                throws java.io.IOException
        Opens the given file for reading, assuming the specified encoding for file names.
        Parameters:
        f - the archive.
        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.
        Throws:
        java.io.IOException - if an error occurs while reading the file.
      • ZipFile

        public ZipFile​(java.io.File f,
                       java.lang.String encoding,
                       boolean useUnicodeExtraFields,
                       boolean ignoreLocalFileHeader)
                throws java.io.IOException
        Opens the given file for reading, assuming the specified encoding for file names.

        By default the central directory record and all local file headers of the archive will be read immediately which may take a considerable amount of time when the archive is big. The ignoreLocalFileHeader parameter can be set to true which restricts parsing to the central directory. Unfortunately the local file header may contain information not present inside of the central directory which will not be available when the argument is set to true. This includes the content of the Unicode extra field, so setting ignoreLocalFileHeader to true means useUnicodeExtraFields will be ignored effectively. Also getRawInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) is always going to return null if ignoreLocalFileHeader is true.

        Parameters:
        f - the archive.
        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.
        ignoreLocalFileHeader - whether to ignore information stored inside the local file header (see the notes in this method's javadoc)
        Throws:
        java.io.IOException - if an error occurs while reading the file.
        Since:
        1.19
      • ZipFile

        public ZipFile​(java.nio.channels.SeekableByteChannel channel)
                throws java.io.IOException
        Opens the given channel for reading, assuming "UTF8" for file names.

        SeekableInMemoryByteChannel allows you to read from an in-memory archive.

        Parameters:
        channel - the archive.
        Throws:
        java.io.IOException - if an error occurs while reading the file.
        Since:
        1.13
      • ZipFile

        public ZipFile​(java.nio.channels.SeekableByteChannel channel,
                       java.lang.String encoding)
                throws java.io.IOException
        Opens the given channel for reading, assuming the specified encoding for file names.

        SeekableInMemoryByteChannel allows you to read from an in-memory archive.

        Parameters:
        channel - the archive.
        encoding - the encoding to use for file names, use null for the platform's default encoding
        Throws:
        java.io.IOException - if an error occurs while reading the file.
        Since:
        1.13
      • ZipFile

        public ZipFile​(java.nio.channels.SeekableByteChannel channel,
                       java.lang.String archiveName,
                       java.lang.String encoding,
                       boolean useUnicodeExtraFields)
                throws java.io.IOException
        Opens the given channel for reading, assuming the specified encoding for file names.

        SeekableInMemoryByteChannel allows you to read from an in-memory archive.

        Parameters:
        channel - the archive.
        archiveName - name of the archive, used for error messages only.
        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.
        Throws:
        java.io.IOException - if an error occurs while reading the file.
        Since:
        1.13
      • ZipFile

        public ZipFile​(java.nio.channels.SeekableByteChannel channel,
                       java.lang.String archiveName,
                       java.lang.String encoding,
                       boolean useUnicodeExtraFields,
                       boolean ignoreLocalFileHeader)
                throws java.io.IOException
        Opens the given channel for reading, assuming the specified encoding for file names.

        SeekableInMemoryByteChannel allows you to read from an in-memory archive.

        By default the central directory record and all local file headers of the archive will be read immediately which may take a considerable amount of time when the archive is big. The ignoreLocalFileHeader parameter can be set to true which restricts parsing to the central directory. Unfortunately the local file header may contain information not present inside of the central directory which will not be available when the argument is set to true. This includes the content of the Unicode extra field, so setting ignoreLocalFileHeader to true means useUnicodeExtraFields will be ignored effectively. Also getRawInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveEntry) is always going to return null if ignoreLocalFileHeader is true.

        Parameters:
        channel - the archive.
        archiveName - name of the archive, used for error messages only.
        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.
        ignoreLocalFileHeader - whether to ignore information stored inside the local file header (see the notes in this method's javadoc)
        Throws:
        java.io.IOException - if an error occurs while reading the file.
        Since:
        1.19
      • ZipFile

        private ZipFile​(java.nio.channels.SeekableByteChannel channel,
                        java.lang.String archiveName,
                        java.lang.String encoding,
                        boolean useUnicodeExtraFields,
                        boolean closeOnError,
                        boolean ignoreLocalFileHeader)
                 throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • getEncoding

        public java.lang.String getEncoding()
        The encoding to use for file names and the file comment.
        Returns:
        null if using the platform's default character encoding.
      • close

        public void close()
                   throws java.io.IOException
        Closes the archive.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException - if an error occurs closing the archive.
      • closeQuietly

        public static void closeQuietly​(ZipFile zipfile)
        close a zipfile quietly; throw no io fault, do nothing on a null parameter
        Parameters:
        zipfile - file to close, can be null
      • getEntries

        public java.util.Enumeration<ZipArchiveEntry> getEntries()
        Returns all entries.

        Entries will be returned in the same order they appear within the archive's central directory.

        Returns:
        all entries as ZipArchiveEntry instances
      • getEntriesInPhysicalOrder

        public java.util.Enumeration<ZipArchiveEntry> getEntriesInPhysicalOrder()
        Returns all entries in physical order.

        Entries will be returned in the same order their contents appear within the archive.

        Returns:
        all entries as ZipArchiveEntry instances
        Since:
        1.1
      • getEntry

        public ZipArchiveEntry getEntry​(java.lang.String name)
        Returns a named entry - or null if no entry by that name exists.

        If multiple entries with the same name exist the first entry in the archive's central directory by that name is returned.

        Parameters:
        name - name of the entry.
        Returns:
        the ZipArchiveEntry corresponding to the given name - or null if not present.
      • getEntries

        public java.lang.Iterable<ZipArchiveEntry> getEntries​(java.lang.String name)
        Returns all named entries in the same order they appear within the archive's central directory.
        Parameters:
        name - name of the entry.
        Returns:
        the Iterable<ZipArchiveEntry> corresponding to the given name
        Since:
        1.6
      • getEntriesInPhysicalOrder

        public java.lang.Iterable<ZipArchiveEntry> getEntriesInPhysicalOrder​(java.lang.String name)
        Returns all named entries in the same order their contents appear within the archive.
        Parameters:
        name - name of the entry.
        Returns:
        the Iterable<ZipArchiveEntry> corresponding to the given name
        Since:
        1.6
      • canReadEntryData

        public boolean canReadEntryData​(ZipArchiveEntry ze)
        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.

        Parameters:
        ze - the entry
        Returns:
        whether this class is able to read the given entry.
        Since:
        1.1
      • getRawInputStream

        public java.io.InputStream getRawInputStream​(ZipArchiveEntry ze)
        Expose the raw stream of the archive entry (compressed form).

        This method does not relate to how/if we understand the payload in the stream, since we really only intend to move it on to somewhere else.

        Parameters:
        ze - The entry to get the stream for
        Returns:
        The raw input stream containing (possibly) compressed data.
        Since:
        1.11
      • copyRawEntries

        public void copyRawEntries​(ZipArchiveOutputStream target,
                                   ZipArchiveEntryPredicate predicate)
                            throws java.io.IOException
        Transfer selected entries from this zipfile to a given #ZipArchiveOutputStream. Compression and all other attributes will be as in this file.

        This method transfers entries based on the central directory of the zip file.

        Parameters:
        target - The zipArchiveOutputStream to write the entries to
        predicate - A predicate that selects which entries to write
        Throws:
        java.io.IOException - on error
      • getInputStream

        public java.io.InputStream getInputStream​(ZipArchiveEntry ze)
                                           throws java.io.IOException
        Returns an InputStream for reading the contents of the given entry.
        Parameters:
        ze - the entry to get the stream for.
        Returns:
        a stream to read the entry from. The returned stream implements InputStreamStatistics.
        Throws:
        java.io.IOException - if unable to create an input stream from the zipentry
      • getUnixSymlink

        public java.lang.String getUnixSymlink​(ZipArchiveEntry entry)
                                        throws java.io.IOException

        Convenience method to return the entry's content as a String if isUnixSymlink() returns true for it, otherwise returns null.

        This method assumes the symbolic link's file name uses the same encoding that as been specified for this ZipFile.

        Parameters:
        entry - ZipArchiveEntry object that represents the symbolic link
        Returns:
        entry's content as a String
        Throws:
        java.io.IOException - problem with content's input stream
        Since:
        1.5
      • finalize

        protected void finalize()
                         throws java.lang.Throwable
        Ensures that the close method of this zipfile is called when there are no more references to it.
        Overrides:
        finalize in class java.lang.Object
        Throws:
        java.lang.Throwable
        See Also:
        close()
      • populateFromCentralDirectory

        private java.util.Map<ZipArchiveEntry,​ZipFile.NameAndComment> populateFromCentralDirectory()
                                                                                                  throws java.io.IOException
        Reads the central directory of the given archive and populates the internal tables with ZipArchiveEntry instances.

        The ZipArchiveEntrys will know all data that can be obtained from the central directory alone, but not the data that requires the local file header or additional data to be read.

        Returns:
        a map of zipentries that didn't have the language encoding flag set when read.
        Throws:
        java.io.IOException
      • readCentralDirectoryEntry

        private void readCentralDirectoryEntry​(java.util.Map<ZipArchiveEntry,​ZipFile.NameAndComment> noUTF8Flag)
                                        throws java.io.IOException
        Reads an individual entry of the central directory, creats an ZipArchiveEntry from it and adds it to the global maps.
        Parameters:
        noUTF8Flag - map used to collect entries that don't have their UTF-8 flag set and whose name will be set by data read from the local file header later. The current entry may be added to this map.
        Throws:
        java.io.IOException
      • setSizesAndOffsetFromZip64Extra

        private void setSizesAndOffsetFromZip64Extra​(ZipArchiveEntry ze,
                                                     int diskStart)
                                              throws java.io.IOException
        If the entry holds a Zip64 extended information extra field, read sizes from there if the entry's sizes are set to 0xFFFFFFFFF, do the same for the offset of the local file header.

        Ensures the Zip64 extra either knows both compressed and uncompressed size or neither of both as the internal logic in ExtraFieldUtils forces the field to create local header data even if they are never used - and here a field with only one size would be invalid.

        Throws:
        java.io.IOException
      • positionAtCentralDirectory

        private void positionAtCentralDirectory()
                                         throws java.io.IOException
        Searches for either the "Zip64 end of central directory locator" or the "End of central dir record", parses it and positions the stream at the first central directory record.
        Throws:
        java.io.IOException
      • positionAtCentralDirectory64

        private void positionAtCentralDirectory64()
                                           throws java.io.IOException
        Parses the "Zip64 end of central directory locator", finds the "Zip64 end of central directory record" using the parsed information, parses that and positions the stream at the first central directory record. Expects stream to be positioned right behind the "Zip64 end of central directory locator"'s signature.
        Throws:
        java.io.IOException
      • positionAtCentralDirectory32

        private void positionAtCentralDirectory32()
                                           throws java.io.IOException
        Parses the "End of central dir record" and positions the stream at the first central directory record. Expects stream to be positioned at the beginning of the "End of central dir record".
        Throws:
        java.io.IOException
      • positionAtEndOfCentralDirectoryRecord

        private void positionAtEndOfCentralDirectoryRecord()
                                                    throws java.io.IOException
        Searches for the and positions the stream at the start of the "End of central dir record".
        Throws:
        java.io.IOException
      • tryToLocateSignature

        private boolean tryToLocateSignature​(long minDistanceFromEnd,
                                             long maxDistanceFromEnd,
                                             byte[] sig)
                                      throws java.io.IOException
        Searches the archive backwards from minDistance to maxDistance for the given signature, positions the RandomaccessFile right at the signature if it has been found.
        Throws:
        java.io.IOException
      • skipBytes

        private void skipBytes​(int count)
                        throws java.io.IOException
        Skips the given number of bytes or throws an EOFException if skipping failed.
        Throws:
        java.io.IOException
      • resolveLocalFileHeaderData

        private void resolveLocalFileHeaderData​(java.util.Map<ZipArchiveEntry,​ZipFile.NameAndComment> entriesWithoutUTF8Flag)
                                         throws java.io.IOException
        Walks through all recorded entries and adds the data available from the local file header.

        Also records the offsets for the data to read from the entries.

        Throws:
        java.io.IOException
      • fillNameMap

        private void fillNameMap()
      • setDataOffset

        private int[] setDataOffset​(ZipArchiveEntry ze)
                             throws java.io.IOException
        Throws:
        java.io.IOException
      • getDataOffset

        private long getDataOffset​(ZipArchiveEntry ze)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • startsWithLocalFileHeader

        private boolean startsWithLocalFileHeader()
                                           throws java.io.IOException
        Checks whether the archive starts with a LFH. If it doesn't, it may be an empty archive.
        Throws:
        java.io.IOException
      • createBoundedInputStream

        private ZipFile.BoundedInputStream createBoundedInputStream​(long start,
                                                                    long remaining)
        Creates new BoundedInputStream, according to implementation of underlying archive channel.