Index: lutinutil/src/java/org/codelutin/util/ZipUtil.java diff -u lutinutil/src/java/org/codelutin/util/ZipUtil.java:1.6 lutinutil/src/java/org/codelutin/util/ZipUtil.java:1.7 --- lutinutil/src/java/org/codelutin/util/ZipUtil.java:1.6 Mon Nov 12 22:50:34 2007 +++ lutinutil/src/java/org/codelutin/util/ZipUtil.java Sun Dec 9 19:09:11 2007 @@ -23,9 +23,9 @@ * Created: 24 août 2006 10:13:35 * * @author poussin - * @version $Revision: 1.6 $ + * @version $Revision: 1.7 $ * - * Last update: $Date: 2007-11-12 22:50:34 $ + * Last update: $Date: 2007-12-09 19:09:11 $ * by : $Author: tchemit $ */ @@ -85,13 +85,14 @@ /** * uncompress zipped file in targetDir * - * @param file - * @param targetDir + * @param file the zip source file + * @param targetDir the destination direcotory * @return return last entry name - * @throws IOException + * @throws IOException if any problem while uncompressing */ public static String uncompress(File file, File targetDir) throws IOException { - String result = uncompressAndRename(file, targetDir, null, null); + String result; + result = uncompressAndRename(file, targetDir, null, null); return result; } @@ -102,18 +103,18 @@ * file in zip use / to separate directory and not begin with / * each directory ended with / * - * @param file - * @param targetDir + * @param file the zip source file + * @param targetDir the destination directory * @param renameFrom pattern to permit rename file before uncompress it * @param renameTo new name for file if renameFrom is applicable to it * you can use $1, $2, ... if you have '(' ')' in renameFrom * @return return last entry name - * @throws IOException + * @throws IOException if any problem while uncompressing */ public static String uncompressAndRename(File file, File targetDir, String renameFrom, String renameTo) throws IOException { String result = ""; ZipInputStream in = new ZipInputStream(new FileInputStream(file)); - ZipEntry entry = null; + ZipEntry entry; while ((entry = in.getNextEntry()) != null) { String name = entry.getName(); if (renameFrom != null && renameTo != null) { @@ -130,7 +131,7 @@ target.getParentFile().mkdirs(); OutputStream out = new BufferedOutputStream(new FileOutputStream(target)); byte[] buffer = new byte[BUFFER_SIZE]; - int len = 0; + int len; while ((len = in.read(buffer, 0, BUFFER_SIZE)) != -1) { out.write(buffer, 0, len); } @@ -145,10 +146,11 @@ * Compress 'includes' files in zipFile. If file in includes is directory * only the directory is put in zipFile, not the file contained in directory * - * @param zipFile + * @param zipFile the destination zip file * @param 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; - * @param includes + * @param includes the files to include in zip + * @throws java.io.IOException if any problem while compressing */ static public void compressFiles(File zipFile, File root, Collection includes) throws IOException { FileOutputStream oStream = new FileOutputStream(zipFile); @@ -165,7 +167,7 @@ if (file.isFile() && file.canRead()) { byte[] readBuffer = new byte[BUFFER_SIZE]; - int bytesIn = 0; + int bytesIn; BufferedInputStream bis = new BufferedInputStream( new FileInputStream(file), BUFFER_SIZE); while ((bytesIn = bis.read(readBuffer, 0, BUFFER_SIZE)) != -1) { @@ -188,10 +190,10 @@ * Entry result name in zip start at fileOrDirectory. * example: if we compress /etc/apache, entry will be apache/http.conf, ... * - * @param zipFile - * @param fileOrDirectory + * @param zipFile the source zip file + * @param fileOrDirectory the file or directory to compress * @param filter used to accept file, if null, all file is accepted - * @throws IOException + * @throws IOException if any problem while compressing */ static public void compress(File zipFile, File fileOrDirectory, FileFilter filter) throws IOException { if (filter == null) { @@ -213,8 +215,10 @@ *
  • ajoute un '/' a la fin pour les repertoires *
  • supprime le premier '/' si la chaine commence par un '/' * - * @param file - * @return + * @param root the root directory + * @param file the file to treate + * @return the zip entry name corresponding to the given file + * from root dir. */ private static String toZipEntryName(File root, File file) { String result = file.getPath(); @@ -257,20 +261,26 @@ * (only use if targetDir is not * null) * @param excludeFilter used to exclude some files + * @param renameFrom {@link #uncompressAndRename(java.io.File, java.io.File, String, String)} + * @param renameTo {@link #uncompressAndRename(java.io.File, java.io.File, String, String)} * @throws IOException if any exception while dealing with zipfile */ public static void scan(File zipFile, File targetDir, List newFiles, - List existingFiles, FileFilter excludeFilter) + List existingFiles, FileFilter excludeFilter,String renameFrom, String renameTo) throws IOException { ZipFile zip = null; try { zip = new ZipFile(zipFile); boolean findExisting = targetDir != null && targetDir.exists(); boolean filter = findExisting && excludeFilter != null; + boolean rename = renameFrom!=null && renameTo!=null; Enumeration entries = zip.entries(); while (entries.hasMoreElements()) { - String name = convertToLocalEntryName( - entries.nextElement().getName()); + String entryName = entries.nextElement().getName(); + if (rename) { + entryName = entryName.replaceAll(renameFrom, renameTo); + } + String name = convertToLocalEntryName(entryName); if (findExisting || filter) { File file = new File(targetDir, name); if (filter && excludeFilter.accept(file)) continue; @@ -297,27 +307,34 @@ * @param file location of zip file * @param targetDir destination directory * @param toTreate list of relative local path of entries to treate + * @param renameFrom {@link #uncompressAndRename(java.io.File, java.io.File, String, String)} + * @param renameTo {@link #uncompressAndRename(java.io.File, java.io.File, String, String)} * @return return last entry name * @throws IOException if nay exception while operation */ - public static String uncompress(File file, File targetDir, - List toTreate) throws IOException { + public static String uncompress(File file, File targetDir,List toTreate,String renameFrom, String renameTo) throws IOException { String result = ""; ZipInputStream in = new ZipInputStream(new FileInputStream(file)); ZipEntry entry; if (toTreate == null || toTreate.isEmpty()) - return ZipUtil.uncompress(file, targetDir); + return ZipUtil.uncompressAndRename(file, targetDir,renameFrom,renameTo); + + boolean rename = renameFrom!=null && renameTo!=null; while ((entry = in.getNextEntry()) != null) { String name = entry.getName(); - result = convertToLocalEntryName(name); + if (rename) { + result = convertToLocalEntryName(name.replaceAll(renameFrom, renameTo)); + } else { + result = convertToLocalEntryName(name); + } log.debug("open [" + name + "] : " + result); if (!toTreate.contains(result)) continue; log.debug("copy [" + name + "] : " + result); - File target = new File(targetDir, name); + File target = new File(targetDir, result); if (entry.isDirectory()) { target.mkdirs(); } else {