2010-01-27 19:46:39 +00:00
|
|
|
// Source: http://www.java2s.com/Code/Java/File-Input-Output/CreateJarfile.htm
|
|
|
|
|
|
|
|
package javaCompile;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.util.jar.JarEntry;
|
|
|
|
import java.util.jar.JarOutputStream;
|
|
|
|
import java.util.jar.Manifest;
|
|
|
|
|
|
|
|
public class CreateJarFile {
|
|
|
|
public static int BUFFER_SIZE = 10240;
|
|
|
|
public static void createJarArchive(File archiveFile, File[] tobeJared) {
|
|
|
|
try {
|
|
|
|
byte buffer[] = new byte[BUFFER_SIZE];
|
|
|
|
// Open archive file
|
|
|
|
FileOutputStream stream = new FileOutputStream(archiveFile);
|
|
|
|
JarOutputStream out = new JarOutputStream(stream, new Manifest());
|
|
|
|
|
|
|
|
for (int i = 0; i < tobeJared.length; i++) {
|
|
|
|
if (tobeJared[i] == null || !tobeJared[i].exists()
|
|
|
|
|| tobeJared[i].isDirectory())
|
|
|
|
continue; // Just in case...
|
2010-01-28 22:53:36 +00:00
|
|
|
//System.out.println("Adding " + tobeJared[i].getName());
|
2010-01-27 19:46:39 +00:00
|
|
|
|
|
|
|
// Add archive entry
|
|
|
|
JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
|
|
|
|
jarAdd.setTime(tobeJared[i].lastModified());
|
|
|
|
out.putNextEntry(jarAdd);
|
|
|
|
|
|
|
|
// Write file to archive
|
|
|
|
FileInputStream in = new FileInputStream(tobeJared[i]);
|
|
|
|
while (true) {
|
|
|
|
int nRead = in.read(buffer, 0, buffer.length);
|
|
|
|
if (nRead <= 0)
|
|
|
|
break;
|
|
|
|
out.write(buffer, 0, nRead);
|
|
|
|
}
|
|
|
|
in.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
stream.close();
|
2010-01-28 22:53:36 +00:00
|
|
|
//System.out.println("Adding completed OK");
|
2010-01-27 19:46:39 +00:00
|
|
|
} catch (Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
System.out.println("Error: " + ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|