Top Description Inners Fields Constructors Methods
org.apache.commons.compress.archivers.zip

public Class ZipFile

extends Object
implements Closeable
Class Inheritance
All Implemented Interfaces
java.io.Closeable, java.lang.AutoCloseable
Imports
java.io.BufferedInputStream, .ByteArrayInputStream, .Closeable, .EOFException, .File, .IOException, .InputStream, .SequenceInputStream, java.nio.ByteBuffer, .ByteOrder, java.nio.channels.FileChannel, .SeekableByteChannel, java.nio.charset.Charset, .StandardCharsets, java.nio.file.Files, .OpenOption, .Path, .StandardOpenOption, java.util.ArrayList, .Arrays, .Collections, .Comparator, .EnumSet, .Enumeration, .HashMap, .LinkedList, .List, .Map, java.util.stream.Collectors, .IntStream, java.util.zip.Inflater, .ZipException, org.apache.commons.compress.archivers.EntryStreamOffsets, org.apache.commons.compress.compressors.bzip2.BZip2CompressorInputStream, org.apache.commons.compress.compressors.deflate64.Deflate64CompressorInputStream, org.apache.commons.compress.utils.BoundedArchiveInputStream, .BoundedSeekableByteChannelInputStream, .CharsetNames, .IOUtils, .InputStreamStatistics, .SeekableInMemoryByteChannel, org.apache.commons.io.Charsets, .FilenameUtils, org.apache.commons.io.build.AbstractOrigin.ByteArrayOrigin, .AbstractStreamBuilder, org.apache.commons.io.input.CountingInputStream

Replacement for 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.util.zip.ZipEntry.

It doesn't extend java.util.zip.ZipFile as it would have to reimplement all methods anyway. Like java.util.zip.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 65,536 entries.

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
ZipFile.BoundedFileChannelInputStream

Lock-free implementation of BoundedInputStream.

public static class
ZipFile.Builder

Builds new ZipFile instances.

private static class
ZipFile.Entry

Extends ZipArchiveEntry to store the offset within the archive.

private static class
private static class

Field Summary

Modifier and TypeField 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<ZipArchiveEntry>
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<String, LinkedList<ZipArchiveEntry>>
nameMap

Maps String to list of ZipArchiveEntrys, name -> actual entries.

pack-priv static final int
private static final Comparator<ZipArchiveEntry>
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<StandardOpenOption>
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.

Constructor Summary

AccessConstructor and Description
public
ZipFile(final File
the archive.
file
)

Deprecated Use Builder#get().
Opens the given file for reading, assuming "UTF8" for file names.
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 Builder#get().
Opens the given file for reading, assuming the specified encoding for file names and scanning for Unicode extra fields.
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 Builder#get().
Opens the given file for reading, assuming the specified encoding for file names.
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 Builder#get().
Opens the given file for reading, assuming the specified encoding for file names.
public
ZipFile(final Path
path to the archive.
path
)

Deprecated Use Builder#get().
Opens the given path for reading, assuming "UTF-8" for file names.
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 Builder#get().
Opens the given path for reading, assuming the specified encoding for file names and scanning for Unicode extra fields.
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 Builder#get().
Opens the given path for reading, assuming the specified encoding for file names.
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 Builder#get().
Opens the given path for reading, assuming the specified encoding for file names.
public
ZipFile(final SeekableByteChannel
the archive.
channel
)

Deprecated Use Builder#get().
Opens the given channel for reading, assuming "UTF-8" for file names.
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 Builder#get().
Opens the given channel for reading, assuming the specified encoding for file names.
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 Builder#get().
Opens the given channel for reading, assuming the specified encoding for file names.
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 Builder#get().
Opens the given channel for reading, assuming the specified encoding for file names.
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 Builder#get().
Opens the given file for reading, assuming "UTF-8".
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 Builder#get().
Opens the given file for reading, assuming the specified encoding for file names, scanning unicode extra fields.

Method Summary

Modifier and TypeMethod and Description
public static ZipFile.Builder

Returns:

a new Builder.
builder
()

Creates a new Builder.

public boolean

Returns:

whether this class is able to read the given entry.
canReadEntryData
(final ZipArchiveEntry
the entry
entry
)

Whether this class is able to read the given entry.

public void
close()

Implements java.io.Closeable.close.

Closes the archive.
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.lang.Object.finalize.

Ensures that the close method of this ZIP file is called when there are no more references to it.
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.
getContentBeforeFirstLocalFileHeader
()

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.
getEncoding
()

Gets the encoding to use for file names and the file comment.

public Enumeration<ZipArchiveEntry>

Returns:

all entries as ZipArchiveEntry instances
getEntries
()

Gets all entries.

public Iterable<ZipArchiveEntry>

Returns:

the Iterable<ZipArchiveEntry> corresponding to the given name
getEntries
(final String
name of the entry.
name
)

Gets all named entries in the same order they appear within the archive's central directory.

public Enumeration<ZipArchiveEntry>

Returns:

all entries as ZipArchiveEntry instances
getEntriesInPhysicalOrder
()

Gets all entries in physical order.

public Iterable<ZipArchiveEntry>

Returns:

the Iterable<ZipArchiveEntry> corresponding to the given name
getEntriesInPhysicalOrder
(final String
name of the entry.
name
)

Gets all named entries in the same order their contents appear within the archive.

public ZipArchiveEntry

Returns:

the ZipArchiveEntry corresponding to the given name - or null if not present.
getEntry
(final String
name of the entry.
name
)

Gets a named entry or null if no entry by that name exists.

public long

Returns:

the length of the content before the first local file header
getFirstLocalFileHeaderOffset
()

Gets the offset of the first local file header in the file.

public InputStream

Returns:

a stream to read the entry from. The returned stream implements InputStreamStatistics.
getInputStream
(final ZipArchiveEntry
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.
getRawInputStream
(final ZipArchiveEntry
The entry to get the stream for
entry
)

Gets the raw stream of the archive entry (compressed form).

private static SeekableByteChannel

Returns:

a new seekable byte channel
newReadByteChannel
(final Path
the path to the file to open or create
path
)

Creates a new SeekableByteChannel for reading.

private static SeekableByteChannel
openZipChannel(final Path path, final long maxNumberOfDisks, final OpenOption[] openOptions)

private Map<ZipArchiveEntry, ZipFile.NameAndComment>

Returns:

a map of zip entries that didn't have the language encoding flag set when read.
populateFromCentralDirectory
()

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 Zip32
positionAtEndOfCentralDirectoryRecord
(final SeekableByteChannel channel)

Searches for the and positions the stream at the start of the "End of central dir record".

private void
readCentralDirectoryEntry(final Map<ZipArchiveEntry, ZipFile.NameAndComment>
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.
noUTF8Flag
)

Reads 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.NameAndComment> entriesWithoutUTF8Flag)

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.
sortByOffset
(final ZipArchiveEntry[]
entries to sort
allEntries
)

Sorts entries in place by offset.

private boolean
startsWithLocalFileHeader()

Checks whether the archive starts with an LFH.

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.

Inherited from java.lang.Object:
cloneequalsgetClasshashCodenotifynotifyAlltoStringwaitwaitwait