dol: create target object folders when generating
[jump.git] / dol / src / dol / util / JarCopier.java
1 /* $Id: JarCopier.java 1 2010-02-24 13:03:05Z haidw $ */\r
2 package dol.util;\r
3 \r
4 import java.io.File;\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
13 \r
14 /**\r
15  * Class to copy files from a jar archive.\r
16  */\r
17 public class JarCopier {\r
18 \r
19     /**\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
23      *\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
27      */\r
28     public void copyFile(JarFile jar, String from, String to) {\r
29         try {\r
30             InputStream in = jar.getInputStream(jar.getEntry(from));\r
31             OutputStream out = new FileOutputStream(new File(to));\r
32             int c;\r
33             while ((c = in.read()) != -1) {\r
34                 out.write(c);\r
35             }\r
36             in.close();\r
37             out.close();\r
38         }\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
43         }\r
44     }\r
45 \r
46     /**\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
49      *\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
53      */\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
57 \r
58         while (entries.hasMoreElements()) {\r
59             String entry = entries.nextElement().toString();\r
60             if (entry.startsWith(path)) {\r
61                 filelist.add(entry);\r
62             }\r
63         }\r
64         return filelist;\r
65     }\r
66 \r
67     /**\r
68      * Recursively copy a directory from the specified jar archive.\r
69      *\r
70      * @param jar jar archive\r
71      * @param srcDir source directory\r
72      * @param destDir target directory\r
73      */\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
78             directory.mkdirs();\r
79         }\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
86                 directory.mkdirs();\r
87             }\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
92             }\r
93         }\r
94     }\r
95 \r
96     /**\r
97      * Copy the specified directory from the jar archive to the specified\r
98      * destination.\r
99      *\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
103      */\r
104     public static void main(String args[])\r
105             throws Exception {\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
110         else\r
111             System.out.println("usage: java JarCopier "\r
112                     + "jarFile sourceDirectory targetDirectory");\r
113     }\r
114 }