Class Unzip


  • public class Unzip
    extends java.lang.Object
    Provides several static utilities for use when unzipping files from an archive.
    • Constructor Summary

      Constructors 
      Constructor Description
      Unzip()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean findFileInsideInnerZip​(java.lang.String filename, java.lang.String zipfile, java.lang.String innerZipFile)
      Looks for a file that is in a zip within the main zip file.
      static java.lang.String findMatchingFileSpecInZip​(java.lang.String spec, java.lang.String zipfile)
      Tells if a file matching the given regular expression is contained in a zip archive.
      static java.lang.String getDefaultExtractionLocation()
      Gets the default extraction location.
      static java.io.InputStream getInputStreamFromFileInArchive​(java.lang.String filename, java.util.zip.ZipFile zf, java.lang.String zipfile)
      Gets an input stream associated with a file inside a zip archive so it can be subsequently read.
      static boolean isFileInArchive​(java.lang.String filename, java.lang.String zipfile)
      Tells if a file with the given name is contained in a zip archive.
      static boolean isUseCEAlg()
      Get the value of the useCEAlg flag
      static void listArchive​(java.lang.String zipfile)
      Lists files in an archive.
      static void setDefaultExtractionLocation​(java.lang.String loc)
      Sets the extraction location.
      static void setUseCEAlg​(boolean useCEAlg)
      Set whether or not to use the CE algorithm
      static java.lang.String[] unzipAndReadSingleFile​(java.lang.String filename, java.lang.String zipfile)
      Reads a file in a zip archive without unzipping it.
      static java.lang.String[] unzipSingleBinaryFile​(java.lang.String filename, java.lang.String zipfile, boolean isText)
      Unzips a file from a specified archive.
      static java.lang.String[] unzipSingleBinaryFile​(java.lang.String filename, java.lang.String zipfile, boolean isText, java.lang.String target_filename)
      Unzips a file from a specified archive with the same name as contained in the archive.
      static int unzipToDirectory​(java.lang.String filename, java.lang.String directory, java.lang.String backupDir)
      Unzips an entire zip archive to a given directory and optionally backs up replaced files.
      static int unzipToDirectory​(java.lang.String filename, java.lang.String directory, java.lang.String backupDir, java.lang.String[] filestoExclude)
      Unzips an entire zip archive to a given directory and optionally backs up replaced files.
      static void writeNewFiles​(java.lang.String backupDir)  
      • Methods inherited from class java.lang.Object

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

      • Unzip

        public Unzip()
    • Method Detail

      • setDefaultExtractionLocation

        public static void setDefaultExtractionLocation​(java.lang.String loc)
        Sets the extraction location.
        Parameters:
        loc - The location to extract files
      • getDefaultExtractionLocation

        public static java.lang.String getDefaultExtractionLocation()
        Gets the default extraction location.
        Returns:
        The default location to extract files
      • setUseCEAlg

        public static void setUseCEAlg​(boolean useCEAlg)
        Set whether or not to use the CE algorithm
        Parameters:
        useCEAlg - true or false
      • isUseCEAlg

        public static boolean isUseCEAlg()
        Get the value of the useCEAlg flag
        Returns:
        true or false
      • unzipToDirectory

        public static int unzipToDirectory​(java.lang.String filename,
                                           java.lang.String directory,
                                           java.lang.String backupDir)
        Unzips an entire zip archive to a given directory and optionally backs up replaced files. If the CE algorithm for replacing and backing up files should be used, a call to setUseCEAlg(true) should have been made previously and a call to setUseCEAlg(false) should be made afterwards.
        Parameters:
        filename - Name of the zip archive
        directory - Destination for extracted files from the archive
        backupDir - Directory to place backed up files or null if no backup should occur
      • unzipToDirectory

        public static int unzipToDirectory​(java.lang.String filename,
                                           java.lang.String directory,
                                           java.lang.String backupDir,
                                           java.lang.String[] filestoExclude)
        Unzips an entire zip archive to a given directory and optionally backs up replaced files. If the CE algorithm for replacing and backing up files should be used, a call to setUseCEAlg(true) should have been made previously and a call to setUseCEAlg(false) should be made afterwards. Also excludes list of files which need not to be unzipped.
        Parameters:
        filename - Name of the zip archive
        directory - Destination for extracted files from the archive
        backupDir - Directory to place backed up files or null if no backup should occur
        filestoExclude - List of files which need NOT to be unzipped from zip.
      • unzipAndReadSingleFile

        public static java.lang.String[] unzipAndReadSingleFile​(java.lang.String filename,
                                                                java.lang.String zipfile)
        Reads a file in a zip archive without unzipping it. This method is only intended to be used on text files.
        Parameters:
        filename - Filename inside of the archive to read
        zipfile - Name of the zip archive which contains the file to read
        Returns:
        String[] Lines of the target file
      • getInputStreamFromFileInArchive

        public static java.io.InputStream getInputStreamFromFileInArchive​(java.lang.String filename,
                                                                          java.util.zip.ZipFile zf,
                                                                          java.lang.String zipfile)
        Gets an input stream associated with a file inside a zip archive so it can be subsequently read.
        Parameters:
        filename - Filename inside of the archive
        zipfile - Name of the zip archive which contains the file
        Returns:
        An input stream representing the file to be read
      • isFileInArchive

        public static boolean isFileInArchive​(java.lang.String filename,
                                              java.lang.String zipfile)
        Tells if a file with the given name is contained in a zip archive.
        Parameters:
        filename - Filename to search for in the zip archive
        zipfile - Name of the zip archive
        Returns:
        true if a file with the specified name is in the archive, false otherwise
      • findMatchingFileSpecInZip

        public static java.lang.String findMatchingFileSpecInZip​(java.lang.String spec,
                                                                 java.lang.String zipfile)
        Tells if a file matching the given regular expression is contained in a zip archive.
        Parameters:
        spec - A regular expression indicating a file to search for in a zip archive
        zipfile - Name of the zip archive
        Returns:
        Name of the first matching file or the empty string if not found
      • unzipSingleBinaryFile

        public static java.lang.String[] unzipSingleBinaryFile​(java.lang.String filename,
                                                               java.lang.String zipfile,
                                                               boolean isText,
                                                               java.lang.String target_filename)
        Unzips a file from a specified archive with the same name as contained in the archive. The file can be optionally read into a buffer.
        Parameters:
        filename - Name of the file to extract
        zipfile - Name of the zip archive
        isText - Indicates whether or not the file should be treated as text and read into a buffer
        target_filename - Name of the file this archive should be extracted as. Normally, this should match the original filename
        Returns:
        Lines of the file as an array if isText was true, null otherwise
      • unzipSingleBinaryFile

        public static java.lang.String[] unzipSingleBinaryFile​(java.lang.String filename,
                                                               java.lang.String zipfile,
                                                               boolean isText)
        Unzips a file from a specified archive. The file can be optionally read into a buffer.
        Parameters:
        filename - Name of the file to extract
        zipfile - Name of the zip archive
        isText - Indicates whether or not the file should be treated as text and read into a buffer
        Returns:
        Lines of the file as an array if isText was true, null otherwise
      • listArchive

        public static void listArchive​(java.lang.String zipfile)
        Lists files in an archive. (DEBUG ONLY)
        Parameters:
        zipfile - Name of the zip archive
      • findFileInsideInnerZip

        public static boolean findFileInsideInnerZip​(java.lang.String filename,
                                                     java.lang.String zipfile,
                                                     java.lang.String innerZipFile)
        Looks for a file that is in a zip within the main zip file. This function basically stops short of doing a full recursive search for a file, but could be expanded to do so.
        Parameters:
        filename - Name of the file to search for
        zipfile - Name of the zip archive
        innerZipFile - Name of the zip within the zip file to check archive
        Returns:
        true or false indicating whether or not the file was found
      • writeNewFiles

        public static void writeNewFiles​(java.lang.String backupDir)