Author: chatellier Date: 2009-04-28 17:13:08 +0000 (Tue, 28 Apr 2009) New Revision: 1449 Removed: lutinutil/trunk/src/main/java/org/codelutin/util/MD5State.java Modified: lutinutil/trunk/changelog.txt lutinutil/trunk/src/main/java/org/codelutin/util/ZipUtil.java lutinutil/trunk/src/test/java/org/codelutin/util/ZipUtilTest.java Log: Create a md5 file "on-the-fly" when crating an archive Modified: lutinutil/trunk/changelog.txt =================================================================== --- lutinutil/trunk/changelog.txt 2009-04-28 16:59:51 UTC (rev 1448) +++ lutinutil/trunk/changelog.txt 2009-04-28 17:13:08 UTC (rev 1449) @@ -1,5 +1,9 @@ +ver 1.0.5 chatellier xxxxxx + * Change MD5 implementation to http://ostermiller.org/utils/MD5.html one. + ver 1.0.4 chemit 20090311 * 20090305 [chemit] - improve FileUtil methods : now can specify the component's invoker (fix some focus bugs in fullscreen mode) + ver 1.0.3 chemit 20090218 * 20090218 [chemit] - use lutinproject 3.4 * 20090209 [chemit] - add inputStreamToFile method in FileUtil + javadoc on this class Deleted: lutinutil/trunk/src/main/java/org/codelutin/util/MD5State.java =================================================================== --- lutinutil/trunk/src/main/java/org/codelutin/util/MD5State.java 2009-04-28 16:59:51 UTC (rev 1448) +++ lutinutil/trunk/src/main/java/org/codelutin/util/MD5State.java 2009-04-28 17:13:08 UTC (rev 1449) @@ -1,97 +0,0 @@ -/** - * *##% Lutin utilities library - * Copyright (C) 2004 - 2008 CodeLutin - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as - * published by the Free Software Foundation, either version 3 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 Lesser Public License for more details. - * - * You should have received a copy of the GNU General Lesser Public - * License along with this program. If not, see - * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* - */ -package org.codelutin.util; - -/** - * Fast implementation of RSA's MD5 hash generator in Java JDK Beta-2 or higher<br> - * Originally written by Santeri Paavolainen, Helsinki Finland 1996 <br> - * (c) Santeri Paavolainen, Helsinki Finland 1996 <br> - * Some changes Copyright (c) 2002 Timothy W Macinta <br> - * <p> - * This library is free software; you can redistribute it and/or modify it under - * the terms of the GNU Library General Public License as published by the Free - * Software Foundation; either version 2.1 of the License, or (at your option) - * any later version. - * <p> - * This library 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 Library General Public License for more - * details. - * <p> - * You should have received a copy of the GNU Library General Public License - * along with this library; if not, write to the Free Software Foundation, Inc., - * 675 Mass Ave, Cambridge, MA 02139, USA. - * <p> - * See http://www.twmacinta.com/myjava/fast_md5.php for more information on this - * file. - * <p> - * Contains internal state of the MD5 class - * <p> - * Please note: I (Timothy Macinta) have put this code in the com.twmacinta.util - * package only because it came without a package. I was not the the original - * author of the code, although I did optimize it (substantially) and fix some - * bugs. - * - * @author Santeri Paavolainen <sjpaavol@cc.helsinki.fi> - * @author Timothy W Macinta (twm@alum.mit.edu) (optimizations and bug fixes) - */ - -class MD5State { - /** - * 128-bit state - */ - int state[]; - - /** - * 64-bit character count - */ - long count; - - /** - * 64-byte buffer (512 bits) for storing to-be-hashed characters - */ - byte buffer[]; - - public MD5State() { - buffer = new byte[64]; - count = 0; - state = new int[4]; - - state[0] = 0x67452301; - state[1] = 0xefcdab89; - state[2] = 0x98badcfe; - state[3] = 0x10325476; - - } - - /** Create this State as a copy of another state */ - public MD5State(MD5State from) { - this(); - - int i; - - for (i = 0; i < buffer.length; i++) - this.buffer[i] = from.buffer[i]; - - for (i = 0; i < state.length; i++) - this.state[i] = from.state[i]; - - this.count = from.count; - } -}; Modified: lutinutil/trunk/src/main/java/org/codelutin/util/ZipUtil.java =================================================================== --- lutinutil/trunk/src/main/java/org/codelutin/util/ZipUtil.java 2009-04-28 16:59:51 UTC (rev 1448) +++ lutinutil/trunk/src/main/java/org/codelutin/util/ZipUtil.java 2009-04-28 17:13:08 UTC (rev 1449) @@ -1,5 +1,5 @@ /* *##% Lutin utilities library - * Copyright (C) 2004 - 2008 CodeLutin + * Copyright (C) 2004 - 2009 CodeLutin * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as @@ -15,18 +15,6 @@ * License along with this program. If not, see * <http://www.gnu.org/licenses/lgpl-3.0.html>. ##%* */ -/* * - * ZipUtil.java - * - * Created: 24 août 2006 10:13:35 - * - * @author poussin - * @version $Revision$ - * - * Last update: $Date$ - * by : $Author$ - */ - package org.codelutin.util; import org.apache.commons.logging.LogFactory; @@ -48,37 +36,45 @@ import java.util.zip.ZipInputStream; import java.util.zip.ZipOutputStream; - -/** @author poussin */ - +/** + * ZipUtil.java + * + * Created: 24 août 2006 10:13:35 + * + * @author poussin + * @version $Revision$ + * + * Last update: $Date$ + * by : $Author$ + */ 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); + /** Class logger. */ + private static org.apache.commons.logging.Log log = LogFactory.getLog(ZipUtil.class); - /** Taille du buffer pour les lectures/écritures */ + /** Taille du buffer pour les lectures/écritures. */ private static final int BUFFER_SIZE = 8 * 1024; - /** le séparateur de fichier en local */ + /** Le séparateur de fichier en local. */ private static final String LOCAL_SEP = File.separator; private static final String LOCAL_SEP_PATTERN = "\\".equals(LOCAL_SEP) ? LOCAL_SEP + LOCAL_SEP : LOCAL_SEP; - /** le séparateur zip */ + /** Le séparateur zip. */ private static final String ZIP_SEP = "/"; private static final String ZIP_SEP_PATTERN = "/"; - static private FileFilter ALL_FILE_FILTER = new FileFilter() { + /** Accept all file pattern. */ + protected static FileFilter ALL_FILE_FILTER = new FileFilter() { public boolean accept(File pathname) { return true; } - }; /** - * uncompress zipped file in targetDir + * Uncompress zipped file in targetDir. * * @param file the zip source file * @param targetDir the destination direcotory @@ -92,7 +88,7 @@ } /** - * uncompress zipped file in targetDir, and rename uncompressed file if + * Uncompress zipped file in targetDir, and rename uncompressed file if * necessary. If renameFrom or renameTo is null no renaming is done * <p/> * file in zip use / to separate directory and not begin with / @@ -148,7 +144,27 @@ * @throws java.io.IOException if any problem while compressing */ static public void compressFiles(File zipFile, File root, Collection<File> includes) throws IOException { - FileOutputStream oStream = new FileOutputStream(zipFile); + compressFiles(zipFile, root, includes, false); + } + + /** + * 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 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 the files to include in zip + * @param createMD5 also create a MD5 file (zip name + .md5) + * @throws IOException if any problem while compressing + */ + public static void compressFiles(File zipFile, File root, Collection<File> includes, boolean createMD5) throws IOException { + OutputStream oStream = new FileOutputStream(zipFile); + + // if md5 creation flag + if (createMD5) { + oStream = new MD5OutputStream(oStream); + } try { ZipOutputStream zipOStream = new ZipOutputStream(oStream); @@ -173,6 +189,13 @@ zipOStream.closeEntry(); } zipOStream.close(); + + // if md5 creation flag + if (createMD5) { + String md5hash = ((MD5OutputStream)oStream).getHashString(); + File md5File = new File(zipFile.getAbsoluteFile() + ".md5"); + FileUtil.writeString(md5File, md5hash); + } } finally { oStream.close(); } Modified: lutinutil/trunk/src/test/java/org/codelutin/util/ZipUtilTest.java =================================================================== --- lutinutil/trunk/src/test/java/org/codelutin/util/ZipUtilTest.java 2009-04-28 16:59:51 UTC (rev 1448) +++ lutinutil/trunk/src/test/java/org/codelutin/util/ZipUtilTest.java 2009-04-28 17:13:08 UTC (rev 1449) @@ -20,6 +20,8 @@ import java.io.File; import java.io.FileFilter; import java.io.IOException; +import java.util.Collection; +import java.util.Collections; import java.util.List; import org.apache.commons.logging.LogFactory; @@ -40,9 +42,11 @@ public class ZipUtilTest { /** to use log facility, just put in your code: log.info(\"...\"); */ - private static org.apache.commons.logging.Log log = LogFactory.getLog(ZipUtilTest.class); + private static org.apache.commons.logging.Log log = LogFactory + .getLog(ZipUtilTest.class); - protected static final File DIR = new File(System.getProperty("java.home"), "bin"); + protected static final File DIR = new File(System.getProperty("java.home"), + "bin"); @Test public void testUncompress() throws IOException { @@ -62,7 +66,7 @@ List<File> dest = FileUtil.getFilteredElements(ucz, null, true); Assert.assertEquals(src.size() + 1, dest.size()); // +1 car il y a le rep lui meme dans dest - + // remove created temp dirs : FileUtil.deleteRecursively(ucz); Assert.assertFalse(ucz.isDirectory()); @@ -108,7 +112,8 @@ @Test public void testCompressFile() throws IOException { - File dir = new File(System.getProperty("java.home"), "bin" + File.separator + "java"); + File dir = new File(System.getProperty("java.home"), "bin" + + File.separator + "java"); File zipFile = File.createTempFile("testCompressZip", ".zip"); zipFile.deleteOnExit(); log.info("Compress " + dir + " in zip file = " + zipFile); @@ -119,4 +124,23 @@ Assert.assertTrue(0 != zipFile.length()); } + @Test + public void testCompressFileMD5() throws IOException { + File dir = new File(System.getProperty("java.home"), "bin" + + File.separator + "java"); + File zipFile = File.createTempFile("testCompressZip", ".zip"); + zipFile.deleteOnExit(); + log.info("Compress " + dir + " in zip file = " + zipFile); + + Collection<File> files = Collections.singleton(dir); + ZipUtil.compressFiles(zipFile, dir, files, true); + + Assert.assertTrue(zipFile.exists()); + Assert.assertTrue(zipFile.length() > 0); + File md5File = new File(zipFile.getAbsoluteFile() + ".md5"); + md5File.deleteOnExit(); + Assert.assertTrue(md5File.exists()); + Assert.assertTrue(md5File.length() > 0); + } + }