Class ZipFileReader

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ZipFileReader
    extends java.lang.Object
    implements java.io.Closeable
    Allows reading entries from a zip file.
    • Constructor Summary

      Constructors 
      Constructor Description
      ZipFileReader​(java.lang.String archivePath)
      Creates an instance for zip file reading.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()  
      java.util.Set<java.lang.String> getFileNames()
      Get all file entries paths inside the reading zip file.
      java.io.InputStream readFromZip​(java.lang.String fileName)
      Read single file from zip.
      void setThresholdEntries​(int thresholdEntries)
      Sets the maximum number of file entries in the archive to prevent a Zip Bomb Attack.
      void setThresholdRatio​(double thresholdRatio)
      Sets the maximum ratio between compressed and uncompressed data to prevent a Zip Bomb Attack.
      void setThresholdSize​(int thresholdSize)
      Sets the maximum total uncompressed data size to prevent a Zip Bomb Attack.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • LOGGER

        private static final org.slf4j.Logger LOGGER
      • zipFile

        private final java.util.zip.ZipFile zipFile
      • thresholdSize

        private int thresholdSize
      • thresholdEntries

        private int thresholdEntries
      • thresholdRatio

        private double thresholdRatio
    • Constructor Detail

      • ZipFileReader

        public ZipFileReader​(java.lang.String archivePath)
                      throws java.io.IOException
        Creates an instance for zip file reading.
        Parameters:
        archivePath - the path to the zip file to read
        Throws:
        java.io.IOException - if some I/O exception occurs
    • Method Detail

      • getFileNames

        public java.util.Set<java.lang.String> getFileNames()
                                                     throws java.io.IOException
        Get all file entries paths inside the reading zip file.
        Returns:
        the Set of all file entries paths
        Throws:
        java.io.IOException - if some I/O exception occurs
      • readFromZip

        public java.io.InputStream readFromZip​(java.lang.String fileName)
                                        throws java.io.IOException
        Read single file from zip.
        Parameters:
        fileName - the file path inside zip to read
        Returns:
        the InputStream represents read file content
        Throws:
        java.io.IOException - if some I/O exception occurs
      • setThresholdSize

        public void setThresholdSize​(int thresholdSize)
        Sets the maximum total uncompressed data size to prevent a Zip Bomb Attack. Default value is 1 GB (1000000000).
        Parameters:
        thresholdSize - the threshold for maximum total size of the uncompressed data
      • setThresholdEntries

        public void setThresholdEntries​(int thresholdEntries)
        Sets the maximum number of file entries in the archive to prevent a Zip Bomb Attack. Default value is 10000.
        Parameters:
        thresholdEntries - maximum number of file entries in the archive
      • setThresholdRatio

        public void setThresholdRatio​(double thresholdRatio)
        Sets the maximum ratio between compressed and uncompressed data to prevent a Zip Bomb Attack. In general the data compression ratio for most of the legit archives is 1 to 3. Default value is 10.
        Parameters:
        thresholdRatio - maximum ratio between compressed and uncompressed data
      • close

        public void close()
                   throws java.io.IOException
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Throws:
        java.io.IOException