org.nuiton.wikitty.publication.externalize
Class JarUtil

java.lang.Object
  extended by org.nuiton.wikitty.publication.externalize.JarUtil

public class JarUtil
extends Object

Opérations sur des fichiers Jar. Compression et décompression avec ou sans filtres, scan des fichiers créés ou écrasés lors de la décompression... Créée à partir de org.nuiton.util.ZipUtil

Author:
mfortun

Field Summary
protected static FileFilter ALL_FILE_FILTER
          Accept all file pattern.
static FileFilter jarFilter
           
 
Constructor Summary
JarUtil()
           
 
Method Summary
static void compress(File jarFile, 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 jarFile, 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 jarFile, File fileOrDirectory, Manifest mf)
          If fileOrDirectory is directory Compress recursively all file in this directory, else if is just file compress one file.
static void compress(File jarFile, File fileOrDirectory, Manifest mf, 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 jarFile, File fileOrDirectory)
           
static void compressFiles(File jarFile, File root, Collection<File> includes)
          Compress 'includes' files in jarFile.
static void compressFiles(File jarFile, File root, Manifest mf, Collection<File> includes, boolean createMD5)
          Compress 'includes' files in jarFile.
protected static String convertToLocalEntryName(String txt)
           
static byte[] getByteContent(JarFile jar, JarEntry jarEnt)
           
static String getStringContent(JarFile jar, JarEntry jarEntry)
           
static void scan(File jarFile, File targetDir, List<String> newFiles, List<String> existingFiles, FileFilter excludeFilter, String renameFrom, String renameTo)
          Scan a jarFile, and fill two lists of relative paths corresponding of jar entries.
static List<String>[] scanAndExplodeJar(File source, File root, FileFilter excludeFilter)
           
static String uncompress(File file, File targetDir)
          Uncompress jarped file in targetDir.
static String uncompress(File file, File targetDir, List<String> toTreate, String renameFrom, String renameTo)
          uncompress jarped file in targetDir.
static String uncompressAndRename(File file, File targetDir, String renameFrom, String renameTo)
          Uncompress jarped file in targetDir, and rename uncompressed file if necessary.
static void uncompressFiltred(File file, File targetDir, String... excludes)
          Unjar 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

jarFilter

public static FileFilter jarFilter

ALL_FILE_FILTER

protected static FileFilter ALL_FILE_FILTER
Accept all file pattern.

Constructor Detail

JarUtil

public JarUtil()
Method Detail

uncompress

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

Parameters:
file - the jar 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 jarped file in targetDir, and rename uncompressed file if necessary. If renameFrom or renameTo is null no renaming is done

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

Parameters:
file - the jar 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 jarFile,
                                 File fileOrDirectory)
                          throws IOException
Parameters:
jarFile -
fileOrDirectory -
Throws:
IOException

compressFiles

public static void compressFiles(File jarFile,
                                 File root,
                                 Collection<File> includes)
                          throws IOException
Compress 'includes' files in jarFile. If file in includes is directory only the directory is put in jarFile, not the file contained in directory

Parameters:
jarFile - the destination jar file
root - for all file in includes that is in this directory, then we remove this directory in jar entry name (aka -C for tar), can be null;
includes - the files to include in jar
Throws:
IOException - if any problem while compressing

compressFiles

public static void compressFiles(File jarFile,
                                 File root,
                                 Manifest mf,
                                 Collection<File> includes,
                                 boolean createMD5)
                          throws IOException
Compress 'includes' files in jarFile. If file in includes is directory only the directory is put in jarFile, not the file contained in directory

Parameters:
jarFile - the destination jar file
root - for all file in includes that is in this directory, then we remove this directory in jar entry name (aka -C for tar), can be null;
includes - the files to include in jar
createMD5 - also create a MD5 file (jar name + .md5). MD5 file is created after jar.
Throws:
IOException - if any problem while compressing

compress

public static void compress(File jarFile,
                            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 jar start at fileOrDirectory. example: if we compress /etc/apache, entry will be apache/http.conf, ...

Parameters:
jarFile - the target jar file
fileOrDirectory - the file or directory to compress
Throws:
IOException - if any problem while compressing

compress

public static void compress(File jarFile,
                            File fileOrDirectory,
                            Manifest mf)
                     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 jar start at fileOrDirectory. example: if we compress /etc/apache, entry will be apache/http.conf, ...

Parameters:
jarFile - the target jar file
fileOrDirectory - the file or directory to compress
mf - the manifest for the jar
Throws:
IOException - if any problem while compressing

compress

public static void compress(File jarFile,
                            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 jar start at fileOrDirectory. example: if we compress /etc/apache, entry will be apache/http.conf, ...

Parameters:
jarFile - the target jar 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 jarFile,
                            File fileOrDirectory,
                            Manifest mf,
                            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 jar start at fileOrDirectory. example: if we compress /etc/apache, entry will be apache/http.conf, ...

Parameters:
jarFile - the target jar 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 (jar name + .md5). MD5 file is created after jar.
Throws:
IOException - if any problem while compressing

scan

public static void scan(File jarFile,
                        File targetDir,
                        List<String> newFiles,
                        List<String> existingFiles,
                        FileFilter excludeFilter,
                        String renameFrom,
                        String renameTo)
                 throws IOException
Scan a jarFile, and fill two lists of relative paths corresponding of jar 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:
jarFile - location of the jar to scanJar
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 jarfile

scanAndExplodeJar

public static List<String>[] scanAndExplodeJar(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 jarped 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 jar 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
Unjar 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

convertToLocalEntryName

protected static String convertToLocalEntryName(String txt)

getStringContent

public static String getStringContent(JarFile jar,
                                      JarEntry jarEntry)
                               throws IOException
Throws:
IOException

getByteContent

public static byte[] getByteContent(JarFile jar,
                                    JarEntry jarEnt)
                             throws IOException
Throws:
IOException


Copyright © 2010-2011 CodeLutin. All Rights Reserved.