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.tools.zip.ZipEntry 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 RandomAccessFile under the
covers and supports compressed and uncompressed entries. As of
Apache Ant 1.9.0 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:
org.apache.tools.zip.ZipEntry instances.| Modifier and Type | Class and Description |
|---|---|
| private class | ZipFile.BoundedInputStream
InputStream that delegates requests to the underlying RandomAccessFile, making sure that only bytes from a certain range can be read. |
| private static class | ZipFile.Entry
Extends ZipEntry to store the offset within the archive. |
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final RandomAccessFile | archive
The actual data source. |
| private final String | archiveName
File name of actual source. |
| pack-priv static final int | |
| 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". |
| private final byte[] | |
| private static final int | CFH_LEN
Length of a "central directory" entry structure without file name, extra fields or comment. |
| private static final long | |
| private volatile boolean | closed
Whether the file is closed. |
| private final byte[] | |
| private final String | encoding
The encoding to use for filenames and the file comment. |
| private final List | entries
List of entries in the order they appear inside the central directory. |
| private static final int | |
| private static final long | LFH_OFFSET_FOR_FILENAME_LENGTH
Number of bytes in local file header up to the "length of filename" entry. |
| private static final int | MAX_EOCD_SIZE
Maximum length of the "End of central directory record" with a file comment. |
| private 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. |
| private final Map | nameMap
Maps String to list of ZipEntrys, name -> actual entries. |
| pack-priv static final int | |
| private final Comparator | OFFSET_COMPARATOR
Compares two ZipEntries based on their offset within the archive. |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private final byte[] | |
| private final boolean | useUnicodeExtraFields
Whether to look for and use Unicode extra fields. |
| private final byte[] | |
| 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". |
| 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. |
| 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". |
| private final ZipEncoding | zipEncoding
The zip encoding to use for filenames and the file comment. |
| Access | Constructor and Description |
|---|---|
| public | |
| public | |
| public | |
| public | |
| public | ZipFile(final File
the archive. f, 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)Opens the given file for reading, assuming the specified encoding for file names. |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | Returns: booleanZipEntry ze)Whether this class is able to read the given entry. |
| public void | |
| public static void | closeQuietly(final ZipFile
file to close, can be null zipfile)close a zipfile quietly; throw no io fault, do nothing on a null parameter |
| protected void | finalize()
Overrides java. |
| public String | Returns: null if using the platform's default character encoding.The encoding to use for filenames and the file comment. |
| public Enumeration | |
| public Iterable | Returns: the Iterable<ZipEntry> corresponding to the given namename of the entry. name)Returns all named entries in the same order they appear within the archive's central directory. |
| public Enumeration | Returns: all entries asZipEntry instancesReturns all entries in physical order. |
| public Iterable | Returns: the Iterable<ZipEntry> corresponding to the given namename of the entry. name)Returns all named entries in the same order their contents appear within the archive. |
| public ZipEntry | |
| public InputStream | Returns: a stream to read the entry from.the entry to get the stream for. ze)Returns an InputStream for reading the contents of the given entry. |
| public String | |
| private Map | Returns: a map of zipentries that didn't have the language encoding flag set when read.Reads the central directory of the given archive and populates the internal tables with ZipEntry instances. |
| private void | positionAtCentralDirectory()
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. |
| private void | positionAtCentralDirectory32()
Searches for the "End of central dir record", parses it and positions the stream at the first central directory record. |
| private void | positionAtCentralDirectory64()
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. |
| private void | positionAtEndOfCentralDirectoryRecord()
Searches for the and positions the stream at the start of the "End of central dir record". |
| private void | readCentralDirectoryEntry(final Map<ZipEntry, ZipFile.
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. noUTF8FlagReads an individual entry of the central directory, creates an ZipEntry from it and adds it to the global maps. |
| private void | resolveLocalFileHeaderData(final Map<ZipEntry, ZipFile.
Walks through all recorded entries and adds the data available from the local file header. |
| private void | setSizesAndOffsetFromZip64Extra(final ZipEntry ze, final ZipFile.
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. |
| private void | skipBytes(final int count)
Skips the given number of bytes or throws an EOFException if skipping failed. |
| private boolean | |
| private boolean | tryToLocateSignature(final long minDistanceFromEnd, final long maxDistanceFromEnd, final byte[] sig)
Searches the archive backwards from minDistance to maxDistance for the given signature, positions the RandomAccessFile right at the signature if it has been found. |
| archive | back to summary |
|---|---|
| private final RandomAccessFile archive The actual data source. | |
| archiveName | back to summary |
|---|---|
| private final String archiveName File name of actual source. | |
| BYTE_SHIFT | back to summary |
|---|---|
| pack-priv static final int BYTE_SHIFT | |
| CFD_LOCATOR_OFFSET | back to summary |
|---|---|
| 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". | |
| CFH_BUF | back to summary |
|---|---|
| private final byte[] CFH_BUF | |
| CFH_LEN | back to summary |
|---|---|
| private static final int CFH_LEN Length of a "central directory" entry structure without file name, extra fields or comment. | |
| CFH_SIG | back to summary |
|---|---|
| private static final long CFH_SIG | |
| closed | back to summary |
|---|---|
| private volatile boolean closed Whether the file is closed. | |
| DWORD_BUF | back to summary |
|---|---|
| private final byte[] DWORD_BUF | |
| encoding | back to summary |
|---|---|
| private final String encoding The encoding to use for filenames and the file comment. For a list of possible values see https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html. Defaults to the platform's default character encoding. | |
| entries | back to summary |
|---|---|
| private final List<ZipEntry> entries List of entries in the order they appear inside the central directory. | |
| HASH_SIZE | back to summary |
|---|---|
| private static final int HASH_SIZE | |
| LFH_OFFSET_FOR_FILENAME_LENGTH | back to summary |
|---|---|
| private static final long LFH_OFFSET_FOR_FILENAME_LENGTH Number of bytes in local file header up to the "length of filename" entry. | |
| MAX_EOCD_SIZE | back to summary |
|---|---|
| private static final int MAX_EOCD_SIZE Maximum length of the "End of central directory record" with a file comment. | |
| MIN_EOCD_SIZE | back to summary |
|---|---|
| private 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. | |
| nameMap | back to summary |
|---|---|
| private final Map<String, LinkedList<ZipEntry>> nameMap Maps String to list of ZipEntrys, name -> actual entries. | |
| NIBLET_MASK | back to summary |
|---|---|
| pack-priv static final int NIBLET_MASK | |
| OFFSET_COMPARATOR | back to summary |
|---|---|
| private final Comparator<ZipEntry> OFFSET_COMPARATOR Compares two ZipEntries 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.
| |
| POS_0 | back to summary |
|---|---|
| private static final int POS_0 | |
| POS_1 | back to summary |
|---|---|
| private static final int POS_1 | |
| POS_2 | back to summary |
|---|---|
| private static final int POS_2 | |
| POS_3 | back to summary |
|---|---|
| private static final int POS_3 | |
| SHORT_BUF | back to summary |
|---|---|
| private final byte[] SHORT_BUF | |
| useUnicodeExtraFields | back to summary |
|---|---|
| private final boolean useUnicodeExtraFields Whether to look for and use Unicode extra fields. | |
| WORD_BUF | back to summary |
|---|---|
| private final byte[] WORD_BUF | |
| ZIP64_EOCD_CFD_LOCATOR_OFFSET | back to summary |
|---|---|
| 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". | |
| ZIP64_EOCDL_LENGTH | back to summary |
|---|---|
| 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. | |
| ZIP64_EOCDL_LOCATOR_OFFSET | back to summary |
|---|---|
| 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". | |
| zipEncoding | back to summary |
|---|---|
| private final ZipEncoding zipEncoding The zip encoding to use for filenames and the file comment. | |
| ZipFile | back to summary |
|---|---|
| public ZipFile(final File f) throws IOException Opens the given file for reading, assuming the platform's native encoding for file names.
| |
| ZipFile | back to summary |
|---|---|
| public ZipFile(final String name) throws IOException Opens the given file for reading, assuming the platform's native encoding for file names.
| |
| ZipFile | back to summary |
|---|---|
| public ZipFile(final String name, final String encoding) throws IOException Opens the given file for reading, assuming the specified encoding for file names, scanning unicode extra fields.
| |
| ZipFile | back to summary |
|---|---|
| public ZipFile(final File f, final String encoding) throws IOException Opens the given file for reading, assuming the specified encoding for file names and scanning for unicode extra fields.
| |
| ZipFile | back to summary |
|---|---|
| public ZipFile(final File f, final String encoding, final boolean useUnicodeExtraFields) throws IOException Opens the given file for reading, assuming the specified encoding for file names.
| |
| canReadEntryData | back to summary |
|---|---|
| public boolean canReadEntryData(final ZipEntry 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.
| |
| close | back to summary |
|---|---|
| public void close() throws IOException Implements java. Closes the archive.
| |
| closeQuietly | back to summary |
|---|---|
| public static void closeQuietly(final ZipFile zipfile) close a zipfile quietly; throw no io fault, do nothing on a null parameter
| |
| finalize | back to summary |
|---|---|
| protected void finalize() throws Throwable Overrides java. Ensures that the close method of this zipfile is called when there are no more references to it.
| |
| getEncoding | back to summary |
|---|---|
| public String getEncoding() The encoding to use for filenames and the file comment.
| |
| getEntries | back to summary |
|---|---|
| public Enumeration Returns all entries. Entries will be returned in the same order they appear within the archive's central directory.
| |
| getEntries | back to summary |
|---|---|
| public Iterable Returns all named entries in the same order they appear within the archive's central directory. | |
| getEntriesInPhysicalOrder | back to summary |
|---|---|
| public Enumeration Returns all entries in physical order. Entries will be returned in the same order their contents appear within the archive.
| |
| getEntriesInPhysicalOrder | back to summary |
|---|---|
| public Iterable Returns all named entries in the same order their contents appear within the archive. | |
| getEntry | back to summary |
|---|---|
| public ZipEntry getEntry(final String name) Returns a named entry - or If multiple entries with the same name exist the first entry in the archive's central directory by that name is returned. | |
| getInputStream | back to summary |
|---|---|
| public InputStream getInputStream(final ZipEntry ze) throws IOException, ZipException Returns an InputStream for reading the contents of the given entry.
| |
| getName | back to summary |
|---|---|
| public String getName() | |
| populateFromCentralDirectory | back to summary |
|---|---|
| private Map Reads the central directory of the given archive and populates the internal tables with ZipEntry instances. The ZipEntrys 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.
| |
| positionAtCentralDirectory | back to summary |
|---|---|
| private void positionAtCentralDirectory() throws 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. | |
| positionAtCentralDirectory32 | back to summary |
|---|---|
| private void positionAtCentralDirectory32() throws IOException Searches for the "End of central dir record", parses it and positions the stream at the first central directory record. | |
| positionAtCentralDirectory64 | back to summary |
|---|---|
| private void positionAtCentralDirectory64() throws 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. | |
| positionAtEndOfCentralDirectoryRecord | back to summary |
|---|---|
| private void positionAtEndOfCentralDirectoryRecord() throws IOException Searches for the and positions the stream at the start of the "End of central dir record". | |
| readCentralDirectoryEntry | back to summary |
|---|---|
| private void readCentralDirectoryEntry(final Map<ZipEntry, ZipFile. Reads an individual entry of the central directory, creates an ZipEntry from it and adds it to the global maps.
| |
| resolveLocalFileHeaderData | back to summary |
|---|---|
| private void resolveLocalFileHeaderData(final Map<ZipEntry, ZipFile. 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. | |
| setSizesAndOffsetFromZip64Extra | back to summary |
|---|---|
| private void setSizesAndOffsetFromZip64Extra(final ZipEntry ze, final ZipFile. 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. | |
| skipBytes | back to summary |
|---|---|
| private void skipBytes(final int count) throws IOException Skips the given number of bytes or throws an EOFException if skipping failed. | |
| startsWithLocalFileHeader | back to summary |
|---|---|
| private boolean startsWithLocalFileHeader() throws IOException Checks whether the archive starts with a LFH. If it doesn't, it may be an empty archive. | |
| tryToLocateSignature | back to summary |
|---|---|
| private boolean tryToLocateSignature(final long minDistanceFromEnd, final long maxDistanceFromEnd, final byte[] sig) throws 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. | |
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private long | |
| private long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| public int | read()
Implements abstract java. |
| public int | read(final byte[]
the buffer into which the data is read. b, final int the start offset in array off, int b
at which the data is written.the maximum number of bytes to read. len)Overrides java. len bytes of data from the input stream into
an array of bytes.
|
| addDummyByte | back to summary |
|---|---|
| private boolean addDummyByte | |
| loc | back to summary |
|---|---|
| private long loc | |
| remaining | back to summary |
|---|---|
| private long remaining | |
| BoundedInputStream | back to summary |
|---|---|
| pack-priv BoundedInputStream(final long start, final long remaining) | |
| addDummy | back to summary |
|---|---|
| pack-priv void addDummy() Inflater needs an extra dummy byte for nowrap - see Inflater's javadocs. | |
| read | back to summary |
|---|---|
| public int read() throws IOException Implements abstract java. Doc from java. Reads the next byte of data from the input stream. The value byte is
returned as an
| |
| read | back to summary |
|---|---|
| public int read(final byte[] b, final int off, int len) throws IOException Overrides java. Doc from java. Reads up to This method blocks until input data is available, end of file is detected, or an exception is thrown. If The first byte read is stored into element In every case, elements
| |
| Modifier and Type | Field and Description |
|---|---|
| private final ZipFile. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | equals(final Object
the reference object with which to compare. other)Overrides org. |
| pack-priv ZipFile. | |
| public int |
| offsetEntry | back to summary |
|---|---|
| private final ZipFile. | |
| Entry | back to summary |
|---|---|
| pack-priv Entry(final ZipFile. | |
| equals | back to summary |
|---|---|
| public boolean equals(final Object other) Overrides org. Doc from org. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| |
| getOffsetEntry | back to summary |
|---|---|
| pack-priv ZipFile. | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides org. Doc from org. Get the hashCode of the entry. This uses the name as the hashcode.
| |
| Modifier and Type | Field and Description |
|---|---|
| private final byte[] | |
| private final byte[] |
| Access | Constructor and Description |
|---|---|
| private |
| comment | back to summary |
|---|---|
| private final byte[] comment | |
| name | back to summary |
|---|---|
| private final byte[] name | |
| NameAndComment | back to summary |
|---|---|
| private NameAndComment(final byte[] name, final byte[] comment) | |
| Modifier and Type | Field and Description |
|---|---|
| private long | |
| private long |
| Access | Constructor and Description |
|---|---|
| private |
| dataOffset | back to summary |
|---|---|
| private long dataOffset | |
| headerOffset | back to summary |
|---|---|
| private long headerOffset | |
| OffsetEntry | back to summary |
|---|---|
| private OffsetEntry() | |