--- /dev/null
+/* $Id: JarCopier.java 1 2010-02-24 13:03:05Z haidw $ */\r
+package dol.util;\r
+\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.util.Enumeration;\r
+import java.util.Vector;\r
+import java.util.jar.JarEntry;\r
+import java.util.jar.JarFile;\r
+\r
+/**\r
+ * Class to copy files from a jar archive.\r
+ */\r
+public class JarCopier {\r
+\r
+ /**\r
+ * Copy a file from the specified jar file to the specified\r
+ * destination file. The file path must be specified using forward\r
+ * slashes, for instance, META-INF/MANIFEST.MF.\r
+ *\r
+ * @param jar jar archive\r
+ * @param from file to extract from jar archive\r
+ * @param to file to copy the file to\r
+ */\r
+ public void copyFile(JarFile jar, String from, String to) {\r
+ try {\r
+ InputStream in = jar.getInputStream(jar.getEntry(from));\r
+ OutputStream out = new FileOutputStream(new File(to));\r
+ int c;\r
+ while ((c = in.read()) != -1) {\r
+ out.write(c);\r
+ }\r
+ in.close();\r
+ out.close();\r
+ }\r
+ catch (IOException e) {\r
+ System.out.println("An error has occured while copying \""\r
+ + from + "\" to \"" + to + "\":");\r
+ System.out.println(e.getMessage());\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Get all files located in the given path of the given jar archive.\r
+ * The path must be specified with forward slashes.\r
+ *\r
+ * @param jar jar archive\r
+ * @param path path to search for files\r
+ * @return vector of all files in the specified path\r
+ */\r
+ public Vector<String> getFilesInDirectory(JarFile jar, String path) {\r
+ Vector<String> filelist = new Vector<String>();\r
+ Enumeration<JarEntry> entries = jar.entries();\r
+\r
+ while (entries.hasMoreElements()) {\r
+ String entry = entries.nextElement().toString();\r
+ if (entry.startsWith(path)) {\r
+ filelist.add(entry);\r
+ }\r
+ }\r
+ return filelist;\r
+ }\r
+\r
+ /**\r
+ * Recursively copy a directory from the specified jar archive.\r
+ *\r
+ * @param jar jar archive\r
+ * @param srcDir source directory\r
+ * @param destDir target directory\r
+ */\r
+ public void copy(JarFile jar, String srcDir, String destDir)\r
+ throws IOException {\r
+ File directory = new File(destDir);\r
+ if (!directory.exists()) {\r
+ directory.mkdirs();\r
+ }\r
+ for (String file : getFilesInDirectory(jar, srcDir)) {\r
+ String dir = file.substring(0, file.lastIndexOf("/"));\r
+ directory = new File(destDir\r
+ + System.getProperty("file.separator")\r
+ + dir.substring(srcDir.length()));\r
+ if (!directory.exists()) {\r
+ directory.mkdirs();\r
+ }\r
+ if (!file.endsWith("/")) {\r
+ copyFile(jar, file, destDir\r
+ + System.getProperty("file.separator")\r
+ + file.substring(srcDir.length() + 1));\r
+ }\r
+ }\r
+ }\r
+\r
+ /**\r
+ * Copy the specified directory from the jar archive to the specified\r
+ * destination.\r
+ *\r
+ * @param args args[0] specifies the jar file to read from,\r
+ * args[1] specifies the source directory,\r
+ * args[2] specifies the destination directory\r
+ */\r
+ public static void main(String args[])\r
+ throws Exception {\r
+ JarCopier copier = new JarCopier();\r
+ JarFile jar = new JarFile(args[0]);\r
+ if (args.length == 3)\r
+ copier.copy(jar, args[1], args[2]);\r
+ else\r
+ System.out.println("usage: java JarCopier "\r
+ + "jarFile sourceDirectory targetDirectory");\r
+ }\r
+}
\ No newline at end of file