001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    package org.apache.commons.compress.compressors.pack200;
021    
022    import java.io.File;
023    import java.io.FileOutputStream;
024    import java.io.IOException;
025    import java.io.OutputStream;
026    import java.util.HashMap;
027    import java.util.Map;
028    import java.util.jar.JarFile;
029    import java.util.jar.JarOutputStream;
030    import java.util.jar.Pack200;
031    
032    /**
033     * Utility methods for Pack200.
034     *
035     * @ThreadSafe
036     * @since 1.3
037     */
038    public class Pack200Utils {
039        private Pack200Utils() { }
040    
041        /**
042         * Normalizes a JAR archive in-place so it can be safely signed
043         * and packed.
044         *
045         * <p>As stated in <a
046         * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
047         * javadocs applying a Pack200 compression to a JAR archive will
048         * in general make its sigantures invalid.  In order to prepare a
049         * JAR for signing it should be "normalized" by packing and
050         * unpacking it.  This is what this method does.</p>
051         *
052         * <p>Note this methods implicitly sets the segment length to
053         * -1.</p>
054         *
055         * @param jar the JAR archive to normalize
056         */
057        public static void normalize(File jar)
058            throws IOException {
059            normalize(jar, jar, null);
060        }
061    
062        /**
063         * Normalizes a JAR archive in-place so it can be safely signed
064         * and packed.
065         *
066         * <p>As stated in <a
067         * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
068         * javadocs applying a Pack200 compression to a JAR archive will
069         * in general make its sigantures invalid.  In order to prepare a
070         * JAR for signing it should be "normalized" by packing and
071         * unpacking it.  This is what this method does.</p>
072         *
073         * @param jar the JAR archive to normalize
074         * @param props properties to set for the pack operation.  This
075         * method will implicitly set the segment limit to -1.
076         */
077        public static void normalize(File jar, Map<String, String> props)
078            throws IOException {
079            normalize(jar, jar, props);
080        }
081    
082        /**
083         * Normalizes a JAR archive so it can be safely signed and packed.
084         *
085         * <p>As stated in <a
086         * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
087         * javadocs applying a Pack200 compression to a JAR archive will
088         * in general make its sigantures invalid.  In order to prepare a
089         * JAR for signing it should be "normalized" by packing and
090         * unpacking it.  This is what this method does.</p>
091         *
092         * <p>This method does not replace the existing archive but creates
093         * a new one.</p>
094         *
095         * <p>Note this methods implicitly sets the segment length to
096         * -1.</p>
097         *
098         * @param from the JAR archive to normalize
099         * @param to the normalized archive
100         */
101        public static void normalize(File from, File to)
102            throws IOException {
103            normalize(from, to, null);
104        }
105    
106        /**
107         * Normalizes a JAR archive so it can be safely signed and packed.
108         *
109         * <p>As stated in <a
110         * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
111         * javadocs applying a Pack200 compression to a JAR archive will
112         * in general make its sigantures invalid.  In order to prepare a
113         * JAR for signing it should be "normalized" by packing and
114         * unpacking it.  This is what this method does.</p>
115         *
116         * <p>This method does not replace the existing archive but creates
117         * a new one.</p>
118         *
119         * @param from the JAR archive to normalize
120         * @param to the normalized archive
121         * @param props properties to set for the pack operation.  This
122         * method will implicitly set the segment limit to -1.
123         */
124        public static void normalize(File from, File to, Map<String, String> props)
125            throws IOException {
126            if (props == null) {
127                props = new HashMap<String, String>();
128            }
129            props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
130            File f = File.createTempFile("commons-compress", "pack200normalize");
131            f.deleteOnExit();
132            try {
133                OutputStream os = new FileOutputStream(f);
134                JarFile j = null;
135                try {
136                    Pack200.Packer p = Pack200.newPacker();
137                    p.properties().putAll(props);
138                    p.pack(j = new JarFile(from), os);
139                    j = null;
140                    os.close();
141                    os = null;
142    
143                    Pack200.Unpacker u = Pack200.newUnpacker();
144                    os = new JarOutputStream(new FileOutputStream(to));
145                    u.unpack(f, (JarOutputStream) os);
146                } finally {
147                    if (j != null) {
148                        j.close();
149                    }
150                    if (os != null) {
151                        os.close();
152                    }
153                }
154            } finally {
155                f.delete();
156            }
157        }
158    }