Что является Java, эквивалентным этой команде банки:
C:\>jar cvf myjar.jar directory
Я хотел бы создать этот файл банки программно, поскольку меня нельзя уверить что jar
команда будет на системном пути, куда я мог просто выполнить внешний процесс.
Править: Все, что я хочу, должно заархивировать (и сжатие) каталог. Не должен следовать никакому стандарту Java. Т.е.: стандартная zip прекрасна.
// These are the files to include in the ZIP file
String[] source = new String[]{"source1", "source2"};
// Create a buffer for reading the files
byte[] buf = new byte[1024];
try {
// Create the ZIP file
String target = "target.zip";
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(target));
// Compress the files
for (int i=0; i<source.length; i++) {
FileInputStream in = new FileInputStream(source[i]);
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(source[i]));
// Transfer bytes from the file to the ZIP file
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
// Complete the entry
out.closeEntry();
in.close();
}
// Complete the ZIP file
out.close();
} catch (IOException e) {
}
Вы также можете использовать ответ из этого сообщения Как использовать JarOutputStream для создания файла JAR?
Все, что вам понадобится, находится в пакете java.util.jar:
http://java.sun.com/javase/6/docs/api/java/util/jar/package-summary.html