Java Reference
In-Depth Information
// Get the current directory for later use
String currentDirectory = System.getProperty("user.dir");
// Create the JAR file
try (JarOutputStream jos = new JarOutputStream(
new BufferedOutputStream(
new FileOutputStream(jarFileName)
), manifest)) {
// Set the compression level to best compression
jos.setLevel(Deflater.BEST_COMPRESSION);
// Add each entry to JAR file
for (int i = 0; i < jarEntries.length; i++) {
// Make sure the entry file exists
File entryFile = new File(jarEntries[i]);
if (!entryFile.exists()) {
System.out.println("The entry file " +
entryFile.getAbsolutePath() +
" does not exist");
System.out.println("Aborted processing.");
return;
}
// Create a JarEntry object
JarEntry je = new JarEntry(jarEntries[i]);
// Add jar entry object to JAR file
jos.putNextEntry(je);
// Add the entry's contents to the JAR file
addEntryContent(jos, jarEntries[i]);
// Inform the JAR output stream that we are done
// working with the current entry
jos.closeEntry();
}
System.out.println("Output has been written to " +
currentDirectory + File.separator + jarFileName);
}
catch (IOException e) {
e.printStackTrace();
}
}
public static void addEntryContent(JarOutputStream jos, String entryFileName)
throws IOException, FileNotFoundException {
// Create an input stream to read data from the entry file
BufferedInputStream bis =
new BufferedInputStream(new FileInputStream(entryFileName));
Search WWH ::




Custom Search