Class ZIPHelper

java.lang.Object
net.simplace.core.service.ZIPHelper

public final class ZIPHelper extends Object
Static helper methods for creating, reading, extending and unpacking ZIP archives.

Methods that modify an archive never work in place: they read a source archive and write a new destination archive.

Author:
Andreas Enders
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static void
    addToZIP(File aFileToAdd, File aSourceZipFile, File aDestinationZipFile)
    Copies all entries of a ZIP file into a new ZIP file and adds one further file (without path).
    static void
    addToZIP(Collection<File> aFilesCollection, File aSourceZipFile, File aDestinationZipFile)
    Copies all entries of a ZIP file into a new ZIP file and adds further files (without paths).
    static void
    createZIP(String aFileName, InputStream aInputStream, File aDestinationFile)
    Creates a ZIP file with a single entry read from an input stream.
    static void
    createZIP(Collection<File> aFilesCollection, File aDestinationFile)
    Creates a ZIP file from a collection of files.
    static File
    createZIP(Collection<ZipEntry> aZipEntriesColl, File aSourceZipFile, File aDestinationZipFile)
    Creates a new ZIP file containing only the given entries of an existing ZIP file.
    static void
    createZIPWithPathEntries(File aDirectory, File aZipFile)
    Packs a directory recursively into a ZIP file.
    static void
    createZIPWithPathEntries(Collection<File> aFilesCollection, File aDestinationFile, String aParentStartPath)
    Creates a ZIP file from a collection of files, keeping the path of each file relative to a start path, so that the directory structure is restored when unpacking.
    static void
    flattenZipArchive(File aSourceFile, File aDestinationFile)
    Iterates all ZipEntries in the source file and moves them into a new ZipArchive aDestinationFile.
    static char
    getFileSeparator(File aZipArchive)
    Parses the Entries of the given Zip archive and looks for the file separator character, if it is a directory.
    static Collection<? extends ZipEntry>
    getZIPEntries(File aSourceZipFile)
    Lists the entries of a ZIP file.
    getZIPEntriesHavingPathName(File aSourceZipFile, String aName)
    Returns the entries of a ZIP file whose name (including path) contains the given string.
    getZIPEntriesWithoutPathName(File aSourceZipFile, String aName)
    Returns the entries of a ZIP file whose name (including path) does not contain the given string.
    static ZipEntry
    getZIPEntry(File aSourceZipFile, String aFileName)
    Returns a single entry of a ZIP file.
    static ZipEntry
    getZIPEntryExcludingPath(File aSourceZipFile, String aFileName)
    Returns the first entry of a ZIP file whose name ends with the given file name, regardless of its path.
    static final boolean
    packAndDeleteDirContents(File aZipFile, File aDir, String aExtension, long aOlderThanMillis)
    Packs the matching files of a directory (not its subdirectories) into a ZIP file and deletes them afterwards.
    static boolean
    readZIPEntry(File aSourceZipFile, File aDestinationFile, String aFileName)
    Extracts a single entry of a ZIP file into a file.
    static String[][]
    readZIPEntry(File aSourceZipFile, String aFileName, String aDivider)
    Reads a CSV file from a ZIP file into a two-dimensional array.
    static void
    removeZipEntry(String aFilename, File aSourceZipFile, File aDestinationZipFile)
    Copies a ZIP file without the entries whose file name (without path) equals the given name, ignoring case.
    static void
    removeZipEntry(List<String> aFilenameList, File aSourceZipFile, File aDestinationZipFile)
    Copies a ZIP file without the entries whose file name (without path) is in the given list.
    static List<File>
    unZIP(File aSourceZipFile, String aDestinationDir)
    Unpacks a ZIP file into a directory.
    static List<File>
    unZIP(File aSourceZipFile, String aDestinationDir, boolean aReplacePercentInPath)
    Unpacks a ZIP file into a directory.
    static List<File>
    unZIP(File aSourceZipFile, String aSubPath, String aDestinationDir)
    Unpacks the entries of a ZIP file that start with a given sub path into a directory.
    static List<File>
    unZIP(File aSourceZipFile, String aSubPath, String aDestinationDir, boolean aReplacePercentInPath)
    Unpacks the entries of a ZIP file that start with a given sub path into a directory.
    static void
    unZIPWithPath(File aSourceZipFile, String aDestinationDir)
    Unpacks a ZIP file into a directory, restoring the paths of the entries.

    Methods inherited from class Object

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

    • ZIPHelper

      public ZIPHelper()
  • Method Details

    • createZIPWithPathEntries

      public static void createZIPWithPathEntries(File aDirectory, File aZipFile) throws ZipException
      Packs a directory recursively into a ZIP file. Entry names are relative to the directory.
      Parameters:
      aDirectory - directory to pack
      aZipFile - ZIP file to create
      Throws:
      ZipException - if the ZIP file cannot be created
    • createZIPWithPathEntries

      public static void createZIPWithPathEntries(Collection<File> aFilesCollection, File aDestinationFile, String aParentStartPath) throws ZipException
      Creates a ZIP file from a collection of files, keeping the path of each file relative to a start path, so that the directory structure is restored when unpacking. Directories in the collection are added as directory entries, so empty directories are preserved.
      Parameters:
      aFilesCollection - files and directories to pack
      aDestinationFile - ZIP file to create
      aParentStartPath - path prefix that is removed from the absolute file paths to build the entry names
      Throws:
      ZipException - if the ZIP file cannot be created
    • unZIPWithPath

      public static void unZIPWithPath(File aSourceZipFile, String aDestinationDir) throws ZipException
      Unpacks a ZIP file into a directory, restoring the paths of the entries. Directory entries are created as (possibly empty) directories.
      Parameters:
      aSourceZipFile - ZIP file to unpack
      aDestinationDir - target directory
      Throws:
      ZipException - if the ZIP file cannot be read or unpacked
    • packAndDeleteDirContents

      public static final boolean packAndDeleteDirContents(File aZipFile, File aDir, String aExtension, long aOlderThanMillis)
      Packs the matching files of a directory (not its subdirectories) into a ZIP file and deletes them afterwards. The directory itself is not deleted.
      Parameters:
      aZipFile - ZIP file to create
      aDir - directory whose files are packed
      aExtension - only files with this extension are packed
      aOlderThanMillis - only files older than this age in milliseconds are packed
      Returns:
      true if the files were packed and deleted
      See Also:
    • createZIP

      public static void createZIP(Collection<File> aFilesCollection, File aDestinationFile) throws ZipException
      Creates a ZIP file from a collection of files. Only the file names are stored, without paths. Nothing is done if the collection is empty.
      Parameters:
      aFilesCollection - files to pack
      aDestinationFile - ZIP file to create
      Throws:
      ZipException - if the ZIP file cannot be created
    • createZIP

      public static void createZIP(String aFileName, InputStream aInputStream, File aDestinationFile) throws ZipException
      Creates a ZIP file with a single entry read from an input stream. The stream is closed afterwards. Nothing is done if the stream is null.
      Parameters:
      aFileName - name of the entry in the ZIP file
      aInputStream - content of the entry
      aDestinationFile - ZIP file to create
      Throws:
      ZipException - if the ZIP file cannot be created
    • addToZIP

      public static void addToZIP(Collection<File> aFilesCollection, File aSourceZipFile, File aDestinationZipFile) throws ZipException
      Copies all entries of a ZIP file into a new ZIP file and adds further files (without paths). If the source ZIP file does not exist or cannot be read, a new ZIP file with only the given files is created.
      Parameters:
      aFilesCollection - files to add
      aSourceZipFile - existing ZIP file
      aDestinationZipFile - ZIP file to create; must differ from the source file
      Throws:
      ZipException - if the ZIP file cannot be created
    • addToZIP

      public static void addToZIP(File aFileToAdd, File aSourceZipFile, File aDestinationZipFile) throws ZipException
      Copies all entries of a ZIP file into a new ZIP file and adds one further file (without path).
      Parameters:
      aFileToAdd - file to add
      aSourceZipFile - existing ZIP file
      aDestinationZipFile - ZIP file to create; must differ from the source file
      Throws:
      ZipException - if a ZIP file cannot be read or created
    • createZIP

      public static File createZIP(Collection<ZipEntry> aZipEntriesColl, File aSourceZipFile, File aDestinationZipFile) throws ZipException
      Creates a new ZIP file containing only the given entries of an existing ZIP file.
      Parameters:
      aZipEntriesColl - entries of the source file to copy; if null, nothing is done
      aSourceZipFile - existing ZIP file
      aDestinationZipFile - ZIP file to create
      Returns:
      aDestinationZipFile
      Throws:
      ZipException - if a ZIP file cannot be read or created
    • unZIP

      public static List<File> unZIP(File aSourceZipFile, String aDestinationDir) throws ZipException
      Unpacks a ZIP file into a directory.
      Parameters:
      aSourceZipFile - ZIP file to unpack
      aDestinationDir - target directory
      Returns:
      the unpacked files (without directories)
      Throws:
      ZipException - if the ZIP file cannot be read or unpacked
    • unZIP

      public static List<File> unZIP(File aSourceZipFile, String aDestinationDir, boolean aReplacePercentInPath) throws ZipException
      Unpacks a ZIP file into a directory.
      Parameters:
      aSourceZipFile - ZIP file to unpack
      aDestinationDir - target directory
      aReplacePercentInPath - if true, % characters in entry names are treated as path separators
      Returns:
      the unpacked files (without directories)
      Throws:
      ZipException - if the ZIP file cannot be read or unpacked
    • unZIP

      public static List<File> unZIP(File aSourceZipFile, String aSubPath, String aDestinationDir) throws ZipException
      Unpacks the entries of a ZIP file that start with a given sub path into a directory. The sub path is removed from the entry names.
      Parameters:
      aSourceZipFile - ZIP file to unpack
      aSubPath - only entries starting with this path are unpacked; empty string for all entries
      aDestinationDir - target directory
      Returns:
      the unpacked files (without directories)
      Throws:
      ZipException - if the ZIP file cannot be read or unpacked
    • unZIP

      public static List<File> unZIP(File aSourceZipFile, String aSubPath, String aDestinationDir, boolean aReplacePercentInPath) throws ZipException
      Unpacks the entries of a ZIP file that start with a given sub path into a directory. The sub path is removed from the entry names.
      Parameters:
      aSourceZipFile - ZIP file to unpack
      aSubPath - only entries starting with this path are unpacked; empty string for all entries
      aDestinationDir - target directory
      aReplacePercentInPath - if true, % characters in entry names are treated as path separators
      Returns:
      the unpacked files (without directories)
      Throws:
      ZipException - if the ZIP file cannot be read or unpacked
    • getZIPEntries

      public static Collection<? extends ZipEntry> getZIPEntries(File aSourceZipFile) throws ZipException
      Lists the entries of a ZIP file. If the path contains a ! (as in JAR URLs), only the part before it is used as file name.
      Parameters:
      aSourceZipFile - ZIP file to read
      Returns:
      the entries of the ZIP file
      Throws:
      ZipException - if the ZIP file cannot be read
    • getZIPEntriesHavingPathName

      public static Collection<ZipEntry> getZIPEntriesHavingPathName(File aSourceZipFile, String aName) throws ZipException
      Returns the entries of a ZIP file whose name (including path) contains the given string.
      Parameters:
      aSourceZipFile - ZIP file to read
      aName - string to search for in the entry names
      Returns:
      the matching entries
      Throws:
      ZipException - if the ZIP file cannot be read
    • getZIPEntriesWithoutPathName

      public static Collection<ZipEntry> getZIPEntriesWithoutPathName(File aSourceZipFile, String aName) throws ZipException
      Returns the entries of a ZIP file whose name (including path) does not contain the given string.
      Parameters:
      aSourceZipFile - ZIP file to read
      aName - string that must not occur in the entry names
      Returns:
      the matching entries
      Throws:
      ZipException - if the ZIP file cannot be read
    • getZIPEntry

      public static ZipEntry getZIPEntry(File aSourceZipFile, String aFileName) throws ZipException
      Returns a single entry of a ZIP file.
      Parameters:
      aSourceZipFile - ZIP file to read
      aFileName - full entry name including path
      Returns:
      the entry, or null if it does not exist
      Throws:
      ZipException - if the ZIP file cannot be read
    • getZIPEntryExcludingPath

      public static ZipEntry getZIPEntryExcludingPath(File aSourceZipFile, String aFileName) throws ZipException
      Returns the first entry of a ZIP file whose name ends with the given file name, regardless of its path.
      Parameters:
      aSourceZipFile - ZIP file to read
      aFileName - file name (or end of the entry name) to search for
      Returns:
      the first matching entry, or null if there is none
      Throws:
      ZipException - if the ZIP file cannot be read
    • removeZipEntry

      public static void removeZipEntry(List<String> aFilenameList, File aSourceZipFile, File aDestinationZipFile) throws ZipException
      Copies a ZIP file without the entries whose file name (without path) is in the given list.
      Parameters:
      aFilenameList - file names to remove (case sensitive)
      aSourceZipFile - existing ZIP file
      aDestinationZipFile - ZIP file to create; must differ from the source file
      Throws:
      ZipException - if a ZIP file cannot be read or created
    • removeZipEntry

      public static void removeZipEntry(String aFilename, File aSourceZipFile, File aDestinationZipFile) throws ZipException
      Copies a ZIP file without the entries whose file name (without path) equals the given name, ignoring case.
      Parameters:
      aFilename - file name to remove
      aSourceZipFile - existing ZIP file
      aDestinationZipFile - ZIP file to create; must differ from the source file
      Throws:
      ZipException - if a ZIP file cannot be read or created
    • readZIPEntry

      public static boolean readZIPEntry(File aSourceZipFile, File aDestinationFile, String aFileName) throws ZipException, IOException
      Extracts a single entry of a ZIP file into a file.
      Parameters:
      aSourceZipFile - ZIP file to read
      aDestinationFile - file to write the entry content to
      aFileName - full entry name including path
      Returns:
      true if the entry was found and written, false if it does not exist
      Throws:
      ZipException - if the ZIP file cannot be read
      IOException - if the destination file cannot be written
    • readZIPEntry

      public static String[][] readZIPEntry(File aSourceZipFile, String aFileName, String aDivider) throws ZipException, IOException
      Reads a CSV file from a ZIP file into a two-dimensional array.
      Parameters:
      aSourceZipFile - ZIP file to read
      aFileName - full entry name including path
      aDivider - column separator of the CSV file
      Returns:
      the CSV content as rows and columns including the header line, or null if the entry does not exist
      Throws:
      ZipException - if the ZIP file cannot be read
      IOException - if the entry cannot be read
      See Also:
    • getFileSeparator

      public static char getFileSeparator(File aZipArchive)
      Parses the Entries of the given Zip archive and looks for the file separator character, if it is a directory. This method is useful as you don't know if the ZipArchive was packed on a Linux or on a Windows system. Both operating systems use different file separators when creating a zip archive. if it should be unzipped on the other OS, this might lead to errors.
      Parameters:
      aZipArchive - ZIP archive to check
      Returns:
      the separator found in the first file entry containing one; / by default or on error
    • flattenZipArchive

      public static void flattenZipArchive(File aSourceFile, File aDestinationFile) throws ZipException
      Iterates all ZipEntries in the source file and moves them into a new ZipArchive aDestinationFile. Only files are moved. Directories are not moved. Files are written to the new archive without path entries.

      Author: ffrankenberger

      Parameters:
      aSourceFile - ZIP archive to read
      aDestinationFile - flat ZIP archive to create
      Throws:
      ZipException - if an archive cannot be read or created