Class ZipUtil
Created: 24 août 2006 10:13:35
- Author:
- Benjamin Poussin - poussin@codelutin.com
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidIf fileOrDirectory is directory Compress recursively all file in this directory, else if is just file compress one file.static voidcompress(File zipFile, File fileOrDirectory, FileFilter filter) If fileOrDirectory is directory Compress recursively all file in this directory, else if is just file compress one file.static voidcompress(File zipFile, File fileOrDirectory, FileFilter filter, boolean createMD5) If fileOrDirectory is directory Compress recursively all file in this directory, else if is just file compress one file.static voidcompressFiles(File zipFile, File root, Collection<File> includes) Compress 'includes' files in zipFile.static voidcompressFiles(File zipFile, File root, Collection<File> includes, boolean createMD5) Compress 'includes' files in zipFile.protected static Stringstatic booleanTests if the given file is a zip file.static voidscan(File zipFile, File targetDir, List<String> newFiles, List<String> existingFiles, FileFilter excludeFilter, String renameFrom, String renameTo) Scan a zipFile, and fill two lists of relative paths corresponding of zip entries.scanAndExplodeZip(File source, File root, FileFilter excludeFilter) static Stringuncompress(File file, File targetDir) Uncompress zipped file in targetDir.static Stringuncompress zipped file in targetDir.static Stringuncompress(InputStream stream, File targetDir) Uncompress zipped stream in targetDir.static StringuncompressAndRename(File file, File targetDir, String renameFrom, String renameTo) Uncompress zipped file in targetDir, and rename uncompressed file if necessary.static StringuncompressAndRename(InputStream stream, File targetDir, String renameFrom, String renameTo) Uncompress zipped stream in targetDir, and rename uncompressed file if necessary.static voiduncompressFiltred(File file, File targetDir, String... excludes) Unzip compressed archive and keep non excluded patterns.
-
Field Details
-
ALL_FILE_FILTER
Accept all file pattern.
-
-
Constructor Details
-
ZipUtil
public ZipUtil()
-
-
Method Details
-
uncompress
Uncompress zipped file in targetDir.- Parameters:
file- the zip source filetargetDir- the destination directory- Returns:
- return last entry name
- Throws:
IOException- if any problem while uncompressing
-
uncompress
Uncompress zipped stream in targetDir.- Parameters:
stream- the zip source stream, stream is closed before returntargetDir- the destination directory- Returns:
- return last entry name
- Throws:
IOException- if any problem while uncompressing- Since:
- 2.6.6
-
uncompressAndRename
public static String uncompressAndRename(File file, File targetDir, String renameFrom, String renameTo) throws IOException Uncompress zipped file in targetDir, and rename uncompressed file if necessary. If renameFrom or renameTo is null no renaming is donefile in zip use / to separate directory and not begin with / each directory ended with /
- Parameters:
file- the zip source filetargetDir- the destination directoryrenameFrom- pattern to permit rename file before uncompress itrenameTo- new name for file if renameFrom is applicable to it you can use $1, $2, ... if you have '(' ')' in renameFrom- Returns:
- return last entry name
- Throws:
IOException- if any problem while uncompressing
-
uncompressAndRename
public static String uncompressAndRename(InputStream stream, File targetDir, String renameFrom, String renameTo) throws IOException Uncompress zipped stream in targetDir, and rename uncompressed file if necessary. If renameFrom or renameTo is null no renaming is donefile in zip use / to separate directory and not begin with / each directory ended with /
- Parameters:
stream- the zip source stream, stream is closed before returntargetDir- the destination directoryrenameFrom- pattern to permit rename file before uncompress itrenameTo- new name for file if renameFrom is applicable to it you can use $1, $2, ... if you have '(' ')' in renameFrom- Returns:
- return last entry name
- Throws:
IOException- if any problem while uncompressing- Since:
- 2.6.6
-
compressFiles
public static void compressFiles(File zipFile, File root, Collection<File> includes) throws IOException Compress 'includes' files in zipFile. If file in includes is directory only the directory is put in zipFile, not the file contained in directory- Parameters:
zipFile- the destination zip fileroot- for all file in includes that is in this directory, then we remove this directory in zip entry name (aka -C for tar), can be null;includes- the files to include in zip- Throws:
IOException- if any problem while compressing
-
compressFiles
public static void compressFiles(File zipFile, File root, Collection<File> includes, boolean createMD5) throws IOException Compress 'includes' files in zipFile. If file in includes is directory only the directory is put in zipFile, not the file contained in directory- Parameters:
zipFile- the destination zip fileroot- for all file in includes that is in this directory, then we remove this directory in zip entry name (aka -C for tar), can be null;includes- the files to include in zipcreateMD5- also create a MD5 file (zip name + .md5). MD5 file is created after zip.- Throws:
IOException- if any problem while compressing
-
compress
If fileOrDirectory is directory Compress recursively all file in this directory, else if is just file compress one file.Entry result name in zip start at fileOrDirectory. example: if we compress /etc/apache, entry will be apache/http.conf, ...
- Parameters:
zipFile- the target zip filefileOrDirectory- the file or directory to compress- Throws:
IOException- if any problem while compressing
-
compress
public static void compress(File zipFile, File fileOrDirectory, FileFilter filter) throws IOException If fileOrDirectory is directory Compress recursively all file in this directory, else if is just file compress one file.Entry result name in zip start at fileOrDirectory. example: if we compress /etc/apache, entry will be apache/http.conf, ...
- Parameters:
zipFile- the target zip filefileOrDirectory- the file or directory to compressfilter- used to accept file, if null, all file is accepted- Throws:
IOException- if any problem while compressing
-
compress
public static void compress(File zipFile, File fileOrDirectory, FileFilter filter, boolean createMD5) throws IOException If fileOrDirectory is directory Compress recursively all file in this directory, else if is just file compress one file.Entry result name in zip start at fileOrDirectory. example: if we compress /etc/apache, entry will be apache/http.conf, ...
- Parameters:
zipFile- the target zip filefileOrDirectory- the file or directory to compressfilter- used to accept file, if null, all file is acceptedcreateMD5- also create a MD5 file (zip name + .md5). MD5 file is created after zip.- Throws:
IOException- if any problem while compressing
-
scan
public static void scan(File zipFile, File targetDir, List<String> newFiles, List<String> existingFiles, FileFilter excludeFilter, String renameFrom, String renameTo) throws IOException Scan a zipFile, and fill two lists of relative paths corresponding of zip entries. First list contains all entries to be added while a uncompress operation on the destination directorytargetDir. Second list contains all entries to be overwritten while a uncompress operation on the destination directorytargetDir.If
targetDirisnullwe don't fillexistingFileslist.- Parameters:
zipFile- location of the zip to scanZiptargetDir- location of destination for a uncompress operation. Ifnullwe don't test to find overwritten files.newFiles- list of files to be added while a uncompressexistingFiles- list of files to be overwritten while a uncompress if thetargetDir, (only use iftargetDiris notnull)excludeFilter- used to exclude some filesrenameFrom-uncompressAndRename(File, File, String, String)renameTo-uncompressAndRename(File, File, String, String)- Throws:
IOException- if any exception while dealing with zipfile
-
scanAndExplodeZip
public static List<String>[] scanAndExplodeZip(File source, File root, FileFilter excludeFilter) throws IOException - Throws:
IOException
-
uncompress
public static String uncompress(File file, File targetDir, List<String> toTreate, String renameFrom, String renameTo) throws IOException uncompress zipped file in targetDir.If
toTreateif not null nor empty, we use it to filter entries to uncompress : it contains a list of relative local path of files to uncompress. Otherwise just delegate touncompress(File, File).- Parameters:
file- location of zip filetargetDir- destination directorytoTreate- list of relative local path of entries to treaterenameFrom-uncompressAndRename(File, File, String, String)renameTo-uncompressAndRename(File, File, String, String)- Returns:
- return last entry name
- Throws:
IOException- if nay exception while operation
-
uncompressFiltred
public static void uncompressFiltred(File file, File targetDir, String... excludes) throws IOException Unzip compressed archive and keep non excluded patterns.- Parameters:
file- archive filetargetDir- destination fileexcludes- excludes pattern (pattern must match complete entry name including root folder)- Throws:
IOException- FIXME
-
isZipFile
Tests if the given file is a zip file.- Parameters:
file- the file to test- Returns:
trueif the file is a valid zip file,falseotherwise.- Since:
- 2.4.9
-
convertToLocalEntryName
-