Pack200Utils.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  * http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */

  19. package org.apache.commons.compress.compressors.pack200;

  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.OutputStream;
  23. import java.nio.file.Files;
  24. import java.nio.file.Path;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. import java.util.jar.JarFile;
  28. import java.util.jar.JarOutputStream;

  29. import org.apache.commons.compress.java.util.jar.Pack200;

  30. /**
  31.  * Utility methods for Pack200.
  32.  *
  33.  * @ThreadSafe
  34.  * @since 1.3
  35.  */
  36. public class Pack200Utils {
  37.     /**
  38.      * Normalizes a JAR archive in-place, so it can be safely signed and packed.
  39.      *
  40.      * <p>
  41.      * As stated in <a href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a> javadocs applying a
  42.      * Pack200 compression to a JAR archive will in general make its signatures invalid. In order to prepare a JAR for signing it should be "normalized" by
  43.      * packing and unpacking it. This is what this method does.
  44.      * </p>
  45.      *
  46.      * <p>
  47.      * Note this methods implicitly sets the segment length to -1.
  48.      * </p>
  49.      *
  50.      * @param jar the JAR archive to normalize
  51.      * @throws IOException if reading or writing fails
  52.      */
  53.     public static void normalize(final File jar) throws IOException {
  54.         normalize(jar, jar, null);
  55.     }

  56.     /**
  57.      * Normalizes a JAR archive, so it can be safely signed and packed.
  58.      *
  59.      * <p>
  60.      * As stated in <a href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a> javadocs applying a
  61.      * Pack200 compression to a JAR archive will in general make its signatures invalid. In order to prepare a JAR for signing it should be "normalized" by
  62.      * packing and unpacking it. This is what this method does.
  63.      * </p>
  64.      *
  65.      * <p>
  66.      * This method does not replace the existing archive but creates a new one.
  67.      * </p>
  68.      *
  69.      * <p>
  70.      * Note this methods implicitly sets the segment length to -1.
  71.      * </p>
  72.      *
  73.      * @param from the JAR archive to normalize
  74.      * @param to   the normalized archive
  75.      * @throws IOException if reading or writing fails
  76.      */
  77.     public static void normalize(final File from, final File to) throws IOException {
  78.         normalize(from, to, null);
  79.     }

  80.     /**
  81.      * Normalizes a JAR archive, so it can be safely signed and packed.
  82.      *
  83.      * <p>
  84.      * As stated in <a href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a> javadocs applying a
  85.      * Pack200 compression to a JAR archive will in general make its signatures invalid. In order to prepare a JAR for signing it should be "normalized" by
  86.      * packing and unpacking it. This is what this method does.
  87.      * </p>
  88.      *
  89.      * <p>
  90.      * This method does not replace the existing archive but creates a new one.
  91.      * </p>
  92.      *
  93.      * @param from  the JAR archive to normalize
  94.      * @param to    the normalized archive
  95.      * @param props properties to set for the pack operation. This method will implicitly set the segment limit to -1.
  96.      * @throws IOException if reading or writing fails
  97.      */
  98.     public static void normalize(final File from, final File to, Map<String, String> props) throws IOException {
  99.         if (props == null) {
  100.             props = new HashMap<>();
  101.         }
  102.         props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
  103.         final Path tempFile = Files.createTempFile("commons-compress", "pack200normalize");
  104.         try {
  105.             try (OutputStream fos = Files.newOutputStream(tempFile);
  106.                     JarFile jarFile = new JarFile(from)) {
  107.                 final Pack200.Packer packer = Pack200.newPacker();
  108.                 packer.properties().putAll(props);
  109.                 packer.pack(jarFile, fos);
  110.             }
  111.             final Pack200.Unpacker unpacker = Pack200.newUnpacker();
  112.             try (JarOutputStream jos = new JarOutputStream(Files.newOutputStream(to.toPath()))) {
  113.                 unpacker.unpack(tempFile.toFile(), jos);
  114.             }
  115.         } finally {
  116.             Files.delete(tempFile);
  117.         }
  118.     }

  119.     /**
  120.      * Normalizes a JAR archive in-place, so it can be safely signed and packed.
  121.      *
  122.      * <p>
  123.      * As stated in <a href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a> javadocs applying a
  124.      * Pack200 compression to a JAR archive will in general make its signatures invalid. In order to prepare a JAR for signing it should be "normalized" by
  125.      * packing and unpacking it. This is what this method does.
  126.      * </p>
  127.      *
  128.      * @param jar   the JAR archive to normalize
  129.      * @param props properties to set for the pack operation. This method will implicitly set the segment limit to -1.
  130.      * @throws IOException if reading or writing fails
  131.      */
  132.     public static void normalize(final File jar, final Map<String, String> props) throws IOException {
  133.         normalize(jar, jar, props);
  134.     }

  135.     private Pack200Utils() {
  136.     }
  137. }