Class ZipInputStreamZipEntrySource

java.lang.Object
org.apache.poi.openxml4j.util.ZipInputStreamZipEntrySource
All Implemented Interfaces:
Closeable, AutoCloseable, ZipEntrySource

public class ZipInputStreamZipEntrySource extends Object implements ZipEntrySource
Provides a way to get at all the ZipEntries from a ZipInputStream, as many times as required. Allows a ZipInputStream to be treated much like a ZipFile, for a price in terms of memory. Be sure to call close() as soon as you're done, to free up that memory!
  • Constructor Summary

    Constructors
    Constructor
    Description
    Reads all the entries from the ZipInputStream into memory, and don't close (since POI 4.0.1) the source stream.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Indicates we are done with reading, and resources may be freed
    Enumeration<? extends org.apache.commons.compress.archivers.zip.ZipArchiveEntry>
    Returns an Enumeration of all the Entries
    org.apache.commons.compress.archivers.zip.ZipArchiveEntry
    Return an entry by its path
    getInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveEntry zipEntry)
    Returns an InputStream of the decompressed data that makes up the entry
    static int
    Get the threshold at which a zip entry is regarded as too large for holding in memory and the data is put in a temp file instead (defaults to -1 meaning temp files are not used)
    boolean
    Has close been called already?
    static void
    setEncryptTempFiles(boolean encrypt)
    Encrypt temp files when they are used.
    static void
    setThresholdBytesForTempFiles(int thresholdBytes)
    Set the threshold at which a zip entry is regarded as too large for holding in memory and the data is put in a temp file instead
    static boolean
    Whether temp files should be encrypted (default false).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ZipInputStreamZipEntrySource

      public ZipInputStreamZipEntrySource(ZipArchiveThresholdInputStream inp) throws IOException
      Reads all the entries from the ZipInputStream into memory, and don't close (since POI 4.0.1) the source stream. We'll then eat lots of memory, but be able to work with the entries at-will.
      Throws:
      IOException - if an error occurs while reading the zip entries
      InvalidZipException - if the input file contains an entry with an empty name or more than 1 entry with the same name
      See Also:
  • Method Details

    • setThresholdBytesForTempFiles

      public static void setThresholdBytesForTempFiles(int thresholdBytes)
      Set the threshold at which a zip entry is regarded as too large for holding in memory and the data is put in a temp file instead
      Parameters:
      thresholdBytes - number of bytes at which a zip entry is regarded as too large for holding in memory and the data is put in a temp file instead - defaults to -1 meaning temp files are not used and that zip entries with more than 2GB of data after decompressing will fail, 0 means all zip entries are stored in temp files. A threshold like 50000000 (approx 50Mb is recommended)
      Since:
      5.1.0
      See Also:
    • getThresholdBytesForTempFiles

      public static int getThresholdBytesForTempFiles()
      Get the threshold at which a zip entry is regarded as too large for holding in memory and the data is put in a temp file instead (defaults to -1 meaning temp files are not used)
      Returns:
      threshold in bytes
      Since:
      5.1.0
    • setEncryptTempFiles

      public static void setEncryptTempFiles(boolean encrypt)
      Encrypt temp files when they are used. Only affects temp files related to zip entries.
      Parameters:
      encrypt - whether temp files should be encrypted
      Since:
      5.1.0
      See Also:
    • shouldEncryptTempFiles

      public static boolean shouldEncryptTempFiles()
      Whether temp files should be encrypted (default false). Only affects temp files related to zip entries.
      Since:
      5.1.0
    • getEntries

      public Enumeration<? extends org.apache.commons.compress.archivers.zip.ZipArchiveEntry> getEntries()
      Description copied from interface: ZipEntrySource
      Returns an Enumeration of all the Entries
      Specified by:
      getEntries in interface ZipEntrySource
    • getInputStream

      public InputStream getInputStream(org.apache.commons.compress.archivers.zip.ZipArchiveEntry zipEntry) throws IOException
      Description copied from interface: ZipEntrySource
      Returns an InputStream of the decompressed data that makes up the entry
      Specified by:
      getInputStream in interface ZipEntrySource
      Throws:
      IOException
    • close

      public void close() throws IOException
      Description copied from interface: ZipEntrySource
      Indicates we are done with reading, and resources may be freed
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
      Specified by:
      close in interface ZipEntrySource
      Throws:
      IOException
    • isClosed

      public boolean isClosed()
      Description copied from interface: ZipEntrySource
      Has close been called already?
      Specified by:
      isClosed in interface ZipEntrySource
    • getEntry

      public org.apache.commons.compress.archivers.zip.ZipArchiveEntry getEntry(String path)
      Description copied from interface: ZipEntrySource
      Return an entry by its path
      Specified by:
      getEntry in interface ZipEntrySource
      Parameters:
      path - the path in unix-notation
      Returns:
      the entry or null if not found