Package adams.core.io

Class ZipUtils


  • public class ZipUtils
    extends Object
    A helper class for ZIP-file related tasks.
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    • Field Detail

      • LOGGER

        protected static Logger LOGGER
        for logging errors.
    • Constructor Detail

      • ZipUtils

        public ZipUtils()
    • Method Detail

      • compress

        public static String compress​(File output,
                                      File[] files)
        Creates a zip file from the specified files.
        Parameters:
        output - the output file to generate
        files - the files to store in the zip file
        Returns:
        null if successful, otherwise error message
      • compress

        public static String compress​(File output,
                                      File[] files,
                                      int bufferSize)
        Creates a zip file from the specified files.
        Parameters:
        output - the output file to generate
        files - the files to store in the zip file
        bufferSize - the buffer size to use
        Returns:
        null if successful, otherwise error message
      • compress

        public static String compress​(File output,
                                      File[] files,
                                      String stripRegExp,
                                      int bufferSize)
        Creates a zip file from the specified files.
        Parameters:
        output - the output file to generate
        files - the files to store in the zip file
        stripRegExp - the regular expression used to strip the file names (only applied to the directory!)
        bufferSize - the buffer size to use
        Returns:
        null if successful, otherwise error message
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir)
        Unzips the files in a ZIP file. Does not recreate the directory structure stored in the ZIP file.
        Parameters:
        input - the ZIP file to unzip
        outputDir - the directory where to store the extracted files
        Returns:
        the successfully extracted files
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir,
                                            boolean createDirs)
        Unzips the files in a ZIP file.
        Parameters:
        input - the ZIP file to unzip
        outputDir - the directory where to store the extracted files
        createDirs - whether to re-create the directory structure from the ZIP file
        Returns:
        the successfully extracted files
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir,
                                            boolean createDirs,
                                            BaseRegExp match,
                                            boolean invertMatch)
        Unzips the files in a ZIP file. Files can be filtered based on their filename, using a regular expression (the matching sense can be inverted).
        Parameters:
        input - the ZIP file to unzip
        outputDir - the directory where to store the extracted files
        createDirs - whether to re-create the directory structure from the ZIP file
        match - the regular expression that the files are matched against
        invertMatch - whether to invert the matching sense
        Returns:
        the successfully extracted files
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir,
                                            boolean createDirs,
                                            BaseRegExp match,
                                            boolean invertMatch,
                                            int bufferSize)
        Unzips the files in a ZIP file. Files can be filtered based on their filename, using a regular expression (the matching sense can be inverted).
        Parameters:
        input - the ZIP file to unzip
        outputDir - the directory where to store the extracted files
        createDirs - whether to re-create the directory structure from the ZIP file
        match - the regular expression that the files are matched against
        invertMatch - whether to invert the matching sense
        bufferSize - the buffer size to use
        Returns:
        the successfully extracted files
      • decompress

        public static List<File> decompress​(File input,
                                            File outputDir,
                                            boolean createDirs,
                                            BaseRegExp match,
                                            boolean invertMatch,
                                            int bufferSize,
                                            MessageCollection errors)
        Unzips the files in a ZIP file. Files can be filtered based on their filename, using a regular expression (the matching sense can be inverted).
        Parameters:
        input - the ZIP file to unzip
        outputDir - the directory where to store the extracted files
        createDirs - whether to re-create the directory structure from the ZIP file
        match - the regular expression that the files are matched against
        invertMatch - whether to invert the matching sense
        bufferSize - the buffer size to use
        errors - for storing potential errors
        Returns:
        the successfully extracted files
      • decompress

        public static boolean decompress​(File input,
                                         String archiveFile,
                                         File output)
        Unzips the specified file from a ZIP file. Does not create any directories in case the parent directories of "output" don't exist yet.
        Parameters:
        input - the ZIP file to unzip
        archiveFile - the file from the archive to extract
        output - the name of the output file
        Returns:
        whether file was successfully extracted
      • decompress

        public static boolean decompress​(File input,
                                         String archiveFile,
                                         File output,
                                         boolean createDirs)
        Unzips the specified file from a ZIP file.
        Parameters:
        input - the ZIP file to unzip
        archiveFile - the file from the archive to extract
        output - the name of the output file
        createDirs - whether to create the directory structure represented by output file
        Returns:
        whether file was successfully extracted
      • decompress

        public static boolean decompress​(File input,
                                         String archiveFile,
                                         File output,
                                         boolean createDirs,
                                         int bufferSize,
                                         MessageCollection errors)
        Unzips the specified file from a ZIP file.
        Parameters:
        input - the ZIP file to unzip
        archiveFile - the file from the archive to extract
        output - the name of the output file
        createDirs - whether to create the directory structure represented by output file
        bufferSize - the buffer size to use
        errors - for storing potential errors
        Returns:
        whether file was successfully extracted
      • listFiles

        public static List<File> listFiles​(File input)
        Lists the files stored in the ZIP file. Lists directories automatically.
        Parameters:
        input - the ZIP file to obtain the file list from
        Returns:
        the stored files
      • listFiles

        public static List<File> listFiles​(File input,
                                           boolean listDirs)
        Lists the files stored in the ZIP file.
        Parameters:
        input - the ZIP file to obtain the file list from
        listDirs - whether to include directories in the list
        Returns:
        the stored files
      • isZipCompressed

        public static boolean isZipCompressed​(File file)
        Checks whether the file is zip compressed. See: https://en.wikipedia.org/wiki/ZIP_(file_format)#File_headers
        Parameters:
        file - the file to inspect
        Returns:
        true if gzip
      • isZipCompressed

        public static boolean isZipCompressed​(byte[] data)
        Checks whether the array is zip compressed. See: https://en.wikipedia.org/wiki/ZIP_(file_format)#File_headers
        Parameters:
        data - the data to inspect
        Returns:
        true if gzip