Class ZIPHelper
java.lang.Object
net.simplace.core.service.ZIPHelper
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 -
Method Summary
Modifier and TypeMethodDescriptionstatic voidCopies all entries of a ZIP file into a new ZIP file and adds one further file (without path).static voidaddToZIP(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 voidcreateZIP(String aFileName, InputStream aInputStream, File aDestinationFile) Creates a ZIP file with a single entry read from an input stream.static voidcreateZIP(Collection<File> aFilesCollection, File aDestinationFile) Creates a ZIP file from a collection of files.static FilecreateZIP(Collection<ZipEntry> aZipEntriesColl, File aSourceZipFile, File aDestinationZipFile) Creates a new ZIP file containing only the given entries of an existing ZIP file.static voidcreateZIPWithPathEntries(File aDirectory, File aZipFile) Packs a directory recursively into a ZIP file.static voidcreateZIPWithPathEntries(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 voidflattenZipArchive(File aSourceFile, File aDestinationFile) Iterates all ZipEntries in the source file and moves them into a new ZipArchive aDestinationFile.static chargetFileSeparator(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.static Collection<ZipEntry> getZIPEntriesHavingPathName(File aSourceZipFile, String aName) Returns the entries of a ZIP file whose name (including path) contains the given string.static Collection<ZipEntry> getZIPEntriesWithoutPathName(File aSourceZipFile, String aName) Returns the entries of a ZIP file whose name (including path) does not contain the given string.static ZipEntrygetZIPEntry(File aSourceZipFile, String aFileName) Returns a single entry of a ZIP file.static ZipEntrygetZIPEntryExcludingPath(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 booleanpackAndDeleteDirContents(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 booleanreadZIPEntry(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 voidremoveZipEntry(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 voidremoveZipEntry(List<String> aFilenameList, File aSourceZipFile, File aDestinationZipFile) Copies a ZIP file without the entries whose file name (without path) is in the given list.Unpacks a ZIP file into a directory.Unpacks a ZIP file into a directory.Unpacks the entries of a ZIP file that start with a given sub path into a directory.Unpacks the entries of a ZIP file that start with a given sub path into a directory.static voidunZIPWithPath(File aSourceZipFile, String aDestinationDir) Unpacks a ZIP file into a directory, restoring the paths of the entries.
-
Constructor Details
-
ZIPHelper
public ZIPHelper()
-
-
Method Details
-
createZIPWithPathEntries
Packs a directory recursively into a ZIP file. Entry names are relative to the directory.- Parameters:
aDirectory- directory to packaZipFile- 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 packaDestinationFile- ZIP file to createaParentStartPath- 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
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 unpackaDestinationDir- 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 createaDir- directory whose files are packedaExtension- only files with this extension are packedaOlderThanMillis- only files older than this age in milliseconds are packed- Returns:
trueif 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 packaDestinationFile- 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 isnull.- Parameters:
aFileName- name of the entry in the ZIP fileaInputStream- content of the entryaDestinationFile- 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 addaSourceZipFile- existing ZIP fileaDestinationZipFile- 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 addaSourceZipFile- existing ZIP fileaDestinationZipFile- 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; ifnull, nothing is doneaSourceZipFile- existing ZIP fileaDestinationZipFile- ZIP file to create- Returns:
- aDestinationZipFile
- Throws:
ZipException- if a ZIP file cannot be read or created
-
unZIP
Unpacks a ZIP file into a directory.- Parameters:
aSourceZipFile- ZIP file to unpackaDestinationDir- 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 unpackaDestinationDir- target directoryaReplacePercentInPath- iftrue,%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 unpackaSubPath- only entries starting with this path are unpacked; empty string for all entriesaDestinationDir- 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 unpackaSubPath- only entries starting with this path are unpacked; empty string for all entriesaDestinationDir- target directoryaReplacePercentInPath- iftrue,%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
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 readaName- 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 readaName- string that must not occur in the entry names- Returns:
- the matching entries
- Throws:
ZipException- if the ZIP file cannot be read
-
getZIPEntry
Returns a single entry of a ZIP file.- Parameters:
aSourceZipFile- ZIP file to readaFileName- full entry name including path- Returns:
- the entry, or
nullif 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 readaFileName- file name (or end of the entry name) to search for- Returns:
- the first matching entry, or
nullif 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 fileaDestinationZipFile- 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 removeaSourceZipFile- existing ZIP fileaDestinationZipFile- 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 readaDestinationFile- file to write the entry content toaFileName- full entry name including path- Returns:
trueif the entry was found and written,falseif it does not exist- Throws:
ZipException- if the ZIP file cannot be readIOException- 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 readaFileName- full entry name including pathaDivider- column separator of the CSV file- Returns:
- the CSV content as rows and columns including the header line, or
nullif the entry does not exist - Throws:
ZipException- if the ZIP file cannot be readIOException- if the entry cannot be read- See Also:
-
getFileSeparator
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
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 readaDestinationFile- flat ZIP archive to create- Throws:
ZipException- if an archive cannot be read or created
-