java.util.zip.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..
It doesn't extend java. as it would have to reimplement all methods anyway. Like java., 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 65,536 entries.
The method signatures mimic the ones of java., with a couple of exceptions:
org.apache.commons.compress.archivers.zip.ZipArchiveEntry instances.| Modifier and Type | Class and Description |
|---|---|
| private static class | ZipFile.BoundedFileChannelInputStream
Lock-free implementation of BoundedInputStream. |
| public static class | ZipFile.Builder
Builds new |
| private static class | ZipFile.Entry
Extends ZipArchiveEntry to store the offset within the archive. |
| private static class | |
| private static class |
| Modifier and Type | Field and Description |
|---|---|
| private final SeekableByteChannel | archive
The actual data source. |
| pack-priv static final int | |
| private long | |
| private long | |
| private long | |
| private static final int | CFD_DISK_OFFSET
Offset of the field that holds the disk number 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 final int | CFD_LENGTH_OFFSET
Offset of the field that holds the location of the length of the central directory inside the "End of central directory record" relative to the start of the "End of central directory record". |
| private static final int | CFD_LOCATOR_RELATIVE_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 "number of the disk with the start of the central directory". |
| 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 final ByteBuffer | |
| private final byte[] | |
| private volatile boolean | closed
Whether the file is closed. |
| private final ByteBuffer | |
| private final byte[] | |
| private final Charset | encoding
The encoding to use for file names and the file comment. |
| private final List | entries
List of entries in the order they appear inside the central directory. |
| private long | |
| private static final int | |
| private final boolean | isSplitZipArchive
Whether the ZIP archive is a split ZIP archive |
| private static final long | LFH_OFFSET_FOR_FILENAME_LENGTH
Number of bytes in local file header up to the "length of file name" entry. |
| private static final int | MAX_EOCD_SIZE
Maximum length of the "End of central directory record" with a file comment. |
| pack-priv 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 ZipArchiveEntrys, name -> actual entries. |
| pack-priv static final int | |
| private static final Comparator | offsetComparator
Compares two ZipArchiveEntries based on their offset within the archive. |
| private static final byte[] | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final int | |
| private static final EnumSet | |
| private final ByteBuffer | |
| private final byte[] | |
| private final boolean | useUnicodeExtraFields
Whether to look for and use Unicode extra fields. |
| private final ByteBuffer | |
| private final byte[] | |
| private static final int | ZIP64_EOCD_CFD_DISK_OFFSET
Offset of the field that holds the disk number 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_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_EOCD_CFD_LOCATOR_RELATIVE_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 "number of the disk with the start of the central directory". |
| 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 file names and the file comment. |
| Access | Constructor and Description |
|---|---|
| public | ZipFile(final File
the archive. file)
Deprecated
Use
Opens the given file for reading, assuming "UTF8" for file names.
Builder#get().
|
| public | ZipFile(final File
the archive. file, final String the encoding to use for file names, use null for the platform's default encoding encoding)
Deprecated
Use
Opens the given file for reading, assuming the specified encoding for file names and scanning for Unicode extra fields.
Builder#get().
|
| public | ZipFile(final File
the archive. file, 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)
Deprecated
Use
Opens the given file for reading, assuming the specified encoding for file names.
Builder#get().
|
| public | ZipFile(final File
the archive. file, 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 to ignore information stored inside the local file header (see the notes in this method's Javadoc) ignoreLocalFileHeader)
Deprecated
Use
Opens the given file for reading, assuming the specified encoding for file names.
Builder#get().
|
| public | ZipFile(final Path
path to the archive. path)
Deprecated
Use
Opens the given path for reading, assuming "UTF-8" for file names.
Builder#get().
|
| public | ZipFile(final Path
path to the archive. path, final String the encoding to use for file names, use null for the platform's default encoding encoding)
Deprecated
Use
Opens the given path for reading, assuming the specified encoding for file names and scanning for Unicode extra fields.
Builder#get().
|
| public | ZipFile(final Path
path to the archive. path, 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)
Deprecated
Use
Opens the given path for reading, assuming the specified encoding for file names.
Builder#get().
|
| public | ZipFile(final Path
path to the archive. path, 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 to ignore information stored inside the local file header (see the notes in this method's Javadoc) ignoreLocalFileHeader)
Deprecated
Use
Opens the given path for reading, assuming the specified encoding for file names.
Builder#get().
|
| public | ZipFile(final SeekableByteChannel
the archive. channel)
Deprecated
Use
Opens the given channel for reading, assuming "UTF-8" for file names.
Builder#get().
|
| public | ZipFile(final SeekableByteChannel
the archive. channel, final String the encoding to use for file names, use null for the platform's default encoding encoding)
Deprecated
Use
Opens the given channel for reading, assuming the specified encoding for file names.
Builder#get().
|
| private | ZipFile(final SeekableByteChannel channel, final String channelDescription, final Charset encoding, final boolean useUnicodeExtraFields, final boolean closeOnError, final boolean ignoreLocalFileHeader)
|
| public | ZipFile(final SeekableByteChannel
the archive. channel, final String description of the archive, used for error messages only. channelDescription, 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)
Deprecated
Use
Opens the given channel for reading, assuming the specified encoding for file names.
Builder#get().
|
| public | ZipFile(final SeekableByteChannel
the archive. channel, final String description of the archive, used for error messages only. channelDescription, 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 to ignore information stored inside the local file header (see the notes in this method's Javadoc) ignoreLocalFileHeader)
Deprecated
Use
Opens the given channel for reading, assuming the specified encoding for file names.
Builder#get().
|
| private | ZipFile(final SeekableByteChannel channel, final String channelDescription, final String encoding, final boolean useUnicodeExtraFields, final boolean closeOnError, final boolean ignoreLocalFileHeader)
|
| public | ZipFile(final String
name of the archive. name)
Deprecated
Use
Opens the given file for reading, assuming "UTF-8".
Builder#get().
|
| public | ZipFile(final String
name of the archive. name, final String the encoding to use for file names, use null for the platform's default encoding encoding)
Deprecated
Use
Opens the given file for reading, assuming the specified encoding for file names, scanning unicode extra fields.
Builder#get().
|
| Modifier and Type | Method and Description |
|---|---|
| public static ZipFile. | |
| public boolean | Returns: whether this class is able to read the given entry.the entry entry)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)Closes a ZIP file quietly; throwing no IOException, does nothing on null input. |
| public void | copyRawEntries(final ZipArchiveOutputStream
The zipArchiveOutputStream to write the entries to target, final ZipArchiveEntryPredicate A predicate that selects which entries to write predicate)Transfer selected entries from this ZIP file to a given #ZipArchiveOutputStream. |
| private BoundedArchiveInputStream | createBoundedInputStream(final long start, final long remaining)
Creates new BoundedInputStream, according to implementation of underlying archive channel. |
| private void | |
| protected void | finalize()
Overrides java. |
| public InputStream | Returns: null if there is no content before the first local file header. Otherwise, returns a stream to read the content before the first local file header.Gets an InputStream for reading the content before the first local file header. |
| private long | |
| public String | Returns: null if using the platform's default character encoding.Gets the encoding to use for file names and the file comment. |
| public Enumeration | |
| public Iterable | Returns: the Iterable<ZipArchiveEntry> corresponding to the given namename of the entry. name)Gets all named entries in the same order they appear within the archive's central directory. |
| public Enumeration | Returns: all entries asZipArchiveEntry instancesGets all entries in physical order. |
| public Iterable | Returns: the Iterable<ZipArchiveEntry> corresponding to the given namename of the entry. name)Gets all named entries in the same order their contents appear within the archive. |
| public ZipArchiveEntry | |
| public long | Returns: the length of the content before the first local file headerGets the offset of the first local file header in the file. |
| public InputStream | Returns: a stream to read the entry from. The returned stream implementsInputStreamStatistics.the entry to get the stream for. entry)Gets an InputStream for reading the contents of the given entry. |
| public InputStream | Returns: The raw input stream containing (possibly) compressed data.The entry to get the stream for entry)Gets the raw stream of the archive entry (compressed form). |
| public String | Returns: entry's content as a StringZipArchiveEntry object that represents the symbolic link entry)Gets the entry's content as a String if isUnixSymlink() returns true for it, otherwise returns null. |
| private static SeekableByteChannel | Returns: a new seekable byte channelthe path to the file to open or create path)Creates a new SeekableByteChannel for reading. |
| private static SeekableByteChannel | |
| private Map | Returns: a map of zip entries 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 ZipArchiveEntry 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()
Parses the "End of central dir record" 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 static boolean | Returns: true if it's Zip64 end of central directory or false if it's Zip32Searches for the and positions the stream at the start of the "End of central dir record". |
| private void | readCentralDirectoryEntry(final Map<ZipArchiveEntry, 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 ZipArchiveEntry from it and adds it to the global maps. |
| private void | resolveLocalFileHeaderData(final Map<ZipArchiveEntry, ZipFile.
Walks through all recorded entries and adds the data available from the local file header. |
| private void | |
| private int[] | |
| private void | setSizesAndOffsetFromZip64Extra(final ZipArchiveEntry entry)
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 ZipArchiveEntry[] | Returns: the given entries, sorted.entries to sort allEntries)Sorts entries in place by offset. |
| private boolean | |
| private static boolean | tryToLocateSignature(final SeekableByteChannel channel, 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. |