• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 package org.apache.commons.compress.compressors.pack200;
21 
22 import java.io.File;
23 import java.io.FileOutputStream;
24 import java.io.IOException;
25 import java.util.HashMap;
26 import java.util.Map;
27 import java.util.jar.JarFile;
28 import java.util.jar.JarOutputStream;
29 import java.util.jar.Pack200;
30 
31 /**
32  * Utility methods for Pack200.
33  *
34  * @ThreadSafe
35  * @since 1.3
36  */
37 public class Pack200Utils {
Pack200Utils()38     private Pack200Utils() { }
39 
40     /**
41      * Normalizes a JAR archive in-place so it can be safely signed
42      * and packed.
43      *
44      * <p>As stated in <a
45      * href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
46      * javadocs applying a Pack200 compression to a JAR archive will
47      * in general make its sigantures invalid.  In order to prepare a
48      * JAR for signing it should be "normalized" by packing and
49      * unpacking it.  This is what this method does.</p>
50      *
51      * <p>Note this methods implicitly sets the segment length to
52      * -1.</p>
53      *
54      * @param jar the JAR archive to normalize
55      * @throws IOException if reading or writing fails
56      */
normalize(final File jar)57     public static void normalize(final File jar)
58         throws IOException {
59         normalize(jar, jar, null);
60     }
61 
62     /**
63      * Normalizes a JAR archive in-place so it can be safely signed
64      * and packed.
65      *
66      * <p>As stated in <a
67      * href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
68      * javadocs applying a Pack200 compression to a JAR archive will
69      * in general make its sigantures invalid.  In order to prepare a
70      * JAR for signing it should be "normalized" by packing and
71      * unpacking it.  This is what this method does.</p>
72      *
73      * @param jar the JAR archive to normalize
74      * @param props properties to set for the pack operation.  This
75      * method will implicitly set the segment limit to -1.
76      * @throws IOException if reading or writing fails
77      */
normalize(final File jar, final Map<String, String> props)78     public static void normalize(final File jar, final Map<String, String> props)
79         throws IOException {
80         normalize(jar, jar, props);
81     }
82 
83     /**
84      * Normalizes a JAR archive so it can be safely signed and packed.
85      *
86      * <p>As stated in <a
87      * href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
88      * javadocs applying a Pack200 compression to a JAR archive will
89      * in general make its sigantures invalid.  In order to prepare a
90      * JAR for signing it should be "normalized" by packing and
91      * unpacking it.  This is what this method does.</p>
92      *
93      * <p>This method does not replace the existing archive but creates
94      * a new one.</p>
95      *
96      * <p>Note this methods implicitly sets the segment length to
97      * -1.</p>
98      *
99      * @param from the JAR archive to normalize
100      * @param to the normalized archive
101      * @throws IOException if reading or writing fails
102      */
normalize(final File from, final File to)103     public static void normalize(final File from, final File to)
104         throws IOException {
105         normalize(from, to, null);
106     }
107 
108     /**
109      * Normalizes a JAR archive so it can be safely signed and packed.
110      *
111      * <p>As stated in <a
112      * href="https://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
113      * javadocs applying a Pack200 compression to a JAR archive will
114      * in general make its sigantures invalid.  In order to prepare a
115      * JAR for signing it should be "normalized" by packing and
116      * unpacking it.  This is what this method does.</p>
117      *
118      * <p>This method does not replace the existing archive but creates
119      * a new one.</p>
120      *
121      * @param from the JAR archive to normalize
122      * @param to the normalized archive
123      * @param props properties to set for the pack operation.  This
124      * method will implicitly set the segment limit to -1.
125      * @throws IOException if reading or writing fails
126      */
normalize(final File from, final File to, Map<String, String> props)127     public static void normalize(final File from, final File to, Map<String, String> props)
128             throws IOException {
129         if (props == null) {
130             props = new HashMap<>();
131         }
132         props.put(Pack200.Packer.SEGMENT_LIMIT, "-1");
133         final File tempFile = File.createTempFile("commons-compress", "pack200normalize");
134         try {
135             try (FileOutputStream fos = new FileOutputStream(tempFile);
136                     JarFile jarFile = new JarFile(from)) {
137                 final Pack200.Packer packer = Pack200.newPacker();
138                 packer.properties().putAll(props);
139                 packer.pack(jarFile, fos);
140             }
141             final Pack200.Unpacker unpacker = Pack200.newUnpacker();
142             try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(to))) {
143                 unpacker.unpack(tempFile, jos);
144             }
145         } finally {
146             if (!tempFile.delete()) {
147                 tempFile.deleteOnExit();
148             }
149         }
150     }
151 }
152