org.nuiton.util
Class ZipUtil

java.lang.Object
  extended by org.nuiton.util.ZipUtil

public class ZipUtil
extends Object

ZipUtil.java Created: 24 août 2006 10:13:35

Version:
$Revision: 1974 $ Last update: $Date: 2010-12-20 17:50:43 +0100 (lun, 20 déc 2010) $ by :
Author:
poussin

Field Summary
protected static FileFilter ALL_FILE_FILTER
          Accept all file pattern.
 
Constructor Summary
ZipUtil()
           
 
Method Summary
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 String convertToZipEntryName(String txt, boolean isDir)
          Deprecated. unused/undocumented function
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 uncompressAndRename(File file, File targetDir, String renameFrom, String renameTo)
          Uncompress zipped file 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALL_FILE_FILTER

protected static FileFilter ALL_FILE_FILTER
Accept all file pattern.

Constructor Detail

ZipUtil

public ZipUtil()
Method Detail

uncompress

public static String uncompress(File file,
                                File targetDir)
                         throws IOException
Uncompress zipped file in targetDir.

Parameters:
file - the zip source file
targetDir - the destination directory
Returns:
return last entry name
Throws:
IOException - if any problem while uncompressing

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 done

file in zip use / to separate directory and not begin with / each directory ended with /

Parameters:
file - the zip source file
targetDir - the destination directory
renameFrom - pattern to permit rename file before uncompress it
renameTo - 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

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 file
root - 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 file
root - 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
createMD5 - also create a MD5 file (zip name + .md5). MD5 file is created after zip.
Throws:
IOException - if any problem while compressing

compress

public static void compress(File zipFile,
                            File fileOrDirectory)
                     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 file
fileOrDirectory - 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 file
fileOrDirectory - the file or directory to compress
filter - 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 file
fileOrDirectory - the file or directory to compress
filter - used to accept file, if null, all file is accepted
createMD5 - 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 directory 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.

Parameters:
zipFile - location of the zip to scanZip
targetDir - 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 uncompress
existingFiles - list of files to be overwritten while a uncompress if the targetDir, (only use if targetDir is not null)
excludeFilter - used to exclude some files
renameFrom - 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 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).

Parameters:
file - location of zip file
targetDir - destination directory
toTreate - list of relative local path of entries to treate
renameFrom - 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 file
targetDir - destination file
excludes - excludes pattern (pattern must match complete entry name including root folder)
Throws:
IOException

convertToZipEntryName

public static String convertToZipEntryName(String txt,
                                           boolean isDir)
Deprecated. unused/undocumented function


convertToLocalEntryName

protected static String convertToLocalEntryName(String txt)


Copyright © 2004-2011 CodeLutin. All Rights Reserved.