Package jodd.io

Class ZipUtil


  • public class ZipUtil
    extends java.lang.Object
    Performs zip/gzip/zlib operations on files and directories. These are just tools over existing java.util.zip classes, meaning that existing behavior and bugs are persisted. Most common issue is not being able to use UTF8 in file names, because implementation uses old ZIP format that supports only IBM Code Page 437. This bug was resolved in JDK7: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4244499
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.lang.String GZIP_EXT  
      static java.lang.String ZIP_EXT  
      static java.lang.String ZLIB_EXT  
    • Constructor Summary

      Constructors 
      Constructor Description
      ZipUtil()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void addFolderToZip​(java.util.zip.ZipOutputStream zos, java.lang.String path, java.lang.String comment)  
      static void addToZip​(java.util.zip.ZipOutputStream zos, byte[] content, java.lang.String path, java.lang.String comment)
      Adds byte content into the zip as a file.
      static void addToZip​(java.util.zip.ZipOutputStream zos, java.io.File file, java.lang.String path, java.lang.String comment, boolean recursive)
      Adds single entry to ZIP output stream.
      static void close​(java.util.zip.ZipFile zipFile)
      Closes zip file safely.
      static java.io.File gzip​(java.io.File file)
      Compresses a file into gzip archive.
      static java.io.File gzip​(java.lang.String fileName)
      Compresses a file into gzip archive.
      static java.util.List<java.lang.String> listZip​(java.io.File zipFile)
      Lists zip content.
      static java.io.File ungzip​(java.io.File file)
      Decompress gzip archive.
      static java.io.File ungzip​(java.lang.String file)
      Decompress gzip archive.
      static void unzip​(java.io.File zipFile, java.io.File destDir, java.lang.String... patterns)
      Extracts zip file to the target directory.
      static void unzip​(java.lang.String zipFile, java.lang.String destDir, java.lang.String... patterns)
      Extracts zip file content to the target directory.
      static java.io.File zip​(java.io.File file)
      Zips a file or a folder.
      static java.io.File zip​(java.lang.String file)
      Zips a file or a folder.
      static java.io.File zlib​(java.io.File file)
      Compresses a file into zlib archive.
      static java.io.File zlib​(java.lang.String file)
      Compresses a file into zlib archive.
      • 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

      • zlib

        public static java.io.File zlib​(java.lang.String file)
                                 throws java.io.IOException
        Compresses a file into zlib archive.
        Throws:
        java.io.IOException
      • zlib

        public static java.io.File zlib​(java.io.File file)
                                 throws java.io.IOException
        Compresses a file into zlib archive.
        Throws:
        java.io.IOException
      • gzip

        public static java.io.File gzip​(java.lang.String fileName)
                                 throws java.io.IOException
        Compresses a file into gzip archive.
        Throws:
        java.io.IOException
      • gzip

        public static java.io.File gzip​(java.io.File file)
                                 throws java.io.IOException
        Compresses a file into gzip archive.
        Throws:
        java.io.IOException
      • ungzip

        public static java.io.File ungzip​(java.lang.String file)
                                   throws java.io.IOException
        Decompress gzip archive.
        Throws:
        java.io.IOException
      • ungzip

        public static java.io.File ungzip​(java.io.File file)
                                   throws java.io.IOException
        Decompress gzip archive.
        Throws:
        java.io.IOException
      • zip

        public static java.io.File zip​(java.lang.String file)
                                throws java.io.IOException
        Zips a file or a folder.
        Throws:
        java.io.IOException
        See Also:
        zip(java.io.File)
      • zip

        public static java.io.File zip​(java.io.File file)
                                throws java.io.IOException
        Zips a file or a folder. If adding a folder, all its content will be added.
        Throws:
        java.io.IOException
      • listZip

        public static java.util.List<java.lang.String> listZip​(java.io.File zipFile)
                                                        throws java.io.IOException
        Lists zip content.
        Throws:
        java.io.IOException
      • unzip

        public static void unzip​(java.lang.String zipFile,
                                 java.lang.String destDir,
                                 java.lang.String... patterns)
                          throws java.io.IOException
        Extracts zip file content to the target directory.
        Throws:
        java.io.IOException
        See Also:
        unzip(java.io.File, java.io.File, String...)
      • unzip

        public static void unzip​(java.io.File zipFile,
                                 java.io.File destDir,
                                 java.lang.String... patterns)
                          throws java.io.IOException
        Extracts zip file to the target directory. If patterns are provided only matched paths are extracted.
        Parameters:
        zipFile - zip file
        destDir - destination directory
        patterns - optional wildcard patterns of files to extract, may be null
        Throws:
        java.io.IOException
      • addToZip

        public static void addToZip​(java.util.zip.ZipOutputStream zos,
                                    java.io.File file,
                                    java.lang.String path,
                                    java.lang.String comment,
                                    boolean recursive)
                             throws java.io.IOException
        Adds single entry to ZIP output stream.
        Parameters:
        zos - zip output stream
        file - file or folder to add
        path - relative path of file entry; if null files name will be used instead
        comment - optional comment
        recursive - when set to true content of added folders will be added, too
        Throws:
        java.io.IOException
      • addToZip

        public static void addToZip​(java.util.zip.ZipOutputStream zos,
                                    byte[] content,
                                    java.lang.String path,
                                    java.lang.String comment)
                             throws java.io.IOException
        Adds byte content into the zip as a file.
        Throws:
        java.io.IOException
      • addFolderToZip

        public static void addFolderToZip​(java.util.zip.ZipOutputStream zos,
                                          java.lang.String path,
                                          java.lang.String comment)
                                   throws java.io.IOException
        Throws:
        java.io.IOException
      • close

        public static void close​(java.util.zip.ZipFile zipFile)
        Closes zip file safely.