CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
rapid7

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: rapid7/metasploit-framework
Path: blob/master/external/source/msfJavaToolkit/javaCompile/CreateJarFile.java
Views: 11780
1
// Source: http://www.java2s.com/Code/Java/File-Input-Output/CreateJarfile.htm
2
3
package javaCompile;
4
5
import java.io.File;
6
import java.io.FileInputStream;
7
import java.io.FileOutputStream;
8
import java.util.jar.JarEntry;
9
import java.util.jar.JarOutputStream;
10
import java.util.jar.Manifest;
11
12
public class CreateJarFile {
13
public static int BUFFER_SIZE = 10240;
14
public static void createJarArchive(File archiveFile, File[] tobeJared) {
15
try {
16
byte buffer[] = new byte[BUFFER_SIZE];
17
// Open archive file
18
FileOutputStream stream = new FileOutputStream(archiveFile);
19
JarOutputStream out = new JarOutputStream(stream, new Manifest());
20
21
for (int i = 0; i < tobeJared.length; i++) {
22
if (tobeJared[i] == null || !tobeJared[i].exists()
23
|| tobeJared[i].isDirectory())
24
continue; // Just in case...
25
//System.out.println("Adding " + tobeJared[i].getName());
26
27
// Add archive entry
28
JarEntry jarAdd = new JarEntry(tobeJared[i].getName());
29
jarAdd.setTime(tobeJared[i].lastModified());
30
out.putNextEntry(jarAdd);
31
32
// Write file to archive
33
FileInputStream in = new FileInputStream(tobeJared[i]);
34
while (true) {
35
int nRead = in.read(buffer, 0, buffer.length);
36
if (nRead <= 0)
37
break;
38
out.write(buffer, 0, nRead);
39
}
40
in.close();
41
}
42
43
out.close();
44
stream.close();
45
//System.out.println("Adding completed OK");
46
} catch (Exception ex) {
47
ex.printStackTrace();
48
System.out.println("Error: " + ex.getMessage());
49
}
50
}
51
}
52
53