Class ZipUtils


  • public final class ZipUtils
    extends Object
    Utility class to simplify accessing and creating zip files, and other packed formats.
    • Method Detail

      • unzipPathNames

        public static Set<String> unzipPathNames​(BeanByteSource source)
        Unzips the source returning the file names that are contained.

        The result is a set of relative path names within the zip. Only files are returned, not folders.

        Parameters:
        source - the byte source to unzip
        Returns:
        the set of relative path names
        Throws:
        UncheckedIOException - if an IO error occurs
      • unzipPathNameInMemory

        public static Optional<ArrayByteSource> unzipPathNameInMemory​(BeanByteSource source,
                                                                      String relativePathName)
        Unzips a single file from the source in memory.

        Callers can use unzipPathNames(BeanByteSource) to find the available names. The relative path name must match exactly that in the zip.

        Parameters:
        source - the byte source to unzip
        relativePathName - the exact relative path name that the file is stored as
        Returns:
        the unzipped file, empty if not found
        Throws:
        UncheckedIOException - if an IO error occurs
      • unzip

        public static void unzip​(BeanByteSource source,
                                 Path path)
        Unzips the source to a file path.

        Empty folders in the zip will not be created.

        Parameters:
        source - the byte source to unzip
        path - the path to unzip to
        Throws:
        UncheckedIOException - if an IO error occurs
        SecurityException - if the path is not absolute and the calling thread cannot access system property "user.dir"
      • zipInMemory

        public static ArrayByteSource zipInMemory​(List<? extends BeanByteSource> sources)
        Creates a zip file from the list of files in memory.

        Each source must have a file name. The output will not contain subfolders.

        Parameters:
        sources - the byte sources to zip
        Returns:
        the zip file
        Throws:
        UncheckedIOException - if an IO error occurs
      • unpackInMemory

        public static Map<String,​ArrayByteSource> unpackInMemory​(BeanByteSource source)
        Unpacks the source into memory, returning a map.

        This method loads the whole unpacked file into memory. Where possible, unpackInMemory(BeanByteSource, BiConsumer) should be preferred as it only loads files one at a time.

        Unpacking handles ZIP, GZ and BASE64 formats based entirely on the suffix of the input file name. If the input suffix is not recognized as a packed format, the consumer is invoked with the original file.

        Parameters:
        source - the byte source to unpack
        Returns:
        the map of unzipped byte sources, keyed by relative path name
        Throws:
        UncheckedIOException - if an IO error occurs
      • unpackInMemory

        public static void unpackInMemory​(BeanByteSource source,
                                          BiConsumer<String,​ArrayByteSource> consumer)
        Unpacks the source into memory, invoking the consumer for each entry.

        Unpacking handles ZIP, GZ and BASE64 formats based entirely on the suffix of the input file name. If the input suffix is not recognized as a packed format, the consumer is invoked with the original file. This method is not recursive.

        Parameters:
        source - the byte source to unpack
        consumer - the consumer, which is passed the relative path name and content for each entry
        Throws:
        UncheckedIOException - if an IO error occurs
      • decryptZip

        public static BeanByteSource decryptZip​(BeanByteSource source,
                                                String password)
        Provides a new source that decrypts the specified source ZIP.

        This returns a wrapper around the input source that provides decryption. The result is normally passed directly into one of the other methods on this class.

        Parameters:
        source - the byte source to unpack
        password - the password to decrypt the input
        Returns:
        the decrypted zip file