public class ZipUtil extends Object
Modifier and Type | Field and Description |
---|---|
protected static FileFilter |
ALL_FILE_FILTER
Accept all file pattern.
|
Constructor and Description |
---|
ZipUtil() |
Modifier and Type | Method and Description |
---|---|
static void |
compress(File zipFile,
File fileOrDirectory)
If fileOrDirectory is directory Compress recursively all file in this
directory, else if is just file compress one file.
|
static void |
compress(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 void |
compress(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 void |
compressFiles(File zipFile,
File root,
Collection<File> includes)
Compress 'includes' files in zipFile.
|
static void |
compressFiles(File zipFile,
File root,
Collection<File> includes,
boolean createMD5)
Compress 'includes' files in zipFile.
|
protected static String |
convertToLocalEntryName(String txt) |
static boolean |
isZipFile(File file)
Tests if the given file is a zip file.
|
static void |
scan(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.
|
static List<String>[] |
scanAndExplodeZip(File source,
File root,
FileFilter excludeFilter) |
static String |
uncompress(File file,
File targetDir)
Uncompress zipped file in targetDir.
|
static String |
uncompress(File file,
File targetDir,
List<String> toTreate,
String renameFrom,
String renameTo)
uncompress zipped file in targetDir.
|
static String |
uncompress(InputStream stream,
File targetDir)
Uncompress zipped stream in targetDir.
|
static String |
uncompressAndRename(File file,
File targetDir,
String renameFrom,
String renameTo)
Uncompress zipped file in targetDir, and rename uncompressed file if
necessary.
|
static String |
uncompressAndRename(InputStream stream,
File targetDir,
String renameFrom,
String renameTo)
Uncompress zipped stream in targetDir, and rename uncompressed file if
necessary.
|
static void |
uncompressFiltred(File file,
File targetDir,
String... excludes)
Unzip compressed archive and keep non excluded patterns.
|
protected static FileFilter ALL_FILE_FILTER
public static String uncompress(File file, File targetDir) throws IOException
file
- the zip source filetargetDir
- the destination directoryIOException
- if any problem while uncompressingpublic static String uncompress(InputStream stream, File targetDir) throws IOException
stream
- the zip source stream, stream is closed before returntargetDir
- the destination directoryIOException
- if any problem while uncompressingpublic static String uncompressAndRename(File file, File targetDir, String renameFrom, String renameTo) throws IOException
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 renameFromIOException
- if any problem while uncompressingpublic static String uncompressAndRename(InputStream stream, File targetDir, String renameFrom, String renameTo) throws IOException
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 renameFromIOException
- if any problem while uncompressingpublic static void compressFiles(File zipFile, File root, Collection<File> includes) throws IOException
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 zipIOException
- if any problem while compressingpublic static void compressFiles(File zipFile, File root, Collection<File> includes, boolean createMD5) throws IOException
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.IOException
- if any problem while compressingpublic static void compress(File zipFile, File fileOrDirectory) throws IOException
zipFile
- the target zip filefileOrDirectory
- the file or directory to compressIOException
- if any problem while compressingpublic static void compress(File zipFile, File fileOrDirectory, FileFilter filter) throws IOException
zipFile
- the target zip filefileOrDirectory
- the file or directory to compressfilter
- used to accept file, if null, all file is acceptedIOException
- if any problem while compressingpublic static void compress(File zipFile, File fileOrDirectory, FileFilter filter, boolean createMD5) throws IOException
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.IOException
- if any problem while compressingpublic static void scan(File zipFile, File targetDir, List<String> newFiles, List<String> existingFiles, FileFilter excludeFilter, String renameFrom, String renameTo) throws IOException
targetDir
.
Second list contains all entries to be overwritten while a uncompress
operation on the destination directory targetDir
.
If targetDir
is null
we don't fill existingFiles
list.zipFile
- location of the zip to scanZiptargetDir
- location of destination for a uncompress operation.
If null
we 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 the targetDir
,
(only use if targetDir
is not
null
)excludeFilter
- used to exclude some filesrenameFrom
- uncompressAndRename(File, File, String, String)
renameTo
- uncompressAndRename(File, File, String, String)
IOException
- if any exception while dealing with zipfilepublic static List<String>[] scanAndExplodeZip(File source, File root, FileFilter excludeFilter) throws IOException
IOException
public static String uncompress(File file, File targetDir, List<String> toTreate, String renameFrom, String renameTo) throws IOException
toTreate
if 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 to uncompress(File, File)
.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)
IOException
- if nay exception while operationpublic static void uncompressFiltred(File file, File targetDir, String... excludes) throws IOException
file
- archive filetargetDir
- destination fileexcludes
- excludes pattern (pattern must match complete entry name including root folder)IOException
- FIXMEpublic static boolean isZipFile(File file)
file
- the file to testtrue
if the file is a valid zip file,
false
otherwise.Copyright © 2004–2020 CodeLutin. All rights reserved.