Class ZipUtil

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ZipUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static void addEntries​(java.io.File zip, ZipEntrySource[] entries)
      Changes a zip file it with with new entries.
      static void addEntries​(java.io.File zip, ZipEntrySource[] entries, java.io.File destZip)
      Copies an existing ZIP file and appends it with new entries.
      static void addEntries​(java.io.File zip, ZipEntrySource[] entries, java.io.OutputStream destOut)
      Copies an existing ZIP file and appends it with new entries.
      static void addEntries​(java.io.InputStream is, ZipEntrySource[] entries, java.io.OutputStream destOut)
      Copies an existing ZIP file and appends it with new entries.
      static void addEntry​(java.io.File zip, java.lang.String path, byte[] bytes)
      Changes a zip file, adds one new entry in-place.
      static void addEntry​(java.io.File zip, java.lang.String path, byte[] bytes, int compressionMethod)
      Changes a zip file, adds one new entry in-place.
      static void addEntry​(java.io.File zip, java.lang.String path, byte[] bytes, java.io.File destZip)
      Copies an existing ZIP file and appends it with one new entry.
      static void addEntry​(java.io.File zip, java.lang.String path, byte[] bytes, java.io.File destZip, int compressionMethod)
      Copies an existing ZIP file and appends it with one new entry.
      static void addEntry​(java.io.File zip, java.lang.String path, java.io.File file)
      Changes a zip file, adds one new entry in-place.
      static void addEntry​(java.io.File zip, java.lang.String path, java.io.File file, java.io.File destZip)
      Copies an existing ZIP file and appends it with one new entry.
      static void addEntry​(java.io.File zip, ZipEntrySource entry)
      Changes a zip file, adds one new entry in-place.
      static void addEntry​(java.io.File zip, ZipEntrySource entry, java.io.File destZip)
      Copies an existing ZIP file and appends it with one new entry.
      private static void addEntry​(ZipEntrySource entry, java.util.zip.ZipOutputStream out)
      Adds a given ZIP entry to a ZIP file.
      static void addOrReplaceEntries​(java.io.File zip, ZipEntrySource[] entries)
      Changes a ZIP file: adds/replaces the given entries in it.
      static void addOrReplaceEntries​(java.io.File zip, ZipEntrySource[] entries, java.io.File destZip)
      Copies an existing ZIP file and adds/replaces the given entries in it.
      static boolean archiveEquals​(java.io.File f1, java.io.File f2)
      Compares two ZIP files and returns true if they contain same entries.
      private static boolean archiveEqualsInternal​(java.io.File f1, java.io.File f2)  
      private static java.io.File checkDestinationFileForTraversal​(java.io.File outputDir, java.lang.String name, java.io.File destFile)  
      static void closeQuietly​(java.util.zip.ZipFile zf)
      Closes the ZIP file while ignoring any errors.
      static boolean containsAnyEntry​(java.io.File zip, java.lang.String[] names)
      Checks if the ZIP file contains any of the given entries.
      static boolean containsEntry​(java.io.File zip, java.lang.String name)
      Checks if the ZIP file contains the given entry.
      private static void copyEntries​(java.io.File zip, java.util.zip.ZipOutputStream out)
      Copies all entries from one ZIP file to another.
      private static void copyEntries​(java.io.File zip, java.util.zip.ZipOutputStream out, java.util.Set<java.lang.String> ignoredEntries)
      Copies all entries from one ZIP file to another, ignoring entries with path in ignoredEntries
      private static void copyEntries​(java.io.InputStream is, java.util.zip.ZipOutputStream out)
      Copies all entries from one ZIP stream to another.
      static void createEmpty​(java.io.File file)
      Creates an empty ZIP archive at the location of the provided file.
      private static boolean doEntryEquals​(java.util.zip.ZipFile zf1, java.util.zip.ZipFile zf2, java.lang.String path1, java.lang.String path2)
      Compares two ZIP entries (byte-by-byte).
      private static byte[] doUnpackEntry​(java.util.zip.ZipFile zf, java.lang.String name)
      Unpacks a single entry from a ZIP file.
      private static boolean doUnpackEntry​(java.util.zip.ZipFile zf, java.lang.String name, java.io.File file)
      Unpacks a single file from a ZIP archive to a file.
      (package private) static java.util.Map<java.lang.String,​ZipEntrySource> entriesByPath​(ZipEntrySource... entries)  
      static boolean entryEquals​(java.io.File f1, java.io.File f2, java.lang.String path)
      Compares same entry in two ZIP files (byte-by-byte).
      static boolean entryEquals​(java.io.File f1, java.io.File f2, java.lang.String path1, java.lang.String path2)
      Compares two ZIP entries (byte-by-byte).
      static boolean entryEquals​(java.util.zip.ZipFile zf1, java.util.zip.ZipFile zf2, java.lang.String path1, java.lang.String path2)
      Compares two ZIP entries (byte-by-byte).
      static void explode​(java.io.File zip)
      Unpacks a ZIP file to its own location.
      (package private) static java.util.Set<java.lang.String> filterDirEntries​(java.io.File zip, java.util.Collection<java.lang.String> names)  
      static int getCompressionLevelOfEntry​(java.io.File zip, java.lang.String name)
      Deprecated.
      The compression level cannot be retrieved.
      static int getCompressionMethodOfEntry​(java.io.File zip, java.lang.String name)
      Returns the compression method of a given entry of the ZIP file.
      static boolean handle​(java.io.File zip, java.lang.String name, ZipEntryCallback action)
      Reads the given ZIP file and executes the given action for a single entry.
      static boolean handle​(java.io.InputStream is, java.lang.String name, ZipEntryCallback action)
      Reads the given ZIP stream and executes the given action for a single entry.
      static void iterate​(java.io.File zip, java.lang.String[] entryNames, ZipEntryCallback action)
      Reads the given ZIP file and executes the given action for each given entry.
      static void iterate​(java.io.File zip, java.lang.String[] entryNames, ZipEntryCallback action, java.nio.charset.Charset charset)
      Reads the given ZIP file and executes the given action for each given entry.
      static void iterate​(java.io.File zip, java.lang.String[] entryNames, ZipInfoCallback action)
      Scans the given ZIP file and executes the given action for each given entry.
      static void iterate​(java.io.File zip, ZipEntryCallback action)
      Reads the given ZIP file and executes the given action for each entry.
      static void iterate​(java.io.File zip, ZipEntryCallback action, java.nio.charset.Charset charset)
      Reads the given ZIP file and executes the given action for each entry.
      static void iterate​(java.io.File zip, ZipInfoCallback action)
      Scans the given ZIP file and executes the given action for each entry.
      static void iterate​(java.io.InputStream is, java.lang.String[] entryNames, ZipEntryCallback action)
      static void iterate​(java.io.InputStream is, java.lang.String[] entryNames, ZipEntryCallback action, java.nio.charset.Charset charset)
      Reads the given ZIP stream and executes the given action for each given entry.
      static void iterate​(java.io.InputStream is, ZipEntryCallback action)
      static void iterate​(java.io.InputStream is, ZipEntryCallback action, java.nio.charset.Charset charset)
      Reads the given ZIP stream and executes the given action for each entry.
      private static java.io.File makeDestinationFile​(java.io.File outputDir, java.lang.String name)  
      private static boolean metaDataEquals​(java.lang.String path, java.util.zip.ZipEntry e1, java.util.zip.ZipEntry e2)
      Compares meta-data of two ZIP entries.
      private static java.util.zip.ZipInputStream newCloseShieldZipInputStream​(java.io.InputStream is, java.nio.charset.Charset charset)
      Creates a new ZipInputStream based on the given InputStream.
      private static boolean operateInPlace​(java.io.File src, ZipUtil.InPlaceAction action)
      This method provides a general infrastructure for in-place operations.
      static void pack​(java.io.File rootDir, java.io.File zip)
      Compresses the given directory and all its sub-directories into a ZIP file.
      static void pack​(java.io.File sourceDir, java.io.File targetZipFile, boolean preserveRoot)
      Compresses the given directory and all its sub-directories into a ZIP file.
      static void pack​(java.io.File rootDir, java.io.File zip, int compressionLevel)
      Compresses the given directory and all its sub-directories into a ZIP file.
      static void pack​(java.io.File sourceDir, java.io.File targetZip, NameMapper mapper)
      Compresses the given directory and all its sub-directories into a ZIP file.
      static void pack​(java.io.File sourceDir, java.io.File targetZip, NameMapper mapper, int compressionLevel)
      Compresses the given directory and all its sub-directories into a ZIP file.
      static void pack​(java.io.File sourceDir, java.io.OutputStream os)
      Compresses the given directory and all of its sub-directories into the passed in stream.
      static void pack​(java.io.File sourceDir, java.io.OutputStream os, int compressionLevel)
      Compresses the given directory and all of its sub-directories into the passed in stream.
      static void pack​(java.io.File sourceDir, java.io.OutputStream os, NameMapper mapper)
      Compresses the given directory and all of its sub-directories into the passed in stream.
      static void pack​(java.io.File sourceDir, java.io.OutputStream os, NameMapper mapper, int compressionLevel)
      Compresses the given directory and all of its sub-directories into the passed in stream.
      private static void pack​(java.io.File dir, java.util.zip.ZipOutputStream out, NameMapper mapper, java.lang.String pathPrefix, boolean mustHaveChildren)
      Compresses the given directory and all its sub-directories into a ZIP file.
      static void pack​(ZipEntrySource[] entries, java.io.File zip)
      Compresses the given entries into a new ZIP file.
      static void pack​(ZipEntrySource[] entries, java.io.OutputStream os)
      Compresses the given entries into an output stream.
      private static void pack​(ZipEntrySource[] entries, java.io.OutputStream os, boolean closeStream)  
      static void packEntries​(java.io.File[] filesToPack, java.io.File destZipFile)
      Compresses the given files into a ZIP file.
      static void packEntries​(java.io.File[] filesToPack, java.io.File destZipFile, int compressionLevel)
      Compresses the given files into a ZIP file.
      static void packEntries​(java.io.File[] filesToPack, java.io.File destZipFile, NameMapper mapper)
      Compresses the given files into a ZIP file.
      static void packEntries​(java.io.File[] filesToPack, java.io.File destZipFile, NameMapper mapper, int compressionLevel)
      Compresses the given files into a ZIP file.
      static byte[] packEntry​(java.io.File file)
      Compresses the given file into a ZIP file with single entry.
      static void packEntry​(java.io.File fileToPack, java.io.File destZipFile)
      Compresses the given file into a ZIP file.
      static void packEntry​(java.io.File fileToPack, java.io.File destZipFile, java.lang.String fileName)
      Compresses the given file into a ZIP file.
      static void packEntry​(java.io.File fileToPack, java.io.File destZipFile, NameMapper mapper)
      Compresses the given file into a ZIP file.
      static void removeEntries​(java.io.File zip, java.lang.String[] paths)
      Changes an existing ZIP file: removes entries with given paths.
      static void removeEntries​(java.io.File zip, java.lang.String[] paths, java.io.File destZip)
      Copies an existing ZIP file and removes entries with given paths.
      static void removeEntries​(java.io.File zip, java.lang.String[] paths, java.io.OutputStream destOut)
      Copies an existing ZIP file and removes entries with given paths.
      static void removeEntry​(java.io.File zip, java.lang.String path)
      Changes an existing ZIP file: removes entry with a given path.
      static void removeEntry​(java.io.File zip, java.lang.String path, java.io.File destZip)
      Copies an existing ZIP file and removes entry with a given path.
      static void repack​(java.io.File zip, int compressionLevel)
      Repacks a provided ZIP file and replaces old file with the new one.
      static void repack​(java.io.File srcZip, java.io.File dstZip, int compressionLevel)
      Repacks a provided ZIP file into a new ZIP with a given compression level.
      static void repack​(java.io.InputStream is, java.io.File dstZip, int compressionLevel)
      Repacks a provided ZIP input stream into a ZIP file with a given compression level.
      static boolean replaceEntries​(java.io.File zip, ZipEntrySource[] entries)
      Changes an existing ZIP file: replaces a given entry in it.
      static boolean replaceEntries​(java.io.File zip, ZipEntrySource[] entries, java.io.File destZip)
      Copies an existing ZIP file and replaces the given entries in it.
      static boolean replaceEntry​(java.io.File zip, java.lang.String path, byte[] bytes)
      Changes an existing ZIP file: replaces a given entry in it.
      static boolean replaceEntry​(java.io.File zip, java.lang.String path, byte[] bytes, int compressionMethod)
      Changes an existing ZIP file: replaces a given entry in it.
      static boolean replaceEntry​(java.io.File zip, java.lang.String path, byte[] bytes, java.io.File destZip)
      Copies an existing ZIP file and replaces a given entry in it.
      static boolean replaceEntry​(java.io.File zip, java.lang.String path, java.io.File file)
      Changes an existing ZIP file: replaces a given entry in it.
      static boolean replaceEntry​(java.io.File zip, java.lang.String path, java.io.File file, java.io.File destZip)
      Copies an existing ZIP file and replaces a given entry in it.
      static boolean replaceEntry​(java.io.File zip, ZipEntrySource entry)
      Changes an existing ZIP file: replaces a given entry in it.
      static boolean replaceEntry​(java.io.File zip, ZipEntrySource entry, java.io.File destZip)
      Copies an existing ZIP file and replaces a given entry in it.
      static boolean transformEntries​(java.io.File zip, ZipEntryTransformerEntry[] entries)
      Changes an existing ZIP file: transforms a given entries in it.
      static boolean transformEntries​(java.io.File zip, ZipEntryTransformerEntry[] entries, java.io.File destZip)
      Copies an existing ZIP file and transforms the given entries in it.
      static boolean transformEntries​(java.io.InputStream is, ZipEntryTransformerEntry[] entries, java.io.OutputStream os)
      Copies an existing ZIP file and transforms the given entries in it.
      static boolean transformEntry​(java.io.File zip, java.lang.String path, ZipEntryTransformer transformer)
      Changes an existing ZIP file: transforms a given entry in it.
      static boolean transformEntry​(java.io.File zip, java.lang.String path, ZipEntryTransformer transformer, java.io.File destZip)
      Copies an existing ZIP file and transforms a given entry in it.
      static boolean transformEntry​(java.io.File zip, ZipEntryTransformerEntry entry)
      Changes an existing ZIP file: transforms a given entry in it.
      static boolean transformEntry​(java.io.File zip, ZipEntryTransformerEntry entry, java.io.File destZip)
      Copies an existing ZIP file and transforms a given entry in it.
      static boolean transformEntry​(java.io.InputStream is, java.lang.String path, ZipEntryTransformer transformer, java.io.OutputStream os)
      Copies an existing ZIP file and transforms a given entry in it.
      static boolean transformEntry​(java.io.InputStream is, ZipEntryTransformerEntry entry, java.io.OutputStream os)
      Copies an existing ZIP file and transforms a given entry in it.
      (package private) static java.util.Map<java.lang.String,​ZipEntryTransformer> transformersByPath​(java.util.List<ZipEntryTransformerEntry> entries)  
      static void unexplode​(java.io.File dir)
      Compresses a given directory in its own location.
      static void unexplode​(java.io.File dir, int compressionLevel)
      Compresses a given directory in its own location.
      static void unpack​(java.io.File zip, java.io.File outputDir)
      Unpacks a ZIP file to the given directory.
      static void unpack​(java.io.File zip, java.io.File outputDir, java.nio.charset.Charset charset)
      Unpacks a ZIP file to the given directory using a specific Charset for the input file.
      static void unpack​(java.io.File zip, java.io.File outputDir, NameMapper mapper)
      Unpacks a ZIP file to the given directory using a specific Charset for the input file.
      static void unpack​(java.io.File zip, java.io.File outputDir, NameMapper mapper, java.nio.charset.Charset charset)
      Unpacks a ZIP file to the given directory.
      static void unpack​(java.io.InputStream is, java.io.File outputDir)
      Unpacks a ZIP stream to the given directory.
      static void unpack​(java.io.InputStream is, java.io.File outputDir, java.nio.charset.Charset charset)
      Unpacks a ZIP stream to the given directory.
      static void unpack​(java.io.InputStream is, java.io.File outputDir, NameMapper mapper)
      Unpacks a ZIP stream to the given directory.
      static void unpack​(java.io.InputStream is, java.io.File outputDir, NameMapper mapper, java.nio.charset.Charset charset)
      Unpacks a ZIP stream to the given directory.
      static byte[] unpackEntry​(java.io.File zip, java.lang.String name)
      Unpacks a single entry from a ZIP file.
      static boolean unpackEntry​(java.io.File zip, java.lang.String name, java.io.File file)
      Unpacks a single file from a ZIP archive to a file.
      static boolean unpackEntry​(java.io.File zip, java.lang.String name, java.io.File file, java.nio.charset.Charset charset)
      Unpacks a single file from a ZIP archive to a file.
      static byte[] unpackEntry​(java.io.File zip, java.lang.String name, java.nio.charset.Charset charset)
      Unpacks a single entry from a ZIP file.
      static byte[] unpackEntry​(java.io.InputStream is, java.lang.String name)
      Unpacks a single entry from a ZIP stream.
      static boolean unpackEntry​(java.io.InputStream is, java.lang.String name, java.io.File file)
      Unpacks a single file from a ZIP stream to a file.
      static byte[] unpackEntry​(java.util.zip.ZipFile zf, java.lang.String name)
      Unpacks a single entry from a ZIP file.
      static boolean unpackEntry​(java.util.zip.ZipFile zf, java.lang.String name, java.io.File file)
      Unpacks a single file from a ZIP archive to a file.
      static void unwrap​(java.io.File zip, java.io.File outputDir)
      Unwraps a ZIP file to the given directory shaving of root dir.
      static void unwrap​(java.io.File zip, java.io.File outputDir, NameMapper mapper)
      Unwraps a ZIP file to the given directory shaving of root dir.
      static void unwrap​(java.io.InputStream is, java.io.File outputDir)
      Unwraps a ZIP file to the given directory shaving of root dir.
      static void unwrap​(java.io.InputStream is, java.io.File outputDir, NameMapper mapper)
      Unwraps a ZIP file to the given directory shaving of root dir.
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_COMPRESSION_LEVEL

        public static final int DEFAULT_COMPRESSION_LEVEL
        Default compression level
        See Also:
        Constant Field Values
      • log

        private static final org.slf4j.Logger log
    • Constructor Detail

      • ZipUtil

        private ZipUtil()
    • Method Detail

      • containsEntry

        public static boolean containsEntry​(java.io.File zip,
                                            java.lang.String name)
        Checks if the ZIP file contains the given entry.
        Parameters:
        zip - ZIP file.
        name - entry name.
        Returns:
        true if the ZIP file contains the given entry.
      • getCompressionLevelOfEntry

        @Deprecated
        public static int getCompressionLevelOfEntry​(java.io.File zip,
                                                     java.lang.String name)
        Deprecated.
        The compression level cannot be retrieved. This method exists only to ensure backwards compatibility with ZipUtil version 1.9, which returned the compression method, not the level.
        Returns the compression method of a given entry of the ZIP file.
        Parameters:
        zip - ZIP file.
        name - entry name.
        Returns:
        Returns ZipEntry.STORED, ZipEntry.DEFLATED or -1 if the ZIP file does not contain the given entry.
      • getCompressionMethodOfEntry

        public static int getCompressionMethodOfEntry​(java.io.File zip,
                                                      java.lang.String name)
        Returns the compression method of a given entry of the ZIP file.
        Parameters:
        zip - ZIP file.
        name - entry name.
        Returns:
        Returns ZipEntry.STORED, ZipEntry.DEFLATED or -1 if the ZIP file does not contain the given entry.
      • containsAnyEntry

        public static boolean containsAnyEntry​(java.io.File zip,
                                               java.lang.String[] names)
        Checks if the ZIP file contains any of the given entries.
        Parameters:
        zip - ZIP file.
        names - entry names.
        Returns:
        true if the ZIP file contains any of the given entries.
      • unpackEntry

        public static byte[] unpackEntry​(java.io.File zip,
                                         java.lang.String name)
        Unpacks a single entry from a ZIP file.
        Parameters:
        zip - ZIP file.
        name - entry name.
        Returns:
        contents of the entry or null if it was not found.
      • unpackEntry

        public static byte[] unpackEntry​(java.io.File zip,
                                         java.lang.String name,
                                         java.nio.charset.Charset charset)
        Unpacks a single entry from a ZIP file.
        Parameters:
        zip - ZIP file.
        name - entry name.
        charset - charset to be used to process the zip
        Returns:
        contents of the entry or null if it was not found.
      • unpackEntry

        public static byte[] unpackEntry​(java.util.zip.ZipFile zf,
                                         java.lang.String name)
        Unpacks a single entry from a ZIP file.
        Parameters:
        zf - ZIP file.
        name - entry name.
        Returns:
        contents of the entry or null if it was not found.
      • doUnpackEntry

        private static byte[] doUnpackEntry​(java.util.zip.ZipFile zf,
                                            java.lang.String name)
                                     throws java.io.IOException
        Unpacks a single entry from a ZIP file.
        Parameters:
        zf - ZIP file.
        name - entry name.
        Returns:
        contents of the entry or null if it was not found.
        Throws:
        java.io.IOException
      • unpackEntry

        public static byte[] unpackEntry​(java.io.InputStream is,
                                         java.lang.String name)
        Unpacks a single entry from a ZIP stream.
        Parameters:
        is - ZIP stream.
        name - entry name.
        Returns:
        contents of the entry or null if it was not found.
      • unpackEntry

        public static boolean unpackEntry​(java.io.File zip,
                                          java.lang.String name,
                                          java.io.File file)
        Unpacks a single file from a ZIP archive to a file.
        Parameters:
        zip - ZIP file.
        name - entry name.
        file - target file to be created or overwritten.
        Returns:
        true if the entry was found and unpacked, false if the entry was not found.
      • unpackEntry

        public static boolean unpackEntry​(java.io.File zip,
                                          java.lang.String name,
                                          java.io.File file,
                                          java.nio.charset.Charset charset)
        Unpacks a single file from a ZIP archive to a file.
        Parameters:
        zip - ZIP file.
        name - entry name.
        file - target file to be created or overwritten.
        charset - charset to be used processing the zip
        Returns:
        true if the entry was found and unpacked, false if the entry was not found.
      • unpackEntry

        public static boolean unpackEntry​(java.util.zip.ZipFile zf,
                                          java.lang.String name,
                                          java.io.File file)
        Unpacks a single file from a ZIP archive to a file.
        Parameters:
        zf - ZIP file.
        name - entry name.
        file - target file to be created or overwritten.
        Returns:
        true if the entry was found and unpacked, false if the entry was not found.
      • doUnpackEntry

        private static boolean doUnpackEntry​(java.util.zip.ZipFile zf,
                                             java.lang.String name,
                                             java.io.File file)
                                      throws java.io.IOException
        Unpacks a single file from a ZIP archive to a file.
        Parameters:
        zf - ZIP file.
        name - entry name.
        file - target file to be created or overwritten.
        Returns:
        true if the entry was found and unpacked, false if the entry was not found.
        Throws:
        java.io.IOException
      • unpackEntry

        public static boolean unpackEntry​(java.io.InputStream is,
                                          java.lang.String name,
                                          java.io.File file)
                                   throws java.io.IOException
        Unpacks a single file from a ZIP stream to a file.
        Parameters:
        is - ZIP stream.
        name - entry name.
        file - target file to be created or overwritten.
        Returns:
        true if the entry was found and unpacked, false if the entry was not found.
        Throws:
        java.io.IOException - if file is not found or writing to it fails
      • iterate

        public static void iterate​(java.io.File zip,
                                   ZipEntryCallback action)
        Reads the given ZIP file and executes the given action for each entry.

        For each entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.

        Parameters:
        zip - input ZIP file.
        action - action to be called for each entry.
        See Also:
        ZipEntryCallback, iterate(File, ZipInfoCallback)
      • iterate

        public static void iterate​(java.io.File zip,
                                   ZipEntryCallback action,
                                   java.nio.charset.Charset charset)
        Reads the given ZIP file and executes the given action for each entry.

        For each entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.

        Parameters:
        zip - input ZIP file.
        action - action to be called for each entry.
        charset - Charset used to processed the ZipFile with
        See Also:
        ZipEntryCallback, iterate(File, ZipInfoCallback)
      • iterate

        public static void iterate​(java.io.File zip,
                                   java.lang.String[] entryNames,
                                   ZipEntryCallback action)
        Reads the given ZIP file and executes the given action for each given entry.

        For each given entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.

        Parameters:
        zip - input ZIP file.
        entryNames - names of entries to iterate
        action - action to be called for each entry.
        See Also:
        ZipEntryCallback, iterate(File, String[], ZipInfoCallback)
      • iterate

        public static void iterate​(java.io.File zip,
                                   java.lang.String[] entryNames,
                                   ZipEntryCallback action,
                                   java.nio.charset.Charset charset)
        Reads the given ZIP file and executes the given action for each given entry.

        For each given entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.

        Parameters:
        zip - input ZIP file.
        entryNames - names of entries to iterate
        action - action to be called for each entry.
        charset - charset used to process the zip file
        See Also:
        ZipEntryCallback, iterate(File, String[], ZipInfoCallback)
      • iterate

        public static void iterate​(java.io.File zip,
                                   ZipInfoCallback action)
        Scans the given ZIP file and executes the given action for each entry.

        Only the meta-data without the actual data is read. If you want to stop the loop then throw a ZipBreakException.

        Parameters:
        zip - input ZIP file.
        action - action to be called for each entry.
        See Also:
        ZipInfoCallback, iterate(File, ZipEntryCallback)
      • iterate

        public static void iterate​(java.io.File zip,
                                   java.lang.String[] entryNames,
                                   ZipInfoCallback action)
        Scans the given ZIP file and executes the given action for each given entry.

        Only the meta-data without the actual data is read. If you want to stop the loop then throw a ZipBreakException.

        Parameters:
        zip - input ZIP file.
        entryNames - names of entries to iterate
        action - action to be called for each entry.
        See Also:
        ZipInfoCallback, iterate(File, String[], ZipEntryCallback)
      • iterate

        public static void iterate​(java.io.InputStream is,
                                   ZipEntryCallback action,
                                   java.nio.charset.Charset charset)
        Reads the given ZIP stream and executes the given action for each entry.

        For each entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.

        Parameters:
        is - input ZIP stream (it will not be closed automatically).
        action - action to be called for each entry.
        charset - charset to process entries in
        See Also:
        ZipEntryCallback, iterate(File, ZipEntryCallback)
      • iterate

        public static void iterate​(java.io.InputStream is,
                                   java.lang.String[] entryNames,
                                   ZipEntryCallback action,
                                   java.nio.charset.Charset charset)
        Reads the given ZIP stream and executes the given action for each given entry.

        For each given entry the corresponding input stream is also passed to the action. If you want to stop the loop then throw a ZipBreakException.

        Parameters:
        is - input ZIP stream (it will not be closed automatically).
        entryNames - names of entries to iterate
        action - action to be called for each entry.
        charset - charset to process entries in
        See Also:
        ZipEntryCallback, iterate(File, String[], ZipEntryCallback)
      • newCloseShieldZipInputStream

        private static java.util.zip.ZipInputStream newCloseShieldZipInputStream​(java.io.InputStream is,
                                                                                 java.nio.charset.Charset charset)
        Creates a new ZipInputStream based on the given InputStream. It will be buffered and close-shielded. Closing the result stream flushes the buffers and frees up resources of the ZipInputStream. However the source stream itself remains open.
      • handle

        public static boolean handle​(java.io.File zip,
                                     java.lang.String name,
                                     ZipEntryCallback action)
        Reads the given ZIP file and executes the given action for a single entry.
        Parameters:
        zip - input ZIP file.
        name - entry name.
        action - action to be called for this entry.
        Returns:
        true if the entry was found, false if the entry was not found.
        See Also:
        ZipEntryCallback
      • handle

        public static boolean handle​(java.io.InputStream is,
                                     java.lang.String name,
                                     ZipEntryCallback action)
        Reads the given ZIP stream and executes the given action for a single entry.
        Parameters:
        is - input ZIP stream (it will not be closed automatically).
        name - entry name.
        action - action to be called for this entry.
        Returns:
        true if the entry was found, false if the entry was not found.
        See Also:
        ZipEntryCallback
      • unpack

        public static void unpack​(java.io.File zip,
                                  java.io.File outputDir)
        Unpacks a ZIP file to the given directory.

        The output directory must not be a file.

        Parameters:
        zip - input ZIP file.
        outputDir - output directory (created automatically if not found).
      • unpack

        public static void unpack​(java.io.File zip,
                                  java.io.File outputDir,
                                  java.nio.charset.Charset charset)
        Unpacks a ZIP file to the given directory using a specific Charset for the input file.

        The output directory must not be a file.

        Parameters:
        zip - input ZIP file.
        outputDir - output directory (created automatically if not found).
        charset - charset used to unpack the zip file
      • unpack

        public static void unpack​(java.io.File zip,
                                  java.io.File outputDir,
                                  NameMapper mapper,
                                  java.nio.charset.Charset charset)
        Unpacks a ZIP file to the given directory.

        The output directory must not be a file.

        Parameters:
        zip - input ZIP file.
        outputDir - output directory (created automatically if not found).
        mapper - call-back for renaming the entries.
        charset - charset used to process the zip file
      • unpack

        public static void unpack​(java.io.File zip,
                                  java.io.File outputDir,
                                  NameMapper mapper)
        Unpacks a ZIP file to the given directory using a specific Charset for the input file.

        The output directory must not be a file.

        Parameters:
        zip - input ZIP file.
        outputDir - output directory (created automatically if not found).
        mapper - call-back for renaming the entries.
      • unwrap

        public static void unwrap​(java.io.File zip,
                                  java.io.File outputDir)
        Unwraps a ZIP file to the given directory shaving of root dir. If there are multiple root dirs or entries in the root of zip, ZipException is thrown.

        The output directory must not be a file.

        Parameters:
        zip - input ZIP file.
        outputDir - output directory (created automatically if not found).
      • unwrap

        public static void unwrap​(java.io.File zip,
                                  java.io.File outputDir,
                                  NameMapper mapper)
        Unwraps a ZIP file to the given directory shaving of root dir. If there are multiple root dirs or entries in the root of zip, ZipException is thrown.

        The output directory must not be a file.

        Parameters:
        zip - input ZIP file.
        outputDir - output directory (created automatically if not found).
        mapper - call-back for renaming the entries.
      • unpack

        public static void unpack​(java.io.InputStream is,
                                  java.io.File outputDir)
        Unpacks a ZIP stream to the given directory.

        The output directory must not be a file.

        Parameters:
        is - inputstream for ZIP file.
        outputDir - output directory (created automatically if not found).
      • unpack

        public static void unpack​(java.io.InputStream is,
                                  java.io.File outputDir,
                                  java.nio.charset.Charset charset)
        Unpacks a ZIP stream to the given directory.

        The output directory must not be a file.

        Parameters:
        is - inputstream for ZIP file.
        outputDir - output directory (created automatically if not found).
        charset - charset used to process the zip stream
      • unpack

        public static void unpack​(java.io.InputStream is,
                                  java.io.File outputDir,
                                  NameMapper mapper)
        Unpacks a ZIP stream to the given directory.

        The output directory must not be a file.

        Parameters:
        is - inputstream for ZIP file.
        outputDir - output directory (created automatically if not found).
        mapper - call-back for renaming the entries.
      • unpack

        public static void unpack​(java.io.InputStream is,
                                  java.io.File outputDir,
                                  NameMapper mapper,
                                  java.nio.charset.Charset charset)
        Unpacks a ZIP stream to the given directory.

        The output directory must not be a file.

        Parameters:
        is - inputstream for ZIP file.
        outputDir - output directory (created automatically if not found).
        mapper - call-back for renaming the entries.
        charset - charset to use when unpacking the stream
      • unwrap

        public static void unwrap​(java.io.InputStream is,
                                  java.io.File outputDir)
        Unwraps a ZIP file to the given directory shaving of root dir. If there are multiple root dirs or entries in the root of zip, ZipException is thrown.

        The output directory must not be a file.

        Parameters:
        is - inputstream for ZIP file.
        outputDir - output directory (created automatically if not found).
      • unwrap

        public static void unwrap​(java.io.InputStream is,
                                  java.io.File outputDir,
                                  NameMapper mapper)
        Unwraps a ZIP file to the given directory shaving of root dir. If there are multiple root dirs or entries in the root of zip, ZipException is thrown.

        The output directory must not be a file.

        Parameters:
        is - inputstream for ZIP file.
        outputDir - output directory (created automatically if not found).
        mapper - call-back for renaming the entries.
      • makeDestinationFile

        private static java.io.File makeDestinationFile​(java.io.File outputDir,
                                                        java.lang.String name)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • checkDestinationFileForTraversal

        private static java.io.File checkDestinationFileForTraversal​(java.io.File outputDir,
                                                                     java.lang.String name,
                                                                     java.io.File destFile)
                                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • explode

        public static void explode​(java.io.File zip)
        Unpacks a ZIP file to its own location.

        The ZIP file will be first renamed (using a temporary name). After the extraction it will be deleted.

        Parameters:
        zip - input ZIP file as well as the target directory.
        See Also:
        unpack(File, File)
      • packEntry

        public static byte[] packEntry​(java.io.File file)
        Compresses the given file into a ZIP file with single entry.
        Parameters:
        file - file to be compressed.
        Returns:
        ZIP file created.
      • pack

        public static void pack​(java.io.File rootDir,
                                java.io.File zip)
        Compresses the given directory and all its sub-directories into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist. Will not include the root directory name in the archive.

        Parameters:
        rootDir - root directory.
        zip - ZIP file that will be created or overwritten.
      • pack

        public static void pack​(java.io.File rootDir,
                                java.io.File zip,
                                int compressionLevel)
        Compresses the given directory and all its sub-directories into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist. Will not include the root directory name in the archive.

        Parameters:
        rootDir - root directory.
        zip - ZIP file that will be created or overwritten.
        compressionLevel - compression level
      • pack

        public static void pack​(java.io.File sourceDir,
                                java.io.File targetZipFile,
                                boolean preserveRoot)
        Compresses the given directory and all its sub-directories into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist. Will not include the root directory name in the archive.

        Parameters:
        sourceDir - root directory.
        targetZipFile - ZIP file that will be created or overwritten.
        preserveRoot - true if the resulted archive should have the top directory entry
      • packEntry

        public static void packEntry​(java.io.File fileToPack,
                                     java.io.File destZipFile)
        Compresses the given file into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        fileToPack - file that needs to be zipped.
        destZipFile - ZIP file that will be created or overwritten.
      • packEntry

        public static void packEntry​(java.io.File fileToPack,
                                     java.io.File destZipFile,
                                     java.lang.String fileName)
        Compresses the given file into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        fileToPack - file that needs to be zipped.
        destZipFile - ZIP file that will be created or overwritten.
        fileName - the name for the file inside the archive
      • packEntry

        public static void packEntry​(java.io.File fileToPack,
                                     java.io.File destZipFile,
                                     NameMapper mapper)
        Compresses the given file into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        fileToPack - file that needs to be zipped.
        destZipFile - ZIP file that will be created or overwritten.
        mapper - call-back for renaming the entries.
      • packEntries

        public static void packEntries​(java.io.File[] filesToPack,
                                       java.io.File destZipFile)
        Compresses the given files into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        filesToPack - files that needs to be zipped.
        destZipFile - ZIP file that will be created or overwritten.
      • packEntries

        public static void packEntries​(java.io.File[] filesToPack,
                                       java.io.File destZipFile,
                                       NameMapper mapper)
        Compresses the given files into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        filesToPack - files that needs to be zipped.
        destZipFile - ZIP file that will be created or overwritten.
        mapper - call-back for renaming the entries.
      • packEntries

        public static void packEntries​(java.io.File[] filesToPack,
                                       java.io.File destZipFile,
                                       int compressionLevel)
        Compresses the given files into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        filesToPack - files that needs to be zipped.
        destZipFile - ZIP file that will be created or overwritten.
        compressionLevel - ZIP file compression level (speed versus filesize), e.g. Deflater.NO_COMPRESSION, Deflater.BEST_SPEED, or Deflater.BEST_COMPRESSION
      • packEntries

        public static void packEntries​(java.io.File[] filesToPack,
                                       java.io.File destZipFile,
                                       NameMapper mapper,
                                       int compressionLevel)
        Compresses the given files into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        filesToPack - files that needs to be zipped.
        destZipFile - ZIP file that will be created or overwritten.
        mapper - call-back for renaming the entries.
        compressionLevel - ZIP file compression level (speed versus filesize), e.g. Deflater.NO_COMPRESSION, Deflater.BEST_SPEED, or Deflater.BEST_COMPRESSION
      • pack

        public static void pack​(java.io.File sourceDir,
                                java.io.File targetZip,
                                NameMapper mapper)
        Compresses the given directory and all its sub-directories into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        sourceDir - root directory.
        targetZip - ZIP file that will be created or overwritten.
        mapper - call-back for renaming the entries.
      • pack

        public static void pack​(java.io.File sourceDir,
                                java.io.File targetZip,
                                NameMapper mapper,
                                int compressionLevel)
        Compresses the given directory and all its sub-directories into a ZIP file.

        The ZIP file must not be a directory and its parent directory must exist.

        Parameters:
        sourceDir - root directory.
        targetZip - ZIP file that will be created or overwritten.
        mapper - call-back for renaming the entries.
        compressionLevel - compression level
      • pack

        public static void pack​(java.io.File sourceDir,
                                java.io.OutputStream os)
        Compresses the given directory and all of its sub-directories into the passed in stream. It is the responsibility of the caller to close the passed in stream properly.
        Parameters:
        sourceDir - root directory.
        os - output stream (will be buffered in this method).
        Since:
        1.10
      • pack

        public static void pack​(java.io.File sourceDir,
                                java.io.OutputStream os,
                                int compressionLevel)
        Compresses the given directory and all of its sub-directories into the passed in stream. It is the responsibility of the caller to close the passed in stream properly.
        Parameters:
        sourceDir - root directory.
        os - output stream (will be buffered in this method).
        compressionLevel - compression level
        Since:
        1.10
      • pack

        public static void pack​(java.io.File sourceDir,
                                java.io.OutputStream os,
                                NameMapper mapper)
        Compresses the given directory and all of its sub-directories into the passed in stream. It is the responsibility of the caller to close the passed in stream properly.
        Parameters:
        sourceDir - root directory.
        os - output stream (will be buffered in this method).
        mapper - call-back for renaming the entries.
        Since:
        1.10
      • pack

        public static void pack​(java.io.File sourceDir,
                                java.io.OutputStream os,
                                NameMapper mapper,
                                int compressionLevel)
        Compresses the given directory and all of its sub-directories into the passed in stream. It is the responsibility of the caller to close the passed in stream properly.
        Parameters:
        sourceDir - root directory.
        os - output stream (will be buffered in this method).
        mapper - call-back for renaming the entries.
        compressionLevel - compression level
        Since:
        1.10
      • pack

        private static void pack​(java.io.File dir,
                                 java.util.zip.ZipOutputStream out,
                                 NameMapper mapper,
                                 java.lang.String pathPrefix,
                                 boolean mustHaveChildren)
                          throws java.io.IOException
        Compresses the given directory and all its sub-directories into a ZIP file.
        Parameters:
        dir - root directory.
        out - ZIP output stream.
        mapper - call-back for renaming the entries.
        pathPrefix - prefix to be used for the entries.
        mustHaveChildren - if true, but directory to pack doesn't have any files, throw an exception.
        Throws:
        java.io.IOException
      • repack

        public static void repack​(java.io.File srcZip,
                                  java.io.File dstZip,
                                  int compressionLevel)
        Repacks a provided ZIP file into a new ZIP with a given compression level.

        Parameters:
        srcZip - source ZIP file.
        dstZip - destination ZIP file.
        compressionLevel - compression level.
      • repack

        public static void repack​(java.io.InputStream is,
                                  java.io.File dstZip,
                                  int compressionLevel)
        Repacks a provided ZIP input stream into a ZIP file with a given compression level.

        Parameters:
        is - ZIP input stream.
        dstZip - destination ZIP file.
        compressionLevel - compression level.
      • repack

        public static void repack​(java.io.File zip,
                                  int compressionLevel)
        Repacks a provided ZIP file and replaces old file with the new one.

        Parameters:
        zip - source ZIP file to be repacked and replaced.
        compressionLevel - compression level.
      • createEmpty

        public static void createEmpty​(java.io.File file)
        Creates an empty ZIP archive at the location of the provided file.
        Parameters:
        file - the file to become an empty ZIP archive
      • unexplode

        public static void unexplode​(java.io.File dir)
        Compresses a given directory in its own location.

        A ZIP file will be first created with a temporary name. After the compressing the directory will be deleted and the ZIP file will be renamed as the original directory.

        Parameters:
        dir - input directory as well as the target ZIP file.
        See Also:
        pack(File, File)
      • unexplode

        public static void unexplode​(java.io.File dir,
                                     int compressionLevel)
        Compresses a given directory in its own location.

        A ZIP file will be first created with a temporary name. After the compressing the directory will be deleted and the ZIP file will be renamed as the original directory.

        Parameters:
        dir - input directory as well as the target ZIP file.
        compressionLevel - compression level
        See Also:
        pack(File, File)
      • pack

        public static void pack​(ZipEntrySource[] entries,
                                java.io.OutputStream os)
        Compresses the given entries into an output stream.
        Parameters:
        entries - ZIP entries added.
        os - output stream for the new ZIP (does not have to be buffered)
        Since:
        1.9
      • pack

        private static void pack​(ZipEntrySource[] entries,
                                 java.io.OutputStream os,
                                 boolean closeStream)
      • pack

        public static void pack​(ZipEntrySource[] entries,
                                java.io.File zip)
        Compresses the given entries into a new ZIP file.
        Parameters:
        entries - ZIP entries added.
        zip - new ZIP file created.
      • addEntry

        public static void addEntry​(java.io.File zip,
                                    java.lang.String path,
                                    java.io.File file,
                                    java.io.File destZip)
        Copies an existing ZIP file and appends it with one new entry.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        file - new entry to be added.
        destZip - new ZIP file created.
      • addEntry

        public static void addEntry​(java.io.File zip,
                                    java.lang.String path,
                                    java.io.File file)
        Changes a zip file, adds one new entry in-place.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        file - new entry to be added.
      • addEntry

        public static void addEntry​(java.io.File zip,
                                    java.lang.String path,
                                    byte[] bytes,
                                    java.io.File destZip)
        Copies an existing ZIP file and appends it with one new entry.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        bytes - new entry bytes (or null if directory).
        destZip - new ZIP file created.
      • addEntry

        public static void addEntry​(java.io.File zip,
                                    java.lang.String path,
                                    byte[] bytes,
                                    java.io.File destZip,
                                    int compressionMethod)
        Copies an existing ZIP file and appends it with one new entry.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        bytes - new entry bytes (or null if directory).
        destZip - new ZIP file created.
        compressionMethod - the new compression method (ZipEntry.STORED or ZipEntry.DEFLATED).
      • addEntry

        public static void addEntry​(java.io.File zip,
                                    java.lang.String path,
                                    byte[] bytes)
        Changes a zip file, adds one new entry in-place.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        bytes - new entry bytes (or null if directory).
      • addEntry

        public static void addEntry​(java.io.File zip,
                                    java.lang.String path,
                                    byte[] bytes,
                                    int compressionMethod)
        Changes a zip file, adds one new entry in-place.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        bytes - new entry bytes (or null if directory).
        compressionMethod - the new compression method (ZipEntry.STORED or ZipEntry.DEFLATED).
      • addEntry

        public static void addEntry​(java.io.File zip,
                                    ZipEntrySource entry,
                                    java.io.File destZip)
        Copies an existing ZIP file and appends it with one new entry.
        Parameters:
        zip - an existing ZIP file (only read).
        entry - new ZIP entry appended.
        destZip - new ZIP file created.
      • addEntry

        public static void addEntry​(java.io.File zip,
                                    ZipEntrySource entry)
        Changes a zip file, adds one new entry in-place.
        Parameters:
        zip - an existing ZIP file (only read).
        entry - new ZIP entry appended.
      • addEntries

        public static void addEntries​(java.io.File zip,
                                      ZipEntrySource[] entries,
                                      java.io.File destZip)
        Copies an existing ZIP file and appends it with new entries.
        Parameters:
        zip - an existing ZIP file (only read).
        entries - new ZIP entries appended.
        destZip - new ZIP file created.
      • addEntries

        public static void addEntries​(java.io.File zip,
                                      ZipEntrySource[] entries,
                                      java.io.OutputStream destOut)
        Copies an existing ZIP file and appends it with new entries.
        Parameters:
        zip - an existing ZIP file (only read).
        entries - new ZIP entries appended.
        destOut - new ZIP destination output stream
      • addEntries

        public static void addEntries​(java.io.InputStream is,
                                      ZipEntrySource[] entries,
                                      java.io.OutputStream destOut)
        Copies an existing ZIP file and appends it with new entries.
        Parameters:
        is - an existing ZIP input stream.
        entries - new ZIP entries appended.
        destOut - new ZIP destination output stream
        Since:
        1.9
      • addEntries

        public static void addEntries​(java.io.File zip,
                                      ZipEntrySource[] entries)
        Changes a zip file it with with new entries. in-place.
        Parameters:
        zip - an existing ZIP file (only read).
        entries - new ZIP entries appended.
      • removeEntry

        public static void removeEntry​(java.io.File zip,
                                       java.lang.String path,
                                       java.io.File destZip)
        Copies an existing ZIP file and removes entry with a given path.
        Parameters:
        zip - an existing ZIP file (only read)
        path - path of the entry to remove
        destZip - new ZIP file created.
        Since:
        1.7
      • removeEntry

        public static void removeEntry​(java.io.File zip,
                                       java.lang.String path)
        Changes an existing ZIP file: removes entry with a given path.
        Parameters:
        zip - an existing ZIP file
        path - path of the entry to remove
        Since:
        1.7
      • removeEntries

        public static void removeEntries​(java.io.File zip,
                                         java.lang.String[] paths,
                                         java.io.File destZip)
        Copies an existing ZIP file and removes entries with given paths.
        Parameters:
        zip - an existing ZIP file (only read)
        paths - paths of the entries to remove
        destZip - new ZIP file created.
        Since:
        1.7
      • removeEntries

        public static void removeEntries​(java.io.File zip,
                                         java.lang.String[] paths,
                                         java.io.OutputStream destOut)
        Copies an existing ZIP file and removes entries with given paths.
        Parameters:
        zip - an existing ZIP file (only read)
        paths - paths of the entries to remove
        destOut - new ZIP destination output stream
        Since:
        1.14
      • removeEntries

        public static void removeEntries​(java.io.File zip,
                                         java.lang.String[] paths)
        Changes an existing ZIP file: removes entries with given paths.
        Parameters:
        zip - an existing ZIP file
        paths - paths of the entries to remove
        Since:
        1.7
      • copyEntries

        private static void copyEntries​(java.io.File zip,
                                        java.util.zip.ZipOutputStream out)
        Copies all entries from one ZIP file to another.
        Parameters:
        zip - source ZIP file.
        out - target ZIP stream.
      • copyEntries

        private static void copyEntries​(java.io.InputStream is,
                                        java.util.zip.ZipOutputStream out)
        Copies all entries from one ZIP stream to another.
        Parameters:
        is - source stream (contains ZIP file).
        out - target ZIP stream.
      • copyEntries

        private static void copyEntries​(java.io.File zip,
                                        java.util.zip.ZipOutputStream out,
                                        java.util.Set<java.lang.String> ignoredEntries)
        Copies all entries from one ZIP file to another, ignoring entries with path in ignoredEntries
        Parameters:
        zip - source ZIP file.
        out - target ZIP stream.
        ignoredEntries - paths of entries not to copy
      • filterDirEntries

        static java.util.Set<java.lang.String> filterDirEntries​(java.io.File zip,
                                                                java.util.Collection<java.lang.String> names)
        Parameters:
        zip - zip file to traverse
        names - names of entries to filter dirs from
        Returns:
        Set names of entries that are dirs.
      • replaceEntry

        public static boolean replaceEntry​(java.io.File zip,
                                           java.lang.String path,
                                           java.io.File file,
                                           java.io.File destZip)
        Copies an existing ZIP file and replaces a given entry in it.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        file - new entry.
        destZip - new ZIP file created.
        Returns:
        true if the entry was replaced.
      • replaceEntry

        public static boolean replaceEntry​(java.io.File zip,
                                           java.lang.String path,
                                           java.io.File file)
        Changes an existing ZIP file: replaces a given entry in it.
        Parameters:
        zip - an existing ZIP file.
        path - new ZIP entry path.
        file - new entry.
        Returns:
        true if the entry was replaced.
      • replaceEntry

        public static boolean replaceEntry​(java.io.File zip,
                                           java.lang.String path,
                                           byte[] bytes,
                                           java.io.File destZip)
        Copies an existing ZIP file and replaces a given entry in it.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        bytes - new entry bytes (or null if directory).
        destZip - new ZIP file created.
        Returns:
        true if the entry was replaced.
      • replaceEntry

        public static boolean replaceEntry​(java.io.File zip,
                                           java.lang.String path,
                                           byte[] bytes)
        Changes an existing ZIP file: replaces a given entry in it.
        Parameters:
        zip - an existing ZIP file.
        path - new ZIP entry path.
        bytes - new entry bytes (or null if directory).
        Returns:
        true if the entry was replaced.
      • replaceEntry

        public static boolean replaceEntry​(java.io.File zip,
                                           java.lang.String path,
                                           byte[] bytes,
                                           int compressionMethod)
        Changes an existing ZIP file: replaces a given entry in it.
        Parameters:
        zip - an existing ZIP file.
        path - new ZIP entry path.
        bytes - new entry bytes (or null if directory).
        compressionMethod - the new compression method (ZipEntry.STORED or ZipEntry.DEFLATED).
        Returns:
        true if the entry was replaced.
      • replaceEntry

        public static boolean replaceEntry​(java.io.File zip,
                                           ZipEntrySource entry,
                                           java.io.File destZip)
        Copies an existing ZIP file and replaces a given entry in it.
        Parameters:
        zip - an existing ZIP file (only read).
        entry - new ZIP entry.
        destZip - new ZIP file created.
        Returns:
        true if the entry was replaced.
      • replaceEntry

        public static boolean replaceEntry​(java.io.File zip,
                                           ZipEntrySource entry)
        Changes an existing ZIP file: replaces a given entry in it.
        Parameters:
        zip - an existing ZIP file.
        entry - new ZIP entry.
        Returns:
        true if the entry was replaced.
      • replaceEntries

        public static boolean replaceEntries​(java.io.File zip,
                                             ZipEntrySource[] entries,
                                             java.io.File destZip)
        Copies an existing ZIP file and replaces the given entries in it.
        Parameters:
        zip - an existing ZIP file (only read).
        entries - new ZIP entries to be replaced with.
        destZip - new ZIP file created.
        Returns:
        true if at least one entry was replaced.
      • replaceEntries

        public static boolean replaceEntries​(java.io.File zip,
                                             ZipEntrySource[] entries)
        Changes an existing ZIP file: replaces a given entry in it.
        Parameters:
        zip - an existing ZIP file.
        entries - new ZIP entries to be replaced with.
        Returns:
        true if at least one entry was replaced.
      • addOrReplaceEntries

        public static void addOrReplaceEntries​(java.io.File zip,
                                               ZipEntrySource[] entries,
                                               java.io.File destZip)
        Copies an existing ZIP file and adds/replaces the given entries in it.
        Parameters:
        zip - an existing ZIP file (only read).
        entries - ZIP entries to be replaced or added.
        destZip - new ZIP file created.
      • addOrReplaceEntries

        public static void addOrReplaceEntries​(java.io.File zip,
                                               ZipEntrySource[] entries)
        Changes a ZIP file: adds/replaces the given entries in it.
        Parameters:
        zip - an existing ZIP file (only read).
        entries - ZIP entries to be replaced or added.
      • entriesByPath

        static java.util.Map<java.lang.String,​ZipEntrySource> entriesByPath​(ZipEntrySource... entries)
        Returns:
        given entries indexed by path.
      • transformEntry

        public static boolean transformEntry​(java.io.File zip,
                                             java.lang.String path,
                                             ZipEntryTransformer transformer,
                                             java.io.File destZip)
        Copies an existing ZIP file and transforms a given entry in it.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        transformer - transformer for the given ZIP entry.
        destZip - new ZIP file created.
        Returns:
        true if the entry was replaced.
        Throws:
        java.lang.IllegalArgumentException - if the destination is the same as the location
      • transformEntry

        public static boolean transformEntry​(java.io.File zip,
                                             java.lang.String path,
                                             ZipEntryTransformer transformer)
        Changes an existing ZIP file: transforms a given entry in it.
        Parameters:
        zip - an existing ZIP file (only read).
        path - new ZIP entry path.
        transformer - transformer for the given ZIP entry.
        Returns:
        true if the entry was replaced.
      • transformEntry

        public static boolean transformEntry​(java.io.File zip,
                                             ZipEntryTransformerEntry entry,
                                             java.io.File destZip)
        Copies an existing ZIP file and transforms a given entry in it.
        Parameters:
        zip - an existing ZIP file (only read).
        entry - transformer for a ZIP entry.
        destZip - new ZIP file created.
        Returns:
        true if the entry was replaced.
      • transformEntry

        public static boolean transformEntry​(java.io.File zip,
                                             ZipEntryTransformerEntry entry)
        Changes an existing ZIP file: transforms a given entry in it.
        Parameters:
        zip - an existing ZIP file (only read).
        entry - transformer for a ZIP entry.
        Returns:
        true if the entry was replaced.
      • transformEntries

        public static boolean transformEntries​(java.io.File zip,
                                               ZipEntryTransformerEntry[] entries,
                                               java.io.File destZip)
        Copies an existing ZIP file and transforms the given entries in it.
        Parameters:
        zip - an existing ZIP file (only read).
        entries - ZIP entry transformers.
        destZip - new ZIP file created.
        Returns:
        true if at least one entry was replaced.
      • transformEntries

        public static boolean transformEntries​(java.io.File zip,
                                               ZipEntryTransformerEntry[] entries)
        Changes an existing ZIP file: transforms a given entries in it.
        Parameters:
        zip - an existing ZIP file (only read).
        entries - ZIP entry transformers.
        Returns:
        true if the entry was replaced.
      • transformEntry

        public static boolean transformEntry​(java.io.InputStream is,
                                             java.lang.String path,
                                             ZipEntryTransformer transformer,
                                             java.io.OutputStream os)
        Copies an existing ZIP file and transforms a given entry in it.
        Parameters:
        is - a ZIP input stream.
        path - new ZIP entry path.
        transformer - transformer for the given ZIP entry.
        os - a ZIP output stream.
        Returns:
        true if the entry was replaced.
      • transformEntry

        public static boolean transformEntry​(java.io.InputStream is,
                                             ZipEntryTransformerEntry entry,
                                             java.io.OutputStream os)
        Copies an existing ZIP file and transforms a given entry in it.
        Parameters:
        is - a ZIP input stream.
        entry - transformer for a ZIP entry.
        os - a ZIP output stream.
        Returns:
        true if the entry was replaced.
      • transformEntries

        public static boolean transformEntries​(java.io.InputStream is,
                                               ZipEntryTransformerEntry[] entries,
                                               java.io.OutputStream os)
        Copies an existing ZIP file and transforms the given entries in it.
        Parameters:
        is - a ZIP input stream.
        entries - ZIP entry transformers.
        os - a ZIP output stream.
        Returns:
        true if at least one entry was replaced.
      • addEntry

        private static void addEntry​(ZipEntrySource entry,
                                     java.util.zip.ZipOutputStream out)
                              throws java.io.IOException
        Adds a given ZIP entry to a ZIP file.
        Parameters:
        entry - new ZIP entry.
        out - target ZIP stream.
        Throws:
        java.io.IOException
      • archiveEquals

        public static boolean archiveEquals​(java.io.File f1,
                                            java.io.File f2)
        Compares two ZIP files and returns true if they contain same entries.

        First the two files are compared byte-by-byte. If a difference is found the corresponding entries of both ZIP files are compared. Thus if same contents is packed differently the two archives may still be the same.

        Two archives are considered the same if

        1. they contain same number of entries,
        2. for each entry in the first archive there exists an entry with the same in the second archive
        3. for each entry in the first archive and the entry with the same name in the second archive
          1. both are either directories or files,
          2. both have the same size,
          3. both have the same CRC,
          4. both have the same contents (compared byte-by-byte).
        Parameters:
        f1 - first ZIP file.
        f2 - second ZIP file.
        Returns:
        true if the two ZIP files contain same entries, false if a difference was found or an error occurred during the comparison.
      • archiveEqualsInternal

        private static boolean archiveEqualsInternal​(java.io.File f1,
                                                     java.io.File f2)
                                              throws java.io.IOException
        Throws:
        java.io.IOException
      • metaDataEquals

        private static boolean metaDataEquals​(java.lang.String path,
                                              java.util.zip.ZipEntry e1,
                                              java.util.zip.ZipEntry e2)
                                       throws java.io.IOException
        Compares meta-data of two ZIP entries.

        Two entries are considered the same if

        1. both entries exist,
        2. both entries are either directories or files,
        3. both entries have the same size,
        4. both entries have the same CRC.
        Parameters:
        path - name of the entries.
        e1 - first entry (required).
        e2 - second entry (may be null).
        Returns:
        true if no difference was found.
        Throws:
        java.io.IOException
      • entryEquals

        public static boolean entryEquals​(java.io.File f1,
                                          java.io.File f2,
                                          java.lang.String path)
        Compares same entry in two ZIP files (byte-by-byte).
        Parameters:
        f1 - first ZIP file.
        f2 - second ZIP file.
        path - name of the entry.
        Returns:
        true if the contents of the entry was same in both ZIP files.
      • entryEquals

        public static boolean entryEquals​(java.io.File f1,
                                          java.io.File f2,
                                          java.lang.String path1,
                                          java.lang.String path2)
        Compares two ZIP entries (byte-by-byte). .
        Parameters:
        f1 - first ZIP file.
        f2 - second ZIP file.
        path1 - name of the first entry.
        path2 - name of the second entry.
        Returns:
        true if the contents of the entries were same.
      • entryEquals

        public static boolean entryEquals​(java.util.zip.ZipFile zf1,
                                          java.util.zip.ZipFile zf2,
                                          java.lang.String path1,
                                          java.lang.String path2)
        Compares two ZIP entries (byte-by-byte). .
        Parameters:
        zf1 - first ZIP file.
        zf2 - second ZIP file.
        path1 - name of the first entry.
        path2 - name of the second entry.
        Returns:
        true if the contents of the entries were same.
      • doEntryEquals

        private static boolean doEntryEquals​(java.util.zip.ZipFile zf1,
                                             java.util.zip.ZipFile zf2,
                                             java.lang.String path1,
                                             java.lang.String path2)
                                      throws java.io.IOException
        Compares two ZIP entries (byte-by-byte). .
        Parameters:
        zf1 - first ZIP file.
        zf2 - second ZIP file.
        path1 - name of the first entry.
        path2 - name of the second entry.
        Returns:
        true if the contents of the entries were same.
        Throws:
        java.io.IOException
      • closeQuietly

        public static void closeQuietly​(java.util.zip.ZipFile zf)
        Closes the ZIP file while ignoring any errors.
        Parameters:
        zf - ZIP file to be closed.
      • operateInPlace

        private static boolean operateInPlace​(java.io.File src,
                                              ZipUtil.InPlaceAction action)
        This method provides a general infrastructure for in-place operations. It creates temp file as a destination, then invokes the action on source and destination. Then it copies the result back into src file.
        Parameters:
        src - - source zip file we want to modify
        action - - action which actually modifies the archives
        Returns:
        result of the action