1 /* $Id: JarCopier.java 1 2010-02-24 13:03:05Z haidw $ */
\r
5 import java.io.FileOutputStream;
\r
6 import java.io.IOException;
\r
7 import java.io.InputStream;
\r
8 import java.io.OutputStream;
\r
9 import java.util.Enumeration;
\r
10 import java.util.Vector;
\r
11 import java.util.jar.JarEntry;
\r
12 import java.util.jar.JarFile;
\r
15 * Class to copy files from a jar archive.
\r
17 public class JarCopier {
\r
20 * Copy a file from the specified jar file to the specified
\r
21 * destination file. The file path must be specified using forward
\r
22 * slashes, for instance, META-INF/MANIFEST.MF.
\r
24 * @param jar jar archive
\r
25 * @param from file to extract from jar archive
\r
26 * @param to file to copy the file to
\r
28 public void copyFile(JarFile jar, String from, String to) {
\r
30 InputStream in = jar.getInputStream(jar.getEntry(from));
\r
31 OutputStream out = new FileOutputStream(new File(to));
\r
33 while ((c = in.read()) != -1) {
\r
39 catch (IOException e) {
\r
40 System.out.println("An error has occured while copying \""
\r
41 + from + "\" to \"" + to + "\":");
\r
42 System.out.println(e.getMessage());
\r
47 * Get all files located in the given path of the given jar archive.
\r
48 * The path must be specified with forward slashes.
\r
50 * @param jar jar archive
\r
51 * @param path path to search for files
\r
52 * @return vector of all files in the specified path
\r
54 public Vector<String> getFilesInDirectory(JarFile jar, String path) {
\r
55 Vector<String> filelist = new Vector<String>();
\r
56 Enumeration<JarEntry> entries = jar.entries();
\r
58 while (entries.hasMoreElements()) {
\r
59 String entry = entries.nextElement().toString();
\r
60 if (entry.startsWith(path)) {
\r
61 filelist.add(entry);
\r
68 * Recursively copy a directory from the specified jar archive.
\r
70 * @param jar jar archive
\r
71 * @param srcDir source directory
\r
72 * @param destDir target directory
\r
74 public void copy(JarFile jar, String srcDir, String destDir)
\r
75 throws IOException {
\r
76 File directory = new File(destDir);
\r
77 if (!directory.exists()) {
\r
80 for (String file : getFilesInDirectory(jar, srcDir)) {
\r
81 String dir = file.substring(0, file.lastIndexOf("/"));
\r
82 directory = new File(destDir
\r
83 + System.getProperty("file.separator")
\r
84 + dir.substring(srcDir.length()));
\r
85 if (!directory.exists()) {
\r
88 if (!file.endsWith("/")) {
\r
89 copyFile(jar, file, destDir
\r
90 + System.getProperty("file.separator")
\r
91 + file.substring(srcDir.length() + 1));
\r
97 * Copy the specified directory from the jar archive to the specified
\r
100 * @param args args[0] specifies the jar file to read from,
\r
101 * args[1] specifies the source directory,
\r
102 * args[2] specifies the destination directory
\r
104 public static void main(String args[])
\r
106 JarCopier copier = new JarCopier();
\r
107 JarFile jar = new JarFile(args[0]);
\r
108 if (args.length == 3)
\r
109 copier.copy(jar, args[1], args[2]);
\r
111 System.out.println("usage: java JarCopier "
\r
112 + "jarFile sourceDirectory targetDirectory");
\r