Class ArchiveUtil


  • public abstract class ArchiveUtil
    extends BaseArchiveUtil
    Provides utils to archive and extract files from zip archive
    • Constructor Summary

      Constructors 
      Constructor Description
      ArchiveUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      static <T> T doInArchive​(ArchiveUtil.ArchiveFileDescriptor archive, java.lang.String path, ArchiveUtil.DoInArchiveHandler<T> callback)
      Search for entry with specified path in archive and calls callable.
      static <T> T doInArchive​(ArchiveUtil.ArchiveFileDescriptor archive, java.lang.String path, ArchiveUtil.DoInArchiveHandler2<T> callable)
      Similar to #doInArchive(ArchiveFileDescriptor, String, DoInArchiveHandler) but callback MUST close Closeable elements (third argument)
      static java.io.InputStream extractEntry​(java.util.zip.ZipInputStream input, java.lang.String path)
      Extracts and returns the input stream for the file specified inside a zip stream.
      static java.util.Collection<ArchiveUtil.ArchivedFileInfo> getArchivedFolderEntries​(ArchiveUtil.ArchiveFileDescriptor archive, java.lang.String innerPathToFolder)
      Returns archived files info for entries in specified archive.
      static ArchiveInputStream getArchiveInputStream​(java.lang.String name, java.io.InputStream inputStream)
      Returns the ArchiveInputStream for the specified name of the resource.
      static ArchiveInputStream getArchiveInputStream​(ArchiveType archiveType, java.io.InputStream inputStream)
      Returns the ArchiveInputStream for the specified archiveType.
      static ArchiveType getArchiveType​(java.lang.String name)
      Determines the archive type based on a file name.
      static java.io.File getPackedFile​(java.io.File file)
      Adds ".gz" to the file name
      static java.lang.String getPackedFileName​(java.lang.String fileName)
      adds ".gz" to the given file name
      static java.lang.String getParentPath​(java.lang.String path)
      Similar to PathUtil#getParentPath(String) but support "!/" path separator and only forward slashes allowed.
      static boolean isNameAllowed​(java.lang.String entryName)  
      static boolean isPackedFile​(java.io.File file)  
      static boolean isPackedFileName​(java.lang.String fileName)  
      static boolean isZipContainsEntry​(java.io.File zip, java.lang.String relativePath)  
      static byte[] packBytes​(byte[] unpacked)  
      static java.io.File packFile​(java.io.File srcFile)
      Creates a packed file "file.ext.gz" from the given file "file.ext".
      static void packFileTo​(java.io.File dstFile, java.io.File srcFile)
      Packs the content of the source file to the destination file.
      static void packStream​(java.io.OutputStream out, java.io.InputStream in)
      Packs the entire content of the input stream and puts it into the output stream.
      static boolean packToZip​(java.io.File root, java.io.FilenameFilter filter, java.util.zip.ZipOutputStream output, boolean closeZip)  
      static boolean packZip​(java.io.File destFile, java.io.FilenameFilter filter, java.util.Collection<java.io.File> sourceFiles)
      Packs files and folders to a zip archive.
      static boolean packZip​(java.io.File root, java.io.FilenameFilter filter, java.util.zip.ZipOutputStream output)
      Same as packZip(java.io.File, java.util.zip.ZipOutputStream), but allows to specify the filename filter additionally.
      static boolean packZip​(java.io.File destFile, java.util.Collection<java.io.File> sourceFiles)  
      static boolean packZip​(java.io.File root, java.util.zip.ZipOutputStream output)
      Packs a file (or a directory) to a zip archive.
      static boolean packZip​(java.io.OutputStream destStream, java.util.Collection<java.io.File> sourceFiles)  
      static java.util.List<java.lang.String> splitByArchivePathSeparator​(java.lang.String path, boolean withEmptyLast)
      Splits path by #ARCHIVE_PATH_SEPARATOR ("!/").
      static byte[] unpackBytes​(byte[] packed)  
      static void unpackStream​(java.io.OutputStream out, java.io.InputStream in)
      Unpacks the entire content of the input stream and puts it into the output stream.
      static boolean unpackZip​(java.io.File zip, java.io.File targetDir)  
      static boolean unpackZip​(java.io.File zip, java.lang.String pathPrefix, java.io.File targetDir)
      Extract files from zip archive
      static boolean unpackZip​(java.util.zip.ZipInputStream input, java.io.File targetDir)
      Deprecated.
      input stream should be ZipSlickAwareZipInputStream
      • Methods inherited from class java.lang.Object

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

      • DEFAULT_ZIP_ENCODING

        public static final java.lang.String DEFAULT_ZIP_ENCODING
        See Also:
        Constant Field Values
      • ARCHIVE_PATH_SEPARATOR

        public static final java.lang.String ARCHIVE_PATH_SEPARATOR
        Since:
        8.0
        See Also:
        Constant Field Values
      • ARCHIVE_PATH_SEP_PATTERN

        public static final java.util.regex.Pattern ARCHIVE_PATH_SEP_PATTERN
    • Constructor Detail

      • ArchiveUtil

        public ArchiveUtil()
    • Method Detail

      • getArchiveType

        @NotNull
        public static ArchiveType getArchiveType​(@NotNull
                                                 java.lang.String name)
        Determines the archive type based on a file name.

        Currently the following extensions are supported:

        • .zip, .nupkg, .snupkg, .sit
        • .jar, .war, .ear, .apk
        • .tar.gz, .tgz, .tar.gzip
        • .tar
        Parameters:
        name - the file or resource name
        Returns:
        archive type
      • getArchiveInputStream

        @Nullable
        public static ArchiveInputStream getArchiveInputStream​(@NotNull
                                                               ArchiveType archiveType,
                                                               @NotNull
                                                               java.io.InputStream inputStream)
                                                        throws java.io.IOException
        Returns the ArchiveInputStream for the specified archiveType.
        !!!!NOTE. Calling this method for unknown file type may lead to stream leak !!!!
        Parameters:
        archiveType - the type of archive
        inputStream - the input that should be read
        Returns:
        the best possible ArchiveInputStream, or null if nothing matches.
        Throws:
        java.io.IOException - if I/O error occurs
      • getArchiveInputStream

        @Nullable
        public static ArchiveInputStream getArchiveInputStream​(@NotNull
                                                               java.lang.String name,
                                                               @NotNull
                                                               java.io.InputStream inputStream)
                                                        throws java.io.IOException
        Returns the ArchiveInputStream for the specified name of the resource.
        !!!!NOTE. Calling this method for unknown file type may lead to stream leak !!!!
        Parameters:
        name - the name of the resource
        inputStream - the input that should be read
        Returns:
        the best possible ArchiveInputStream, or null if nothing matches.
        Throws:
        java.io.IOException - if I/O error occurs
      • unpackZip

        public static boolean unpackZip​(@NotNull
                                        java.io.File zip,
                                        @NotNull
                                        java.lang.String pathPrefix,
                                        @NotNull
                                        java.io.File targetDir)
        Extract files from zip archive
        Parameters:
        zip - .zip file to extract
        pathPrefix - path prefix to extract from zip. Use "" to extract all files
        targetDir - target folder to extract
        Returns:
        true if operation succeeded, false otherwise
      • unpackZip

        public static boolean unpackZip​(@NotNull
                                        java.io.File zip,
                                        @NotNull
                                        java.io.File targetDir)
      • unpackZip

        @Deprecated
        public static boolean unpackZip​(@NotNull
                                        java.util.zip.ZipInputStream input,
                                        @NotNull
                                        java.io.File targetDir)
        Deprecated.
        input stream should be ZipSlickAwareZipInputStream
        Extracts the compressed files from the zip input stream input and stores them in the targetDir.

        The input is closed in the end.

        Parameters:
        input - zip input stream
        targetDir - the target directory
        Returns:
        true in case of success, false otherwise
      • packZip

        public static boolean packZip​(@NotNull
                                      java.io.File destFile,
                                      @NotNull
                                      java.util.Collection<java.io.File> sourceFiles)
                               throws java.io.FileNotFoundException
        Throws:
        java.io.FileNotFoundException
      • packZip

        public static boolean packZip​(@NotNull
                                      java.io.File destFile,
                                      @Nullable
                                      java.io.FilenameFilter filter,
                                      @NotNull
                                      java.util.Collection<java.io.File> sourceFiles)
                               throws java.io.FileNotFoundException
        Packs files and folders to a zip archive. In case of directory all inner files are added recursively.
        Parameters:
        destFile - destination zip file
        filter - file name filter
        sourceFiles - files or folders to zip
        Returns:
        true in case of success, false otherwise
        Throws:
        java.io.FileNotFoundException - if destFile exists but is a directory rather than a regular file, does not exist but cannot be created, or cannot be opened for any other reason
      • packZip

        public static boolean packZip​(@NotNull
                                      java.io.OutputStream destStream,
                                      @NotNull
                                      java.util.Collection<java.io.File> sourceFiles)
      • packZip

        public static boolean packZip​(@NotNull
                                      java.io.File root,
                                      @NotNull
                                      java.util.zip.ZipOutputStream output)
        Packs a file (or a directory) to a zip archive. In case of directory all inner files are added recursively.

        Files and directories have relative names, for instance, for the following tree:

         foo/bar/File1
                /File2
                /baz/File3
         
        the call packZip(barDirectory, output) creates a zip with "/File1", "/File2" and "/baz/File3" files, and calls packZip(bazDirectory, output) and packZip(file3, output) both create a zip with a single file "/File3".

        Output stream is closed in the end.

        Parameters:
        root - root file or directory
        output - the zip stream to write to
        Returns:
        true in case of success, false otherwise
      • packZip

        public static boolean packZip​(@NotNull
                                      java.io.File root,
                                      @Nullable
                                      java.io.FilenameFilter filter,
                                      @NotNull
                                      java.util.zip.ZipOutputStream output)
        Same as packZip(java.io.File, java.util.zip.ZipOutputStream), but allows to specify the filename filter additionally.
        Parameters:
        root - root file or directory
        filter - file name filter
        output - the zip stream to write to
        Returns:
        true in case of success, false otherwise
      • packToZip

        public static boolean packToZip​(@NotNull
                                        java.io.File root,
                                        @Nullable
                                        java.io.FilenameFilter filter,
                                        @NotNull
                                        java.util.zip.ZipOutputStream output,
                                        boolean closeZip)
      • packStream

        public static void packStream​(@NotNull
                                      java.io.OutputStream out,
                                      @NotNull
                                      java.io.InputStream in)
                               throws java.io.IOException
        Packs the entire content of the input stream and puts it into the output stream. The caller is responsible for closing both streams.
        Parameters:
        out - output stream
        in - input stream
        Throws:
        java.io.IOException
      • unpackStream

        public static void unpackStream​(@NotNull
                                        java.io.OutputStream out,
                                        @NotNull
                                        java.io.InputStream in)
                                 throws java.io.IOException
        Unpacks the entire content of the input stream and puts it into the output stream. The caller is responsible for closing both streams.
        Parameters:
        out - output stream
        in - input stream
        Throws:
        java.io.IOException
      • packBytes

        @Nullable
        public static byte[] packBytes​(@Nullable
                                       byte[] unpacked)
      • unpackBytes

        @Nullable
        public static byte[] unpackBytes​(@Nullable
                                         byte[] packed)
      • extractEntry

        @Nullable
        public static java.io.InputStream extractEntry​(@NotNull
                                                       java.util.zip.ZipInputStream input,
                                                       @NotNull
                                                       java.lang.String path)
                                                throws java.io.IOException
        Extracts and returns the input stream for the file specified inside a zip stream. If the file cannot be found among zip entries null is returned.

        Note: method does not close the input.

        Parameters:
        input - the zip input stream
        path - relative path of the file to extract
        Returns:
        the input stream of the file (uncompressed)
        Throws:
        java.io.IOException - if I/O error occurs
      • getPackedFile

        @NotNull
        public static java.io.File getPackedFile​(@NotNull
                                                 java.io.File file)
        Adds ".gz" to the file name
        Parameters:
        file - file
        Returns:
        new file in the same location
      • getPackedFileName

        @NotNull
        public static java.lang.String getPackedFileName​(@NotNull
                                                         java.lang.String fileName)
        adds ".gz" to the given file name
        Parameters:
        fileName - file name
        Returns:
        fileName + ".gz"
      • isPackedFile

        public static boolean isPackedFile​(@NotNull
                                           java.io.File file)
      • isPackedFileName

        public static boolean isPackedFileName​(@NotNull
                                               java.lang.String fileName)
      • packFile

        @NotNull
        public static java.io.File packFile​(@NotNull
                                            java.io.File srcFile)
                                     throws java.io.IOException
        Creates a packed file "file.ext.gz" from the given file "file.ext". If the target file exists before calling this method it will be overwritten.
        Parameters:
        srcFile - the given file
        Returns:
        the packed file
        Throws:
        java.io.IOException - if a problem occurs during file operations
      • packFileTo

        public static void packFileTo​(@NotNull
                                      java.io.File dstFile,
                                      @NotNull
                                      java.io.File srcFile)
                               throws java.io.IOException
        Packs the content of the source file to the destination file. If the destination file exists it will be overwritten.
        Parameters:
        dstFile - destination file
        srcFile - source file
        Throws:
        java.io.IOException - if a problem occurs during file operations
      • isZipContainsEntry

        public static boolean isZipContainsEntry​(java.io.File zip,
                                                 java.lang.String relativePath)
                                          throws java.io.IOException
        Throws:
        java.io.IOException
      • getArchivedFolderEntries

        @NotNull
        public static java.util.Collection<ArchiveUtil.ArchivedFileInfo> getArchivedFolderEntries​(@NotNull
                                                                                                  ArchiveUtil.ArchiveFileDescriptor archive,
                                                                                                  @NotNull
                                                                                                  java.lang.String innerPathToFolder)
                                                                                           throws java.io.IOException
        Returns archived files info for entries in specified archive. Supports inner archives.
        Parameters:
        archive - - a particular archive tp process
        innerPathToFolder - may contains "!/", e.g. "/", "/folder1/", "/folder/archive.zip!/", "/foo/archive.zip!/baz"
        Returns:
        see above
        Throws:
        java.io.IOException - if IOException occurs
        Since:
        8.0
      • splitByArchivePathSeparator

        @NotNull
        public static java.util.List<java.lang.String> splitByArchivePathSeparator​(@NotNull
                                                                                   java.lang.String path,
                                                                                   boolean withEmptyLast)
        Splits path by #ARCHIVE_PATH_SEPARATOR ("!/").
        Parameters:
        path - path to process
        withEmptyLast - is should include empty string into result if path ends with "!/"
        Returns:
        see above
        Since:
        8.0
      • doInArchive

        public static <T> T doInArchive​(@NotNull
                                        ArchiveUtil.ArchiveFileDescriptor archive,
                                        @NotNull
                                        java.lang.String path,
                                        @NotNull
                                        ArchiveUtil.DoInArchiveHandler<T> callback)
                                 throws java.io.IOException
        Search for entry with specified path in archive and calls callable. Support internal archives Sample paths: "a.txt", "foo/baz.txt", "foo/bar.zip!/baz.txt"
        Type Parameters:
        T - callback return type
        Parameters:
        archive - - a particular archive to search in
        path - - path to search for, e.g. "a.txt", "foo/baz.txt", "foo/bar.zip!/baz.txt"
        callback - function called on entry found
        Returns:
        what callback returns
        Throws:
        java.io.FileNotFoundException - if entry not found
        java.io.IOException - if archive processing error or callback returns error or failed to get input stream from archive
        Since:
        8.0
      • doInArchive

        public static <T> T doInArchive​(@NotNull
                                        ArchiveUtil.ArchiveFileDescriptor archive,
                                        @NotNull
                                        java.lang.String path,
                                        @NotNull
                                        ArchiveUtil.DoInArchiveHandler2<T> callable)
                                 throws java.io.IOException
        Similar to #doInArchive(ArchiveFileDescriptor, String, DoInArchiveHandler) but callback MUST close Closeable elements (third argument)
        Throws:
        java.io.IOException
        Since:
        8.0
      • getParentPath

        @NotNull
        public static java.lang.String getParentPath​(@NotNull
                                                     java.lang.String path)
        Similar to PathUtil#getParentPath(String) but support "!/" path separator and only forward slashes allowed.
        Parameters:
        path - path to process.
        Returns:
        parent path
        Since:
        8.0
      • isNameAllowed

        public static boolean isNameAllowed​(@Nullable
                                            java.lang.String entryName)