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();
}
}
}
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());
}
}
}
}
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() + "]");
}
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);
}
}
Aggregations