-
- All Implemented Interfaces:
-
java.io.Closeable,java.lang.AutoCloseable
public class ZipFile implements Closeable
Replacement for
{@code 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
{@code org.readium.r2.shared.util.archive.remote.zip.ZipArchiveEntry}instead of{@code java.util.zip.ZipEntry}.It doesn't extend
{@code java.util.zip.ZipFile}as it would have to reimplement all methods anyway. Like{@code 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
{@code java.util.zip.ZipFile}, with a couple of exceptions:- There is no getName method.
- entries has been renamed to getEntries.
- getEntries and getEntry return
{@code org.readium.r2.shared.util.archive.remote.zip.ZipArchiveEntry}instances. - close is allowed to throw IOException.
-
-
Field Summary
Fields Modifier and Type Field Description private final List<ZipArchiveEntry>entriesprivate final Stringencodingprivate longfirstLocalFileHeaderOffset
-
Constructor Summary
Constructors Constructor Description ZipFile(SeekableByteChannel channel)Opens the given channel for reading, assuming "UTF8" for file names. ZipFile(SeekableByteChannel channel, String encoding)Opens the given channel for reading, assuming the specifiedencoding for file names. ZipFile(SeekableByteChannel channel, boolean ignoreLocalFileHeader)ZipFile(SeekableByteChannel channel, String archiveName, String encoding, boolean useUnicodeExtraFields)Opens the given channel for reading, assuming the specifiedencoding for file names. ZipFile(SeekableByteChannel channel, String archiveName, String encoding, boolean useUnicodeExtraFields, boolean ignoreLocalFileHeader)Opens the given channel for reading, assuming the specifiedencoding for file names.
-
Method Summary
Modifier and Type Method Description Iterable<ZipArchiveEntry>getEntries(String name)Gets all named entries in the same order they appear withinthe archive's central directory. StringgetEncoding()Gets the encoding to use for file names and the file comment. longgetFirstLocalFileHeaderOffset()Gets the offset of the first local file header in the file. static voidcloseQuietly(ZipFile zipfile)close a zipfile quietly; throw no io fault, do nothingon a null parameter booleancanReadEntryData(ZipArchiveEntry ze)Whether this class is able to read the given entry. voidclose()Closes the archive. voidcopyRawEntries(ZipArchiveOutputStream target, ZipArchiveEntryPredicate predicate)Transfer selected entries from this zipfile to a given #ZipArchiveOutputStream.Compression and all other attributes will be as in this file. InputStreamgetContentBeforeFirstLocalFileHeader()Gets an InputStream for reading the content before the first local file header. Enumeration<ZipArchiveEntry>getEntriesInPhysicalOrder()Gets all entries in physical order. Iterable<ZipArchiveEntry>getEntriesInPhysicalOrder(String name)Gets all named entries in the same order their contentsappear within the archive. ZipArchiveEntrygetEntry(String name)Gets a named entry or {@code null}if no entry bythat name exists.InputStreamgetInputStream(ZipArchiveEntry ze)Gets an InputStream for reading the contents of the given entry. InputStreamgetRawInputStream(ZipArchiveEntry ze)Gets the raw stream of the archive entry (compressed form). InputStreamgetRawInputStream(ZipArchiveEntry ze, long fromIndex)Gets the raw stream of the stored archive entry starting from fromIndex. StringgetUnixSymlink(ZipArchiveEntry entry)Gets the entry's content as a String if isUnixSymlink()returns true for it, otherwise returns null. -
-
Constructor Detail
-
ZipFile
ZipFile(SeekableByteChannel channel)
Opens the given channel for reading, assuming "UTF8" for file names.- Parameters:
channel- the archive.
-
ZipFile
ZipFile(SeekableByteChannel channel, String encoding)
Opens the given channel for reading, assuming the specifiedencoding for file names.- Parameters:
channel- the archive.encoding- the encoding to use for file names, use nullfor the platform's default encoding
-
ZipFile
ZipFile(SeekableByteChannel channel, boolean ignoreLocalFileHeader)
-
ZipFile
ZipFile(SeekableByteChannel channel, String archiveName, String encoding, boolean useUnicodeExtraFields)
Opens the given channel for reading, assuming the specifiedencoding for file names.- Parameters:
channel- the archive.archiveName- name of the archive, used for error messages only.encoding- the encoding to use for file names, use nullfor the platform's default encodinguseUnicodeExtraFields- whether to use InfoZIP UnicodeExtra Fields (if present) to set the file names.
-
ZipFile
ZipFile(SeekableByteChannel channel, String archiveName, String encoding, boolean useUnicodeExtraFields, boolean ignoreLocalFileHeader)
Opens the given channel for reading, assuming the specifiedencoding for file names.- Parameters:
channel- the archive.archiveName- name of the archive, used for error messages only.encoding- the encoding to use for file names, use nullfor the platform's default encodinguseUnicodeExtraFields- whether to use InfoZIP UnicodeExtra Fields (if present) to set the file names.ignoreLocalFileHeader- whether to ignore informationstored inside the local file header (see the notes in this method's javadoc)
-
-
Method Detail
-
getEntries
Iterable<ZipArchiveEntry> getEntries(String name)
Gets all named entries in the same order they appear withinthe archive's central directory.
- Parameters:
name- name of the entry.
-
getEncoding
String getEncoding()
Gets the encoding to use for file names and the file comment.
-
getFirstLocalFileHeaderOffset
long getFirstLocalFileHeaderOffset()
Gets the offset of the first local file header in the file.
-
closeQuietly
static void closeQuietly(ZipFile zipfile)
close a zipfile quietly; throw no io fault, do nothingon a null parameter
- Parameters:
zipfile- file to close, can be null
-
canReadEntryData
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 acompression method that hasn't been implemented yet.
- Parameters:
ze- the entry
-
close
void close()
Closes the archive.
-
copyRawEntries
void copyRawEntries(ZipArchiveOutputStream target, ZipArchiveEntryPredicate predicate)
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 topredicate- A predicate that selects which entries to write
-
getContentBeforeFirstLocalFileHeader
InputStream getContentBeforeFirstLocalFileHeader()
Gets an InputStream for reading the content before the first local file header.
-
getEntriesInPhysicalOrder
Enumeration<ZipArchiveEntry> getEntriesInPhysicalOrder()
Gets all entries in physical order.
Entries will be returned in the same order their contentsappear within the archive.
-
getEntriesInPhysicalOrder
Iterable<ZipArchiveEntry> getEntriesInPhysicalOrder(String name)
Gets all named entries in the same order their contentsappear within the archive.
- Parameters:
name- name of the entry.
-
getEntry
ZipArchiveEntry getEntry(String name)
Gets a named entry or
{@code null}if no entry bythat name exists.If multiple entries with the same name exist the first entryin the archive's central directory by that name isreturned.
- Parameters:
name- name of the entry.
-
getInputStream
InputStream getInputStream(ZipArchiveEntry ze)
Gets an InputStream for reading the contents of the given entry.
- Parameters:
ze- the entry to get the stream for.
-
getRawInputStream
InputStream getRawInputStream(ZipArchiveEntry ze)
Gets the raw stream of the archive entry (compressed form).
This method does not relate to how/if we understand the payload in thestream, since we really only intend to move it on to somewhere else.
Since version 1.22, this method will make an attempt to read the entry's datastream offset, even if the
{@code ignoreLocalFileHeader}parameter was{@code true}in the constructor. An IOException can also be thrown from the body of the methodif this lookup fails for some reason.- Parameters:
ze- The entry to get the stream for
-
getRawInputStream
InputStream getRawInputStream(ZipArchiveEntry ze, long fromIndex)
Gets the raw stream of the stored archive entry starting from fromIndex.This method does not relate to how/if we understand the payload in thestream, since we really only intend to move it on to somewhere else.
- Parameters:
ze- The stored entry to get the stream forfromIndex- The index in the entry that the stream will start from
-
getUnixSymlink
String getUnixSymlink(ZipArchiveEntry entry)
Gets 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 thesame encoding that as been specified for this ZipFile.
- Parameters:
entry- ZipArchiveEntry object that represents the symbolic link
-
-
-
-