Class ZipUtil


  • public class ZipUtil
    extends java.lang.Object
    Zip/Unzip Utility
    Since:
    1.2
    • Constructor Summary

      Constructors 
      Constructor Description
      ZipUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addToZip​(java.io.File srcDir, java.io.FilenameFilter filter, java.lang.String namePrefix, java.util.zip.ZipOutputStream zos)
      Add all the files under the directory to the zip stream
      static void addToZip​(java.io.File file, java.lang.String entryName, java.util.zip.ZipOutputStream zos)
      Add a file to the zip
      static java.io.InputStream getEntryInputStream​(java.io.File zipFile, java.lang.String entryName)
      Get the inputStream of an entry in a zip file.
      static void unTarGzip​(java.io.File tarGzFile, java.io.File targetDir, NameMapper nameMapper, Progress progress)
      Decompress and untar the tar.gz file to the specified directory.
      static void unzip​(java.io.File zipFile, java.io.File targetDir, NameMapper nameMapper)
      Unzip a zip file to the specified directory.
      static void unzip​(java.io.File zipFile, java.io.File targetDir, NameMapper nameMapper, Progress progress)
      Unzip a zip file to the specified directory.
      static void unzip​(java.io.InputStream zipFileInputStream, java.io.File targetDir, NameMapper nameMapper)
      Unzip a zip file to the specified directory.
      static void zip​(java.io.File srcDir, java.io.FilenameFilter filter, java.lang.String namePrefix, java.io.File targetZipFile)
      Zip all the files under the directory.
      • Methods inherited from class java.lang.Object

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

      • ZipUtil

        public ZipUtil()
    • Method Detail

      • unzip

        public static void unzip​(java.io.File zipFile,
                                 java.io.File targetDir,
                                 NameMapper nameMapper)
                          throws java.io.IOException
        Unzip a zip file to the specified directory. If the target directory does not exist, it will be created.
        Parameters:
        zipFile - zip file
        targetDir - target directory to unzip file
        nameMapper - entry name mapper that is used to change file path name.
        Throws:
        java.io.IOException - throw if any IO error occurs
      • unzip

        public static void unzip​(java.io.InputStream zipFileInputStream,
                                 java.io.File targetDir,
                                 NameMapper nameMapper)
                          throws java.io.IOException
        Unzip a zip file to the specified directory. If the target directory does not exist, it will be created.
        Parameters:
        zipFileInputStream - zip file inputStream
        targetDir - target directory to unzip file
        nameMapper - entry name mapper that is used to change file path name.
        Throws:
        java.io.IOException - throw if any IO error occurs
      • unzip

        public static void unzip​(java.io.File zipFile,
                                 java.io.File targetDir,
                                 NameMapper nameMapper,
                                 Progress progress)
                          throws java.io.IOException
        Unzip a zip file to the specified directory. If the target directory does not exist, it will be created.
        Parameters:
        zipFile - zip file
        targetDir - target directory to unzip file
        nameMapper - entry name mapper that is used to change file path name.
        progress - the Progress object that receives progressing messages
        Throws:
        java.io.IOException - throw if any IO error occurs
      • getEntryInputStream

        public static java.io.InputStream getEntryInputStream​(java.io.File zipFile,
                                                              java.lang.String entryName)
                                                       throws java.io.IOException
        Get the inputStream of an entry in a zip file. The caller of this method is responsible for closing the inputStream.
        Parameters:
        zipFile - zip file
        entryName - entry name
        Returns:
        InputStream instance. return null if the entry is not found in the zip
        Throws:
        java.io.IOException - throw if an IO error occurs
      • zip

        public static void zip​(java.io.File srcDir,
                               java.io.FilenameFilter filter,
                               java.lang.String namePrefix,
                               java.io.File targetZipFile)
                        throws java.io.IOException
        Zip all the files under the directory.
        Parameters:
        srcDir - the directory to zip
        filter - file name filter
        namePrefix - the prefix to prepend to all entry names
        targetZipFile - the zip file to create
        Throws:
        java.io.IOException - throw if any IO error occurs
      • addToZip

        public static void addToZip​(java.io.File file,
                                    java.lang.String entryName,
                                    java.util.zip.ZipOutputStream zos)
                             throws java.io.IOException
        Add a file to the zip
        Parameters:
        file - the file to add
        entryName - the entry name of the file in the zip
        zos - zip outputStream
        Throws:
        java.io.IOException - throw if an IO error occurs
      • addToZip

        public static void addToZip​(java.io.File srcDir,
                                    java.io.FilenameFilter filter,
                                    java.lang.String namePrefix,
                                    java.util.zip.ZipOutputStream zos)
                             throws java.io.IOException
        Add all the files under the directory to the zip stream
        Parameters:
        srcDir - directory under which files are to be added, including sub directories.
        filter - name filter
        namePrefix - the prefix to prepend to all entry names
        zos - zip output stream
        Throws:
        java.io.IOException - throw if any IO error occurs
      • unTarGzip

        public static void unTarGzip​(java.io.File tarGzFile,
                                     java.io.File targetDir,
                                     NameMapper nameMapper,
                                     Progress progress)
                              throws java.io.IOException
        Decompress and untar the tar.gz file to the specified directory. If the target directory does not exist, it will be created.
        Parameters:
        tarGzFile - tar gzip file
        targetDir - target directory to unzip file
        nameMapper - entry name mapper that is used to change file path name.
        progress - if not null, show processing progress
        Throws:
        java.io.IOException - throw if any IO error occurs