Author: bodewig Date: Thu Sep 15 12:58:25 2011 New Revision: 1171086 URL: http://svn.apache.org/viewvc?rev=1171086&view=rev Log: Convenience overloads
Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java Modified: commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java?rev=1171086&r1=1171085&r2=1171086&view=diff ============================================================================== --- commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java (original) +++ commons/proper/compress/trunk/src/main/java/org/apache/commons/compress/compressors/pack200/Pack200Utils.java Thu Sep 15 12:58:25 2011 @@ -23,6 +23,7 @@ import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; +import java.util.HashMap; import java.util.Map; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; @@ -38,6 +39,71 @@ public class Pack200Utils { private Pack200Utils() { } /** + * Normalizes a JAR archive in-place so it can be safely signed + * and packed. + * + * <p>As stated in <a + * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a> + * javadocs applying a Pack200 compression to a JAR archive will + * in general make its sigantures invalid. In order to prepare a + * JAR for signing it should be "normalized" by packing and + * unpacking it. This is what this method does.</p> + * + * <p>Note this methods implicitly sets the segment length to + * -1.</p> + * + * @param jar the JAR archive to normalize + */ + public static void normalize(File jar) + throws IOException { + normalize(jar, jar, null); + } + + /** + * Normalizes a JAR archive in-place so it can be safely signed + * and packed. + * + * <p>As stated in <a + * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a> + * javadocs applying a Pack200 compression to a JAR archive will + * in general make its sigantures invalid. In order to prepare a + * JAR for signing it should be "normalized" by packing and + * unpacking it. This is what this method does.</p> + * + * @param jar the JAR archive to normalize + * @param props properties to set for the pack operation. This + * method will implicitly set the segment limit to -1. + */ + public static void normalize(File jar, Map<String, String> props) + throws IOException { + normalize(jar, jar, props); + } + + /** + * Normalizes a JAR archive so it can be safely signed and packed. + * + * <p>As stated in <a + * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a> + * javadocs applying a Pack200 compression to a JAR archive will + * in general make its sigantures invalid. In order to prepare a + * JAR for signing it should be "normalized" by packing and + * unpacking it. This is what this method does.</p> + * + * <p>This method does not replace the existing archive but creates + * a new one.</p> + * + * <p>Note this methods implicitly sets the segment length to + * -1.</p> + * + * @param from the JAR archive to normalize + * @param to the normalized archive + */ + public static void normalize(File from, File to) + throws IOException { + normalize(from, to, null); + } + + /** * Normalizes a JAR archive so it can be safely signed and packed. * * <p>As stated in <a @@ -57,6 +123,9 @@ public class Pack200Utils { */ public static void normalize(File from, File to, Map<String, String> props) throws IOException { + if (props == null) { + props = new HashMap<String, String>(); + } props.put(Pack200.Packer.SEGMENT_LIMIT, "-1"); File f = File.createTempFile("commons-compress", "pack200normalize"); f.deleteOnExit(); Modified: commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java?rev=1171086&r1=1171085&r2=1171086&view=diff ============================================================================== --- commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java (original) +++ commons/proper/compress/trunk/src/test/java/org/apache/commons/compress/compressors/pack200/Pack200UtilsTest.java Thu Sep 15 12:58:25 2011 @@ -69,4 +69,51 @@ public final class Pack200UtilsTest exte } } + public void testNormalizeInPlace() throws Throwable { + final File input = getFile("bla.jar"); + final File[] output = createTempDirAndFile(); + try { + FileInputStream is = new FileInputStream(input); + OutputStream os = null; + try { + os = new FileOutputStream(output[1]); + IOUtils.copy(is, os); + } finally { + is.close(); + if (os != null) { + os.close(); + } + } + + Pack200Utils.normalize(output[1]); + is = new FileInputStream(output[1]); + try { + final ArchiveInputStream in = new ArchiveStreamFactory() + .createArchiveInputStream("jar", is); + + ArchiveEntry entry = in.getNextEntry(); + while (entry != null) { + File archiveEntry = new File(dir, entry.getName()); + archiveEntry.getParentFile().mkdirs(); + if (entry.isDirectory()) { + archiveEntry.mkdir(); + entry = in.getNextEntry(); + continue; + } + OutputStream out = new FileOutputStream(archiveEntry); + IOUtils.copy(in, out); + out.close(); + entry = in.getNextEntry(); + } + + in.close(); + } finally { + is.close(); + } + } finally { + output[1].delete(); + output[0].delete(); + } + } + }