Index: lutinutil/src/java/org/codelutin/util/FileUtil.java diff -u lutinutil/src/java/org/codelutin/util/FileUtil.java:1.10 lutinutil/src/java/org/codelutin/util/FileUtil.java:1.11 --- lutinutil/src/java/org/codelutin/util/FileUtil.java:1.10 Tue Aug 22 15:38:32 2006 +++ lutinutil/src/java/org/codelutin/util/FileUtil.java Thu Aug 24 13:36:48 2006 @@ -23,9 +23,9 @@ * Created: 22 nov. 2004 * * @author Benjamin Poussin - * @version $Revision: 1.10 $ + * @version $Revision: 1.11 $ * - * Mise a jour: $Date: 2006/08/22 15:38:32 $ + * Mise a jour: $Date: 2006/08/24 13:36:48 $ * par : $Author: bpoussin $ */ @@ -324,7 +324,7 @@ * recursivement à partir de directory, si directory * n'est pas un répertoire la liste est vide. */ - public static List getSubDirectories(File directory) { + public static List getSubDirectories(File directory) { class DirectoryFilter implements FileFilter { public boolean accept(File f) { return f.isDirectory(); @@ -340,7 +340,7 @@ * recursivement à partir de directory, si directory n'est pas un * répertoire la liste est vide */ - public static List getFiles(File directory) { + public static List getFiles(File directory) { class NormalFileFilter implements FileFilter { public boolean accept(File f) { return f.isFile(); @@ -358,7 +358,7 @@ * @return une liste d'objet {@link java.io.File} qui ont s'attisfait le * pattern. */ - public static List find(File directory, final String pattern){ + public static List find(File directory, final String pattern){ final String root = directory.getAbsolutePath(); final int rootLength = root.length(); @@ -377,7 +377,7 @@ * le filtre * @param directory repertoire à partir duquel il faut faire la recherche * @param ff le filtre à appliquer pour savoir si le fichier parcouru doit - * être conservé dans les résultats. + * être conservé dans les résultats, ou null pour tous les fichiers * @return une liste d'objet {@link java.io.File}, qui s'attisfont le filtre */ public static List getFilteredElements(File directory, FileFilter ff) { @@ -389,9 +389,12 @@ while(todo.size() > 0){ File file = todo.removeFirst(); if (file.isDirectory()){ - todo.addAll(Arrays.asList(file.listFiles())); + File [] childs = file.listFiles(); + if (childs != null) { // null if we don't have access to directory + todo.addAll(Arrays.asList(childs)); + } } - if(ff.accept(file)){ + if(ff == null || ff.accept(file)){ result.add(file); } } @@ -488,6 +491,26 @@ public static void copy(String source, String target) throws IOException { copy(new File(source), new File(target)); } + + /** + * Copie recursivement le repertoire source dans le repertoire destination + * + * copyRecursively("/truc/titi", "/var/tmp") donnera le repertoire + * "/var/tmp/titi" + * + * @param srcDir + * @param destDir + * @throws IOException + */ + static public void copyRecursively(File srcDir, File destDir) throws IOException { + String rootSrc = srcDir.getParent(); + List files = getFilteredElements(srcDir, null); + for (File file : files) { + String path = file.getPath().substring(rootSrc.length()); + File destFile = new File(destDir, path); + copy(file, destFile); + } + } } // FileUtil Index: lutinutil/src/java/org/codelutin/util/ZipUtil.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/ZipUtil.java:1.1 --- /dev/null Thu Aug 24 13:36:53 2006 +++ lutinutil/src/java/org/codelutin/util/ZipUtil.java Thu Aug 24 13:36:48 2006 @@ -0,0 +1,166 @@ +/* *##% + * Copyright (C) 2006 + * Code Lutin, Cédric Pineau, Benjamin Poussin + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + *##%*/ + +/* * + * ZipUtil.java + * + * Created: 24 août 2006 10:13:35 + * + * @author poussin + * @version $Revision: 1.1 $ + * + * Last update: $Date: 2006/08/24 13:36:48 $ + * by : $Author: bpoussin $ + */ + +package org.codelutin.util; + +import java.io.BufferedInputStream; +import java.io.File; +import java.io.FileFilter; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream; + +import org.apache.commons.logging.LogFactory; + + +/** + * @author poussin + * + */ + +public class ZipUtil { + + /** to use log facility, just put in your code: log.info(\"...\"); */ + static private org.apache.commons.logging.Log log = LogFactory.getLog(ZipUtil.class); + + /** Taille du buffer pour les lectures/écritures */ + private static final int BUFFER_SIZE = 8 * 1024; + + /** + * 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 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 + */ + static public void compressFiles(File zipFile, File root, Collection includes) throws IOException { + FileOutputStream oStream = new FileOutputStream(zipFile); + try { + ZipOutputStream zipOStream = new ZipOutputStream(oStream); + + for (File file : includes) { + String entryName = toZipEntryName(root, file); + + // Création d'une nouvelle entrée dans le zip + ZipEntry entry = new ZipEntry(entryName); + entry.setTime(file.lastModified()); + zipOStream.putNextEntry(entry); + + if (file.isFile() && file.canRead()) { + byte[] readBuffer = new byte[BUFFER_SIZE]; + int bytesIn = 0; + BufferedInputStream bis = new BufferedInputStream( + new FileInputStream(file), BUFFER_SIZE); + while ((bytesIn = bis.read(readBuffer, 0, BUFFER_SIZE)) != -1) { + zipOStream.write(readBuffer, 0, bytesIn); + } + bis.close(); + } + zipOStream.closeEntry(); + } + + } finally { + oStream.close(); + } + } + + /** + *
  • supprime le root du fichier + *
  • Converti les '\' en '/' car les zip entry utilise des '/' + *
  • ajoute un '/' a la fin pour les repertoires + *
  • supprime le premier '/' si la chaine commence par un '/' + * + * @param file + * @return + */ + private static String toZipEntryName(File root, File file) { + String result = file.getPath(); + + if (root != null) { + String rootPath = root.getPath(); + if (result.startsWith(rootPath)) { + result = result.substring(rootPath.length()); + } + } + + result = result.replace('\\', '/'); + if (file.isDirectory()) { + result += '/'; + } + while (result.startsWith("/")) { + result = result.substring(1); + } + return result; + } + + /** + * 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, ... + * + * @param zipFile + * @param fileOrDirectory + * @param filter used to accept file, if null, all file is accepted + * @throws IOException + */ + static public void compress(File zipFile, File fileOrDirectory, FileFilter filter) throws IOException { + if (filter == null) { + filter = ALL_FILE_FILTER; + } + List files = new ArrayList(); + if (fileOrDirectory.isDirectory()) { + files = FileUtil.getFilteredElements(fileOrDirectory, filter); + } else if (filter.accept(fileOrDirectory)){ + files.add(fileOrDirectory); + } + + compressFiles(zipFile, fileOrDirectory.getParentFile(), files); + } + + static private FileFilter ALL_FILE_FILTER = new FileFilter() { + public boolean accept(File pathname) { + return true; + } + + }; + +} + +