Class ZipFailSafeReader
java.lang.Object
com.pnfsoftware.jeb.util.encoding.zip.fsr.ZipFailSafeReader
- All Implemented Interfaces:
AutoCloseable
PNF Software's Zip Fail Safe Reader (PZFSR). This parser attempts to handle corner-cases that
fail Oracle's and Apache Commons parsers.
Limitations:
- limited to 2Gb archives at most (and the size of an uncompressed entry may not exceed
ENTRY_MAXSIZEbytes) - zip64 is not supported
- uncommon flags are not supported
- non-standard attributes are disregarded
- the only compression scheme supported is DEFLATE
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intZip compression method identifier for deflated entries.static final intMaximum supported uncompressed size for a single entry.static final intZip compression method identifier for stored entries. -
Constructor Summary
ConstructorsConstructorDescriptionZipFailSafeReader(File file) Construct with all optional settings set to false.ZipFailSafeReader(File file, int treatUnsupportedCompressionAsMethod, boolean ignoreEncryption, boolean recoveryMode, boolean doNotParseEntries) Construct a zip parser.ZipFailSafeReader(SeekableByteChannel channel) Construct with all optional settings set to false.ZipFailSafeReader(SeekableByteChannel channel, int treatUnsupportedCompressionAsMethod, boolean ignoreEncryption, boolean recoveryMode, boolean doNotParseEntries) Construct a zip parser. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Get the iterable to parse and enumerate over the zip entries.Retrieve a read-only list of the current entries.Retrieve an entry by name.intintintbooleanbooleanDetermine whether an entry exists.booleanbooleanisClosed()booleanRead an entry's data and provide the decompressed bytes.Read an entry's data and provide the decompressed bytes.toString()
-
Field Details
-
STORED
public static final int STOREDZip compression method identifier for stored entries.- See Also:
-
DEFLATED
public static final int DEFLATEDZip compression method identifier for deflated entries.- See Also:
-
ENTRY_MAXSIZE
public static final int ENTRY_MAXSIZEMaximum supported uncompressed size for a single entry.- See Also:
-
-
Constructor Details
-
ZipFailSafeReader
Construct with all optional settings set to false.- Parameters:
file- input file- Throws:
IOException- if the archive cannot be opened or parsed
-
ZipFailSafeReader
Construct with all optional settings set to false.- Parameters:
channel- input (closed when the client invokesclose())- Throws:
IOException- if the archive cannot be parsed
-
ZipFailSafeReader
public ZipFailSafeReader(File file, int treatUnsupportedCompressionAsMethod, boolean ignoreEncryption, boolean recoveryMode, boolean doNotParseEntries) throws IOException Construct a zip parser.- Parameters:
file- input filetreatUnsupportedCompressionAsMethod- optional (use -1 to fail when an unsupported compression method is encountered); else, a compression method idignoreEncryption- true to read encrypted entries as if they were not encryptedrecoveryMode- optional; if true, the reader will attempt to recover zip entries of truncated files, including those with incomplete Central DirectorydoNotParseEntries- optional; if true, the zip entries will not be pre-emptively parsed, and it will be up to the user to trigger parsing through enumeration, viaenumerateEntries()(this will result in faster opening of large archives)- Throws:
IOException- if the archive cannot be opened or parsed
-
ZipFailSafeReader
public ZipFailSafeReader(SeekableByteChannel channel, int treatUnsupportedCompressionAsMethod, boolean ignoreEncryption, boolean recoveryMode, boolean doNotParseEntries) throws IOException Construct a zip parser.- Parameters:
channel- input (closed when the client invokesclose())treatUnsupportedCompressionAsMethod- optional (use -1 to fail when an unsupported compression method is encountered); else, a compression method idignoreEncryption- true to read encrypted entries as if they were not encryptedrecoveryMode- optional; if true, the reader will attempt to recover zip entries of truncated files, including those with incomplete Central DirectorydoNotParseEntries- optional; if true, the zip entries will not be pre-emptively parsed, and it will be up to the user to trigger parsing through enumeration, viaenumerateEntries()(this will result in faster opening of large archives)- Throws:
IOException- if the archive cannot be parsed
-
-
Method Details
-
enumerateEntries
Get the iterable to parse and enumerate over the zip entries. This method should be called once, and once only, if the object was constructed withdoNotParseEntries=true. AnyIOExceptionraised will be wrapped in aRuntimeException.- Returns:
- the iterable that will iterate and parse the zip entries
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
IOException
-
isClosed
public boolean isClosed() -
getOffsetFirstEntry
public int getOffsetFirstEntry() -
hasPrependedData
public boolean hasPrependedData() -
getOffsetAppendedData
public int getOffsetAppendedData() -
hasAppendedData
public boolean hasAppendedData() -
isTruncated
public boolean isTruncated() -
getNumberOfEntries
public int getNumberOfEntries() -
getEntries
Retrieve a read-only list of the current entries.This method may be called even if not all entries were enumerated (incomplete parsing). if so, it will return an incomplete list.
- Returns:
- read-only view of the parsed entries
-
getEntry
Retrieve an entry by name.This method may be called even if not all entries were enumerated (incomplete parsing). It will fail if the entry has not been parsed yet.
- Parameters:
filename- entry file name- Returns:
- matching entry, or null
-
hasEntry
Determine whether an entry exists.This method may be called even if not all entries were enumerated (incomplete parsing). It will fail if the entry has not been parsed yet.
- Parameters:
filename- entry file name- Returns:
- true if the entry has already been parsed
-
readData
Read an entry's data and provide the decompressed bytes.This method may be called even if not all entries were enumerated (incomplete parsing). It will fail if the entry has not been parsed yet.
- Parameters:
filename- entry file name- Returns:
- decompressed entry data and optional read exception
- Throws:
IOException- if the entry does not exist or cannot be read
-
readData
Read an entry's data and provide the decompressed bytes.This method may be called even if not all entries were enumerated (incomplete parsing). It will fail if the entry has not been parsed yet.
- Parameters:
e- entry descriptor- Returns:
- decompressed entry data and optional read exception
- Throws:
IOException- if the entry cannot be read
-
toString
-