Class ZipFailSafeReader

java.lang.Object
com.pnfsoftware.jeb.util.encoding.zip.fsr.ZipFailSafeReader
All Implemented Interfaces:
AutoCloseable

public class ZipFailSafeReader extends Object implements AutoCloseable
PNF Software's Zip Fail Safe Reader (PZFSR). This parser attempts to handle corner-cases that fail Oracle's and Apache Common's parsers.

Limitations:

  • limited to 2Gb archives at most
  • zip64 is not supported
  • uncommon flags are not supported
  • non-standard attributes are disregarded
  • the only compression scheme supported is DEFLATE
  • Constructor Details

    • ZipFailSafeReader

      public ZipFailSafeReader(File file) throws IOException
      Construct with all optional settings set to false.
      Parameters:
      file - input zip
      Throws:
      IOException
    • ZipFailSafeReader

      public ZipFailSafeReader(SeekableByteChannel channel) throws IOException
      Construct with all optional settings set to false.
      Parameters:
      channel - input zip
      Throws:
      IOException
    • ZipFailSafeReader

      public ZipFailSafeReader(File file, boolean treatUnsupportedCompressionAsDeflate, boolean ignoreEncryption, boolean recoveryMode, boolean doNotParseEntries) throws IOException
      Construct a zip parser.
      Parameters:
      file - input zip
      treatUnsupportedCompressionAsDeflate - optional
      ignoreEncryption - optional
      recoveryMode - optional; if true, the reader will attempt to recover zip entries of truncated files, including those with incomplete Central Directory
      doNotParseEntries - 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, via enumerateEntries() (this will result in faster opening of large archives)
      Throws:
      IOException
    • ZipFailSafeReader

      public ZipFailSafeReader(SeekableByteChannel channel, boolean treatUnsupportedCompressionAsDeflate, boolean ignoreEncryption, boolean recoveryMode, boolean doNotParseEntries) throws IOException
      Construct a zip parser.
      Parameters:
      channel - input
      treatUnsupportedCompressionAsDeflate -
      ignoreEncryption -
      recoveryMode - optional; if true, the reader will attempt to recover zip entries of truncated files, including those with incomplete Central Directory
      doNotParseEntries - 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, via enumerateEntries() (this will result in faster opening of large archives)
      Throws:
      IOException
  • Method Details

    • enumerateEntries

      public Iterable<ZipEntry> 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 with doNotParseEntries=true. Any IOException raised will be wrapped in a RuntimeException.
      Returns:
      the iterable that will iterate and parse the zip entries
    • close

      public void close() throws IOException
      Specified by:
      close in interface AutoCloseable
      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

      public List<ZipEntry> 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:
    • getEntry

      public ZipEntry getEntry(String filename)
      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 -
      Returns:
    • hasEntry

      public boolean hasEntry(String filename)
      Determine whether an entry exists. parsed yet.

      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 -
      Returns:
    • readData

      public ZipData readData(String filename) throws IOException
      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 -
      Returns:
      Throws:
      IOException
    • readData

      public ZipData readData(ZipEntry e) throws IOException
      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 -
      Returns:
      Throws:
      IOException
    • toString

      public String toString()
      Overrides:
      toString in class Object