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
020package org.apache.commons.compress.compressors.pack200;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.OutputStream;
025import java.nio.file.Files;
026import java.nio.file.Path;
027import java.util.HashMap;
028import java.util.Map;
029import java.util.jar.JarFile;
030import java.util.jar.JarOutputStream;
031
032import org.apache.commons.compress.java.util.jar.Pack200;
033
034/**
035 * Utility methods for Pack200.
036 *
037 * @ThreadSafe
038 * @since 1.3
039 */
040public class Pack200Utils {
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="https://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 signatures 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     * @throws IOException if reading or writing fails
057     */
058    public static void normalize(final File jar)
059        throws IOException {
060        normalize(jar, jar, null);
061    }
062
063    /**
064     * Normalizes a JAR archive, so it can be safely signed and packed.
065     *
066     * <p>As stated in <a
067     * href="https://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 signatures 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     * <p>This method does not replace the existing archive but creates
074     * a new one.</p>
075     *
076     * <p>Note this methods implicitly sets the segment length to
077     * -1.</p>
078     *
079     * @param from the JAR archive to normalize
080     * @param to the normalized archive
081     * @throws IOException if reading or writing fails
082     */
083    public static void normalize(final File from, final File to)
084        throws IOException {
085        normalize(from, to, null);
086    }
087
088    /**
089     * Normalizes a JAR archive, so it can be safely signed and packed.
090     *
091     * <p>As stated in <a
092     * href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
093     * javadocs applying a Pack200 compression to a JAR archive will
094     * in general make its signatures invalid.  In order to prepare a
095     * JAR for signing it should be "normalized" by packing and
096     * unpacking it.  This is what this method does.</p>
097     *
098     * <p>This method does not replace the existing archive but creates
099     * a new one.</p>
100     *
101     * @param from the JAR archive to normalize
102     * @param to the normalized archive
103     * @param props properties to set for the pack operation.  This
104     * method will implicitly set the segment limit to -1.
105     * @throws IOException if reading or writing fails
106     */
107    public static void normalize(final File from, final File to, Map<String, String> props)
108            throws IOException {
109        if (props == null) {
110            props = new HashMap<>();
111        }
112        props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
113        final Path tempFile = Files.createTempFile("commons-compress", "pack200normalize");
114        try {
115            try (OutputStream fos = Files.newOutputStream(tempFile);
116                 JarFile jarFile = new JarFile(from)) {
117                final Pack200.Packer packer = Pack200.newPacker();
118                packer.properties().putAll(props);
119                packer.pack(jarFile, fos);
120            }
121            final Pack200.Unpacker unpacker = Pack200.newUnpacker();
122            try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(to.toPath()))) {
123                unpacker.unpack(tempFile.toFile(), jos);
124            }
125        } finally {
126            Files.delete(tempFile);
127        }
128    }
129
130    /**
131     * Normalizes a JAR archive in-place, so it can be safely signed
132     * and packed.
133     *
134     * <p>As stated in <a
135     * href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
136     * javadocs applying a Pack200 compression to a JAR archive will
137     * in general make its signatures invalid.  In order to prepare a
138     * JAR for signing it should be "normalized" by packing and
139     * unpacking it.  This is what this method does.</p>
140     *
141     * @param jar the JAR archive to normalize
142     * @param props properties to set for the pack operation.  This
143     * method will implicitly set the segment limit to -1.
144     * @throws IOException if reading or writing fails
145     */
146    public static void normalize(final File jar, final Map<String, String> props)
147        throws IOException {
148        normalize(jar, jar, props);
149    }
150
151    private Pack200Utils() { }
152}