Class ArchiveUtil


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

      • ArchiveUtil

        public ArchiveUtil()
    • Method Detail

      • getArchiveType

        @NotNull
        public static ArchiveType getArchiveType​(@NotNull
                                                 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 org.apache.commons.compress.archivers.ArchiveInputStream getArchiveInputStream​(@NotNull
                                                                                                     ArchiveType archiveType,
                                                                                                     @NotNull
                                                                                                     InputStream inputStream)
                                                                                              throws 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:
        IOException - if I/O error occurs
      • getArchiveInputStream

        @Nullable
        public static org.apache.commons.compress.archivers.ArchiveInputStream getArchiveInputStream​(@NotNull
                                                                                                     String name,
                                                                                                     @NotNull
                                                                                                     InputStream inputStream)
                                                                                              throws 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:
        IOException - if I/O error occurs
      • unpackZip

        public static boolean unpackZip​(@NotNull
                                        File zip,
                                        @NotNull
                                        String pathPrefix,
                                        @NotNull
                                        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
                                        File zip,
                                        @NotNull
                                        File targetDir)
      • unpackZip

        public static boolean unpackZip​(@NotNull
                                        ZipInputStream input,
                                        @NotNull
                                        File targetDir)
        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
      • unpackZipOrThrow

        public static void unpackZipOrThrow​(@NotNull
                                            File zip,
                                            @NotNull
                                            String pathPrefix,
                                            @NotNull
                                            File targetDir,
                                            boolean clearTargetDir)
                                     throws IOException
        Extracts files from zip archive and throws IOException in case there is an error.
        Parameters:
        zip - .zip file to extract
        pathPrefix - path prefix to extract from zip. Use "" to extract all files
        targetDir - target folder to extract
        clearTargetDir - indicates whether the target dir should be cleared before the unpacking and in case of an error
        Throws:
        IOException
      • packZip

        public static boolean packZip​(@NotNull
                                      File destFile,
                                      @Nullable
                                      FilenameFilter filter,
                                      @NotNull
                                      Collection<File> sourceFiles)
                               throws 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:
        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
                                      File root,
                                      @NotNull
                                      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 void packZip​(@NotNull
                                   Collection<File> sourceFiles,
                                   @NotNull
                                   File resultZip,
                                   @NotNull
                                   Map<String,​Integer> entitiesUnixPermissions,
                                   boolean markExecutableFiles)
                            throws IOException
        Assembles a zip archive setting Unix permissions for the archive entries.
        Parameters:
        sourceFiles - files to be archived
        resultZip - a path to the result zip
        entitiesUnixPermissions - a map from an archive entry path (relative to the archive root) to an integer that represents Unix permissions in the format used by ZipArchiveEntry.getUnixMode()
        markExecutableFiles - if true then files with extensions of executable files will be marked as executable
        Throws:
        IOException
      • packStream

        public static void packStream​(@NotNull
                                      OutputStream out,
                                      @NotNull
                                      InputStream in)
                               throws 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:
        IOException
      • unpackStream

        public static void unpackStream​(@NotNull
                                        OutputStream out,
                                        @NotNull
                                        InputStream in)
                                 throws 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:
        IOException
      • packBytes

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

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

        @Nullable
        public static InputStream extractEntry​(@NotNull
                                               ZipInputStream input,
                                               @NotNull
                                               String path)
                                        throws 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:
        IOException - if I/O error occurs
      • getPackedFile

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

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

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

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

        @NotNull
        public static File packFile​(@NotNull
                                    File srcFile)
                             throws 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:
        IOException - if a problem occurs during file operations
      • packFileTo

        public static void packFileTo​(@NotNull
                                      File dstFile,
                                      @NotNull
                                      File srcFile)
                               throws 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:
        IOException - if a problem occurs during file operations
      • getArchivedFolderEntries

        @NotNull
        public static Collection<ArchiveUtil.ArchivedFileInfo> getArchivedFolderEntries​(@NotNull
                                                                                        ArchiveUtil.ArchiveFileDescriptor archive,
                                                                                        @NotNull
                                                                                        String innerPathToFolder)
                                                                                 throws 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:
        IOException - if IOException occurs
        Since:
        8.0
      • splitByArchivePathSeparator

        @NotNull
        public static List<String> splitByArchivePathSeparator​(@NotNull
                                                               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
                                        String path,
                                        @NotNull
                                        ArchiveUtil.DoInArchiveHandler<T> callback)
                                 throws 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:
        FileNotFoundException - if entry not found
        IOException - if archive processing error or callback returns error or failed to get input stream from archive
        Since:
        8.0
      • getParentPath

        @NotNull
        public static String getParentPath​(@NotNull
                                           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
                                            String entryName)