use of java.util.jar.Pack200 in project Lucee by lucee.
the class Pack200Util method jar2pack.
public static void jar2pack(InputStream is, OutputStream os, boolean closeIS, boolean closeOS) throws IOException {
// Create the Packer object
Packer packer = Pack200.newPacker();
// Initialize the state by setting the desired properties
Map p = packer.properties();
// take more time choosing codings for better compression
// default is "5"
p.put(Packer.EFFORT, "7");
// use largest-possible archive segments (>10% better compression).
p.put(Packer.SEGMENT_LIMIT, "-1");
// reorder files for better compression.
p.put(Packer.KEEP_FILE_ORDER, Packer.FALSE);
// smear modification times to a single value.
p.put(Packer.MODIFICATION_TIME, Packer.LATEST);
// ignore all JAR deflation requests,
// transmitting a single request to use "store" mode.
p.put(Packer.DEFLATE_HINT, Packer.FALSE);
// discard debug attributes
p.put(Packer.CODE_ATTRIBUTE_PFX + "LineNumberTable", Packer.STRIP);
// throw an error if an attribute is unrecognized
p.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);
JarInputStream jis = null;
os = new GZIPOutputStream(os);
PrintStream err = System.err;
try {
System.setErr(new PrintStream(new DevNullOutputStream()));
jis = new JarInputStream(is);
packer.pack(jis, os);
} finally {
System.setErr(err);
if (closeIS)
Util.closeEL(jis);
if (closeOS)
Util.closeEL(os);
}
}
use of java.util.jar.Pack200 in project Lucee by lucee.
the class Pack200Util method pack2Jar.
public static void pack2Jar(InputStream is, OutputStream os, boolean closeIS, boolean closeOS) throws IOException {
Unpacker unpacker = Pack200.newUnpacker();
SortedMap<String, String> p = unpacker.properties();
p.put(Unpacker.DEFLATE_HINT, Unpacker.TRUE);
is = new GZIPInputStream(is);
JarOutputStream jos = null;
try {
jos = new JarOutputStream(os);
unpacker.unpack(is, jos);
jos.finish();
} finally {
if (closeIS)
Util.closeEL(is);
if (closeOS)
Util.closeEL(jos);
}
}
use of java.util.jar.Pack200 in project Spark by igniterealtime.
the class Startup method unpackArchives.
/**
* Converts any pack files in a directory into standard JAR files. Each
* pack file will be deleted after being converted to a JAR. If no
* pack files are found, this method does nothing.
*
* @param libDir the directory containing pack files.
* @param printStatus true if status ellipses should be printed when unpacking.
*/
private void unpackArchives(File libDir, boolean printStatus) {
// Get a list of all packed files in the lib directory.
File[] packedFiles = libDir.listFiles((dir, name) -> {
return name.endsWith(".pack");
});
if (packedFiles == null) {
// Do nothing since no .pack files were found
return;
}
// Unpack each.
boolean unpacked = false;
for (File packedFile : packedFiles) {
try {
String jarName = packedFile.getName().substring(0, packedFile.getName().length() - ".pack".length());
// Delete JAR file with same name if it exists (could be due to upgrade
// from old Wildfire release).
File jarFile = new File(libDir, jarName);
if (jarFile.exists()) {
jarFile.delete();
}
InputStream in = new BufferedInputStream(new FileInputStream(packedFile));
JarOutputStream out = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(new File(libDir, jarName))));
Pack200.Unpacker unpacker = Pack200.newUnpacker();
// Print something so the user knows something is happening.
if (printStatus) {
System.out.print(".");
}
// Call the unpacker
unpacker.unpack(in, out);
in.close();
out.close();
packedFile.delete();
unpacked = true;
} catch (Exception e) {
e.printStackTrace();
}
}
// Print newline if unpacking happened.
if (unpacked && printStatus) {
System.out.println();
}
}
use of java.util.jar.Pack200 in project sis by apache.
the class UnoPkg method createPackage.
/**
* Creates the {@code .oxt} file.
*/
private void createPackage() throws IOException {
final String manifestName = "META-INF/manifest.xml";
final File sourceDirectory = new File(baseDirectory, SOURCE_DIRECTORY);
final File outputDirectory = new File(this.outputDirectory);
final File zipFile = new File(outputDirectory, oxtName + ".oxt");
final File manifestFile = new File(sourceDirectory, manifestName);
final File[] jars = outputDirectory.listFiles(this);
final File[] rdbs = sourceDirectory.listFiles(this);
try (ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipFile))) {
out.setLevel(9);
if (manifestFile.isFile()) {
copyFiltered(manifestFile, out, manifestName);
}
/*
* Copies the RDB files.
*/
for (int i = 0; i < rdbs.length; i++) {
copy(rdbs[i], out, null);
}
/*
* Copies the JAR (and any additional JARs provided in the output directory).
* Do not pack this JAR, because it contains the code needed to inflate the
* PACK200 file.
*/
for (int i = 0; i < jars.length; i++) {
copy(jars[i], out, null);
}
/*
* Copies the dependencies, optionally in a single PACK200 entry.
* We discard most debug information because stack traces are not
* easy to get from an application running in OpenOffice anyway.
*/
Pack200.Packer packer = null;
if (Boolean.parseBoolean(pack200)) {
packer = Pack200.newPacker();
final Map<String, String> p = packer.properties();
// take more time choosing codings for better compression.
p.put(Packer.EFFORT, "9");
// use largest-possible archive segments (>10% better compression).
p.put(Packer.SEGMENT_LIMIT, "-1");
// reorder files for better compression.
p.put(Packer.KEEP_FILE_ORDER, Packer.FALSE);
// smear modification times to a single value.
p.put(Packer.MODIFICATION_TIME, Packer.LATEST);
// discard debug attributes.
p.put(Packer.CODE_ATTRIBUTE_PFX + "LineNumberTable", Packer.STRIP);
// discard debug attributes.
p.put(Packer.CODE_ATTRIBUTE_PFX + "LocalVariableTable", Packer.STRIP);
// discard debug attributes.
p.put(Packer.CLASS_ATTRIBUTE_PFX + "SourceFile", Packer.STRIP);
// transmitting a single request to use "compress" mode.
p.put(Packer.DEFLATE_HINT, Packer.TRUE);
// throw an error if an attribute is unrecognized.
p.put(Packer.UNKNOWN_ATTRIBUTE, Packer.ERROR);
}
for (final Artifact artifact : project.getArtifacts()) {
final String scope = artifact.getScope();
if (Artifact.SCOPE_COMPILE.equalsIgnoreCase(scope) || Artifact.SCOPE_RUNTIME.equalsIgnoreCase(scope)) {
final File file = artifact.getFile();
String name = file.getName();
if (artifact.getGroupId().startsWith(prefixGroup) && !name.startsWith(prefix)) {
name = prefix + name;
}
if (packer != null && name.endsWith(".jar")) {
name = name.substring(0, name.length() - 3) + "pack";
try (JarFile jar = new FilteredJarFile(file)) {
out.putNextEntry(new ZipEntry(name));
packer.pack(jar, out);
out.closeEntry();
}
} else {
copy(file, out, name);
}
}
}
}
}
use of java.util.jar.Pack200 in project sis by apache.
the class PackOutput method pack.
/**
* Creates a Pack200 file from the output JAR, then delete the JAR.
*
* @param out where to write the Pack200. This stream will be closed by this method.
* @throws IOException if an error occurred while packing the JAR.
*/
void pack(final OutputStream out) throws IOException {
if (outputStream != null) {
throw new IllegalStateException("JAR output stream not closed.");
}
final File inputFile = outputJAR;
final Pack200.Packer packer = Pack200.newPacker();
final Map<String, String> p = packer.properties();
// Maximum compression level.
p.put(EFFORT, String.valueOf(9));
// use largest-possible archive segments (>10% better compression).
p.put(SEGMENT_LIMIT, "-1");
// Reorder files for better compression.
p.put(KEEP_FILE_ORDER, FALSE);
// Smear modification times to a single value.
p.put(MODIFICATION_TIME, LATEST);
// Ignore all JAR deflation requests.
p.put(DEFLATE_HINT, TRUE);
// Throw an error if an attribute is unrecognized
p.put(UNKNOWN_ATTRIBUTE, ERROR);
// discard debug attributes.
p.put(CODE_ATTRIBUTE_PFX + "LocalVariableTable", STRIP);
try (JarFile jarFile = new JarFile(inputFile)) {
try (OutputStream deflater = new GZIPOutputStream(out)) {
packer.pack(jarFile, deflater);
}
}
if (!inputFile.delete()) {
throw new IOException("Can't delete temporary file: " + inputFile);
}
}
Aggregations