Class Zips


  • public class Zips
    extends java.lang.Object
    Utility class to work with Zip files (Path based).
    • Constructor Summary

      Constructors 
      Constructor Description
      Zips()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.nio.file.Path checkPathExists​(java.nio.file.Path source, java.lang.String msg)  
      private static org.apache.commons.compress.archivers.zip.ZipArchiveEntry createArchiveEntry​(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.lang.String entryName)  
      private static java.lang.String entryNameFrom​(java.nio.file.Path path, boolean isDirectoryw)  
      private static void fixPosixPermissions​(org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry, java.nio.file.Path entryPath)  
      private static java.io.BufferedOutputStream newBufferedOutputStream​(java.nio.file.Path path)  
      private static int packEntries​(java.nio.file.Path source, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, boolean preserveRoot, java.util.Set<java.nio.file.Path> pathToExcludes)  
      static int packJar​(java.nio.file.Path source, java.nio.file.Path targetJar, boolean preserveRoot)
      Zip the given source file or folder in the targetJar Jar file.
      private static java.util.Set<java.nio.file.Path> packManifestIfAny​(java.nio.file.Path source, org.apache.commons.compress.archivers.jar.JarArchiveOutputStream jos)
      Looks for META-INF/MANIFEST.MF file in the given source folder and add them as entries to the JarOutputStream.
      static int packZip​(java.nio.file.Path source, java.nio.file.Path targetZip, boolean preserveRoot)
      Zip the given source file or folder in the targetZip Zip file.
      private static void putDirectoryEntry​(java.nio.file.Path dir, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.nio.file.Path entryPath)  
      private static void putFileEntry​(java.nio.file.Path file, org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos, java.nio.file.Path entryPath)  
      private static void setPermissions​(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry, java.nio.file.Path entryPath)  
      (package private) static int unpack​(org.apache.commons.compress.archivers.tar.TarArchiveInputStream zis, java.nio.file.Path outputDir)  
      private static int unpack​(org.apache.commons.compress.archivers.zip.ZipFile zipFile, java.nio.file.Path outputDir, java.util.function.BiConsumer<org.apache.commons.compress.archivers.zip.ZipArchiveEntry,​java.nio.file.Path> entryFixer)  
      static int unpackJar​(java.nio.file.Path source, java.nio.file.Path outputDir)
      Unzip the given source Jar file in the outputDir.
      static int unpackTarGz​(java.nio.file.Path sourcePath, java.nio.file.Path outputDir)  
      static int unpackZip​(java.nio.file.Path source, java.nio.file.Path outputDir)
      Unzip the given source Zip file in the outputDir.
      private static java.nio.file.Path unpackZipEntry​(org.apache.commons.compress.archivers.zip.ZipFile zipFile, org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry, java.nio.file.Path outputDir)  
      • Methods inherited from class java.lang.Object

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

      • ZIP_ENTRY_NAME_SEPARATOR

        private static final java.lang.String ZIP_ENTRY_NAME_SEPARATOR
        See Also:
        Constant Field Values
      • BACKSLASH_ESCAPE_REPLACEMENT

        private static final java.lang.String BACKSLASH_ESCAPE_REPLACEMENT
        See Also:
        Constant Field Values
      • BACKSLASH_PATTERN

        private static final java.util.regex.Pattern BACKSLASH_PATTERN
    • Constructor Detail

      • Zips

        public Zips()
    • Method Detail

      • unpackZip

        public static int unpackZip​(java.nio.file.Path source,
                                    java.nio.file.Path outputDir)
                             throws java.io.IOException
        Unzip the given source Zip file in the outputDir.
        Parameters:
        source - the file to unzip.
        outputDir - the output directory where the Zip will be unpacked.
        Returns:
        the number of unpacked entries
        Throws:
        java.io.IOException
      • fixPosixPermissions

        private static void fixPosixPermissions​(org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry,
                                                java.nio.file.Path entryPath)
      • unpack

        private static int unpack​(org.apache.commons.compress.archivers.zip.ZipFile zipFile,
                                  java.nio.file.Path outputDir,
                                  java.util.function.BiConsumer<org.apache.commons.compress.archivers.zip.ZipArchiveEntry,​java.nio.file.Path> entryFixer)
                           throws java.io.IOException,
                                  java.util.zip.ZipException
        Throws:
        java.io.IOException
        java.util.zip.ZipException
      • unpackZipEntry

        private static java.nio.file.Path unpackZipEntry​(org.apache.commons.compress.archivers.zip.ZipFile zipFile,
                                                         org.apache.commons.compress.archivers.zip.ZipArchiveEntry entry,
                                                         java.nio.file.Path outputDir)
                                                  throws java.io.IOException,
                                                         java.util.zip.ZipException
        Throws:
        java.io.IOException
        java.util.zip.ZipException
      • unpackJar

        public static int unpackJar​(java.nio.file.Path source,
                                    java.nio.file.Path outputDir)
                             throws java.io.IOException
        Unzip the given source Jar file in the outputDir.
        Parameters:
        source - the file to unzip.
        outputDir - the output directory where the Jar will be unpacked. It does not have to exist beforehand.
        Returns:
        the number of unpacked entries
        Throws:
        java.io.IOException
      • unpackTarGz

        public static int unpackTarGz​(java.nio.file.Path sourcePath,
                                      java.nio.file.Path outputDir)
                               throws java.io.IOException
        Throws:
        java.io.IOException
      • unpack

        static int unpack​(org.apache.commons.compress.archivers.tar.TarArchiveInputStream zis,
                          java.nio.file.Path outputDir)
                   throws java.io.IOException
        Throws:
        java.io.IOException
      • setPermissions

        private static void setPermissions​(org.apache.commons.compress.archivers.tar.TarArchiveEntry entry,
                                           java.nio.file.Path entryPath)
                                    throws java.io.IOException
        Throws:
        java.io.IOException
      • packZip

        public static int packZip​(java.nio.file.Path source,
                                  java.nio.file.Path targetZip,
                                  boolean preserveRoot)
                           throws java.io.IOException
        Zip the given source file or folder in the targetZip Zip file. If preserveRoot is set to true, the output Zip will contain the folder and its contents, only its contents otherwise.
        Parameters:
        source - the folder to zip.
        targetZip - the Zip file to create or overwrite.
        preserveRoot - whether the source folder should be kept in the target Zip.
        Returns:
        the number of packed entries
        Throws:
        java.io.IOException
      • packJar

        public static int packJar​(java.nio.file.Path source,
                                  java.nio.file.Path targetJar,
                                  boolean preserveRoot)
                           throws java.io.IOException
        Zip the given source file or folder in the targetJar Jar file. If preserveRoot is set to true, the output Zip will contain the folder and its contents, only its contents otherwise.
        Parameters:
        source - the folder to zip.
        targetJar - the Jar file to create or overwrite.
        preserveRoot - whether the source folder should be kept in the target Jar.
        Returns:
        the number of packed entries
        Throws:
        java.io.IOException
      • packManifestIfAny

        private static java.util.Set<java.nio.file.Path> packManifestIfAny​(java.nio.file.Path source,
                                                                           org.apache.commons.compress.archivers.jar.JarArchiveOutputStream jos)
                                                                    throws java.io.IOException
        Looks for META-INF/MANIFEST.MF file in the given source folder and add them as entries to the JarOutputStream.
        Parameters:
        source - the folder to jar. If not a directory, returns 0 and do nothing
        preserveRoot - whether the root folder
        jos - the jar output stream to write
        Returns:
        set of paths to the META-INF and MANIFEST.MF, empty set otherwise.
        Throws:
        java.io.IOException
      • checkPathExists

        private static java.nio.file.Path checkPathExists​(java.nio.file.Path source,
                                                          java.lang.String msg)
      • newBufferedOutputStream

        private static java.io.BufferedOutputStream newBufferedOutputStream​(java.nio.file.Path path)
                                                                     throws java.io.IOException
        Throws:
        java.io.IOException
      • packEntries

        private static int packEntries​(java.nio.file.Path source,
                                       org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos,
                                       boolean preserveRoot,
                                       java.util.Set<java.nio.file.Path> pathToExcludes)
                                throws java.io.IOException
        Throws:
        java.io.IOException
      • entryNameFrom

        private static java.lang.String entryNameFrom​(java.nio.file.Path path,
                                                      boolean isDirectoryw)
      • putFileEntry

        private static void putFileEntry​(java.nio.file.Path file,
                                         org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos,
                                         java.nio.file.Path entryPath)
                                  throws java.io.IOException
        Throws:
        java.io.IOException
      • putDirectoryEntry

        private static void putDirectoryEntry​(java.nio.file.Path dir,
                                              org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos,
                                              java.nio.file.Path entryPath)
                                       throws java.io.IOException
        Throws:
        java.io.IOException
      • createArchiveEntry

        private static org.apache.commons.compress.archivers.zip.ZipArchiveEntry createArchiveEntry​(org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream zos,
                                                                                                    java.lang.String entryName)