Class ZipUtil


  • public class ZipUtil
    extends Object
    • Constructor Detail

      • ZipUtil

        public ZipUtil()
    • Method Detail

      • safeUnzip

        public static void safeUnzip​(ZipInputStream zis,
                                     Path unzipDirectory)
                              throws IOException
        Extract a ZipInputStream to the target destination directory. Does not allow malicious ZIP packages to escape from the prescribed directory tree.
        Parameters:
        zis -
        unzipDirectory -
        Throws:
        IOException
      • safeUnzip

        public static void safeUnzip​(Path zipFile,
                                     Path unzipDirectory)
                              throws IOException
        Convenience method.
        Parameters:
        zipFile -
        unzipDirectory -
        Throws:
        IOException
      • zipDirectory

        public static void zipDirectory​(String sourceDir,
                                        String zipExtension)
        Compresses a given directory. Creates in the same parent folder a ZIP file with the folder name as the file name and a given extension. The given directory is not deleted after compression.

        This method uses the Java ZIP package and does not supports files to zip that have a path with extended characters.

        Parameters:
        sourceDir - the given directory to be compressed
        zipExtension - an extension for the output ZIP file (default is .zip if a null or empty string is passed by the caller). The extension is expected to contain the leading period.
      • zipDirectory

        public static void zipDirectory​(String sourceDir,
                                        String zipExtension,
                                        String destinationPathWithoutExtension)
        Compresses a given directory. The given directory is not deleted after compression.

        This method uses the Java ZIP package and does not supports files to zip that have a path with extended characters.

        Parameters:
        sourceDir - the given directory to be compressed
        zipExtension - an extension for the output ZIP file (default is .zip if a null or empty string is passed by the caller). The extension is expected to contain the leading period.
        destinationPathWithoutExtension - output path of the zip file, without extension. Use null to use the source directory path.
      • zipFiles

        public static void zipFiles​(String zipPath,
                                    String sourceDir,
                                    String... filenames)
        Creates a ZIP file and adds a list of files in it.
        Parameters:
        zipPath - the path of the ZIP file to create.
        sourceDir - the path of the directory where the source files are located.
        filenames - the list of files to zip.