Search in sources :

Example 11 with Unpacker

use of java.util.jar.Pack200.Unpacker in project LibreraReader by foobnix.

the class Pack200Utils method normalize.

/**
 * Normalizes a JAR archive so it can be safely signed and packed.
 *
 * <p>As stated in <a
 * href="http://download.oracle.com/javase/1.5.0/docs/api/java/util/jar/Pack200.Packer.html">Pack200.Packer's</a>
 * 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.</p>
 *
 * <p>This method does not replace the existing archive but creates
 * a new one.</p>
 *
 * @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<String, String> 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();
        }
    }
}
Also used : Pack200(java.util.jar.Pack200) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 12 with Unpacker

use of java.util.jar.Pack200.Unpacker in project runwar by cfmlprojects.

the class LaunchUtil method unpack.

public static void unpack(File inFile) {
    JarOutputStream out = null;
    InputStream in = null;
    String inName = inFile.getPath();
    String outName;
    if (inName.endsWith(".pack.gz")) {
        outName = inName.substring(0, inName.length() - 8);
    } else if (inName.endsWith(".pack")) {
        outName = inName.substring(0, inName.length() - 5);
    } else {
        outName = inName + ".unpacked";
    }
    try {
        Pack200.Unpacker unpacker = Pack200.newUnpacker();
        out = new JarOutputStream(new FileOutputStream(outName));
        in = new FileInputStream(inName);
        if (inName.endsWith(".gz"))
            in = new GZIPInputStream(in);
        unpacker.unpack(in, out);
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException ex) {
                System.err.println("Error closing file: " + ex.getMessage());
            }
        }
        if (out != null) {
            try {
                out.flush();
                out.close();
            } catch (IOException ex) {
                System.err.println("Error closing file: " + ex.getMessage());
            }
        }
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) Pack200(java.util.jar.Pack200) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) JarInputStream(java.util.jar.JarInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 13 with Unpacker

use of java.util.jar.Pack200.Unpacker in project ceylon by eclipse.

the class JarUtils method repack.

/**
 * Takes the jar generated file and repacks it using pack200 in an attempt
 * to reduce the file size. This is only worth doing on jars containing class files.
 */
public static void repack(File outputFile, Logger log) throws IOException, FileNotFoundException {
    Packer packer = Pack200.newPacker();
    packer.properties().put(Packer.EFFORT, "9");
    packer.properties().put(Packer.KEEP_FILE_ORDER, Packer.FALSE);
    packer.properties().put(Packer.DEFLATE_HINT, Packer.TRUE);
    packer.properties().put(Packer.SEGMENT_LIMIT, "-1");
    packer.properties().put(Packer.MODIFICATION_TIME, Packer.LATEST);
    File tmp = File.createTempFile("ceylon-jarutils-", ".pack200", outputFile.getParentFile());
    try {
        try (OutputStream out = new FileOutputStream(tmp)) {
            try (JarFile in = new JarFile(outputFile)) {
                packer.pack(in, out);
            }
        }
        try (JarOutputStream outStream = new JarOutputStream(new FileOutputStream(outputFile))) {
            outStream.setLevel(9);
            Unpacker unpacker = Pack200.newUnpacker();
            unpacker.unpack(tmp, outStream);
        }
    } finally {
        tmp.delete();
    }
    log.debug("[repacked jar: " + outputFile.getPath() + "]");
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) FileOutputStream(java.io.FileOutputStream) JarOutputStream(java.util.jar.JarOutputStream) Unpacker(java.util.jar.Pack200.Unpacker) JarFile(java.util.jar.JarFile) JarFile(java.util.jar.JarFile) File(java.io.File) Packer(java.util.jar.Pack200.Packer)

Example 14 with Unpacker

use of java.util.jar.Pack200.Unpacker in project Manga by herrlock.

the class Launcher method unpackArchives.

/**
 * Unpacks all files ending with .jar.pack.gz with first gzip and second unpack200
 */
private static void unpackArchives() {
    // check the lib-folder
    Path lib = Paths.get(".", "lib");
    // create an unpacker
    Unpacker unpacker = Pack200.newUnpacker();
    // always deflate the unpacked jars
    unpacker.properties().put(Unpacker.DEFLATE_HINT, Unpacker.TRUE);
    // find all ".jar.pack.gz"-files
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(lib, "*.jar.pack.gz")) {
        // iterate over the packed files
        for (Path packedFile : directoryStream) {
            // the name of the packed file
            String oldFileName = packedFile.toFile().getName();
            // get the new filename
            String newFileName = oldFileName.replace(".pack.gz", "");
            // the Path to the new file
            Path newFile = lib.resolve(newFileName);
            // read from GZIP
            try (GZIPInputStream in = new GZIPInputStream(Files.newInputStream(packedFile))) {
                // write to jar
                try (JarOutputStream out = new JarOutputStream(Files.newOutputStream(newFile))) {
                    // unpack the packed and gzipped file
                    unpacker.unpack(in, out);
                }
            }
            // delete the old file if unpacking was successful
            Files.delete(packedFile);
        }
    } catch (IOException ex) {
        throw new IllegalStateException("Cannot unpack archives", ex);
    }
}
Also used : Path(java.nio.file.Path) GZIPInputStream(java.util.zip.GZIPInputStream) JarOutputStream(java.util.jar.JarOutputStream) Unpacker(java.util.jar.Pack200.Unpacker) IOException(java.io.IOException)

Aggregations

JarOutputStream (java.util.jar.JarOutputStream)13 Pack200 (java.util.jar.Pack200)9 File (java.io.File)8 FileOutputStream (java.io.FileOutputStream)8 GZIPInputStream (java.util.zip.GZIPInputStream)7 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 JarFile (java.util.jar.JarFile)6 BufferedInputStream (java.io.BufferedInputStream)5 Unpacker (java.util.jar.Pack200.Unpacker)5 BufferedOutputStream (java.io.BufferedOutputStream)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 OutputStream (java.io.OutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 PrintStream (java.io.PrintStream)2 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 ListIterator (java.util.ListIterator)2 Map (java.util.Map)2