All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.commons.compress.compressors.pack200.Pack200Utils Maven / Gradle / Ivy

Go to download

Apache Commons Compress software defines an API for working with compression and archive formats. These include: bzip2, gzip, pack200, lzma, xz, Snappy, traditional Unix Compress, DEFLATE, DEFLATE64, LZ4, Brotli, Zstandard and ar, cpio, jar, tar, zip, dump, 7z, arj.

There is a newer version: 1.26.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

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

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.jar.JarFile;
import java.util.jar.JarOutputStream;
import java.util.jar.Pack200;

/**
 * Utility methods for Pack200.
 *
 * @ThreadSafe
 * @since 1.3
 */
public class Pack200Utils {
    private Pack200Utils() { }

    /**
     * Normalizes a JAR archive in-place so it can be safely signed
     * and packed.
     *
     * 

As stated in Pack200.Packer's * 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.

* *

Note this methods implicitly sets the segment length to * -1.

* * @param jar the JAR archive to normalize * @throws IOException if reading or writing fails */ public static void normalize(final File jar) throws IOException { normalize(jar, jar, null); } /** * Normalizes a JAR archive in-place so it can be safely signed * and packed. * *

As stated in Pack200.Packer's * 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.

* * @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. * @throws IOException if reading or writing fails */ public static void normalize(final File jar, final Map props) throws IOException { normalize(jar, jar, props); } /** * Normalizes a JAR archive so it can be safely signed and packed. * *

As stated in Pack200.Packer's * 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.

* *

This method does not replace the existing archive but creates * a new one.

* *

Note this methods implicitly sets the segment length to * -1.

* * @param from the JAR archive to normalize * @param to the normalized archive * @throws IOException if reading or writing fails */ public static void normalize(final File from, final File to) throws IOException { normalize(from, to, null); } /** * Normalizes a JAR archive so it can be safely signed and packed. * *

As stated in Pack200.Packer's * 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.

* *

This method does not replace the existing archive but creates * a new one.

* * @param from the JAR archive to normalize * @param to the normalized archive * @param props properties to set for the pack operation. This * method will implicitly set the segment limit to -1. * @throws IOException if reading or writing fails */ public static void normalize(final File from, final File to, Map props) throws IOException { if (props == null) { props = new HashMap<>(); } props.put(Pack200.Packer.SEGMENT_LIMIT, "-1"); final File tempFile = File.createTempFile("commons-compress", "pack200normalize"); try { try (FileOutputStream fos = new FileOutputStream(tempFile); JarFile jarFile = new JarFile(from)) { final Pack200.Packer packer = Pack200.newPacker(); packer.properties().putAll(props); packer.pack(jarFile, fos); } final Pack200.Unpacker unpacker = Pack200.newUnpacker(); try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(to))) { unpacker.unpack(tempFile, jos); } } finally { if (!tempFile.delete()) { tempFile.deleteOnExit(); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy