1 /* $Id: Copier.java 1 2010-02-24 13:03:05Z haidw $ */
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8 import java.nio.channels.FileChannel;
9 import java.util.jar.JarFile;
12 * Class to recursively copy a directory.
16 String _destination; //target directory
21 * @param from source file
22 * @param to destination file
24 public void copyFile(File from, File to)
27 FileChannel srcChannel = new FileInputStream(from).getChannel();
28 FileChannel dstChannel = new FileOutputStream(to).getChannel();
30 dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
35 if (srcChannel != null)
38 if (dstChannel != null)
44 * Method which defines what is done for each file found in the
45 * directory tree: It is copied to the corresponding target directory.
47 * @param filename file to process
48 * @param directory the subdirectory currently processed
50 protected void processFile(String filename, String directory)
52 File from = new File(filename);
53 File to = new File(_destination + directory + from.getName());
55 //System.out.println("Copied " + from.getPath() + " to "
60 * Iterate through the given directory tree. For each file found in
61 * the file, the method
62 * {@link #processFile(java.lang.String, java.lang.String)} is called.
64 * @param path the path of the directory to be browsed
65 * @param currentDir the subdirectory currently explored
67 protected void browseDirectoryTree(String path, String currentDir)
69 File file = new File(path);
71 if (!file.exists()) return;
72 if (!file.isDirectory()) return;
74 //loop through files in directory
75 String[] files = file.list();
77 for (int k = 0; k < files.length; k++) {
78 File newfile = new File(file.getPath(), files[k]);
80 processFile(path + System.getProperty("file.separator")
81 + files[k], currentDir);
82 else if (newfile.isDirectory()) {
83 File newDirectory = new File(_destination + currentDir
85 newDirectory.mkdirs();
86 //System.out.println("Created directory "
87 // + newDirectory.getPath());
88 browseDirectoryTree(file.getPath()
89 + System.getProperty("file.separator")
90 + files[k], currentDir + files[k]
91 + System.getProperty("file.separator"));
97 * Recursively copy a directory.
99 * @param source source directory
100 * @param destination target directory
102 public void copy(File source, File destination)
105 //treat jar archives differently
106 if (source.toString().contains("dol.jar!")) {
107 String jarName = source.toString();
108 jarName = jarName.substring(jarName.indexOf("file:") + 5, jarName.lastIndexOf("!"));
109 String sourceName = source.toString();
110 sourceName = sourceName.substring(
111 sourceName.lastIndexOf("!") + 2);
112 sourceName = sourceName.replaceAll("\\\\", "/");
114 JarFile jar = new JarFile(jarName);
115 JarCopier copier = new JarCopier();
116 copier.copy(jar, sourceName, destination.toString());
120 _destination = destination.getPath();
122 if (!source.isDirectory())
123 throw (new IOException("Source is not a directory."));
125 File destinationDir = new File(_destination);
126 destinationDir.mkdirs();
128 browseDirectoryTree(source.getPath(),
129 System.getProperty("file.separator"));
131 catch (IOException e) {
132 System.out.println("An error has occured while copying \""
133 + source.getPath() + "\" to \""
134 + destination.getPath() + "\":");
135 System.out.println(e.getMessage());
141 * Test the implementation.
143 * @param args args[0] specifies the source directory,
144 * args[1] specifies the destination directory
146 public static void main(String args[])
148 Copier copier = new Copier();
149 if (args.length == 2)
150 copier.copy(new File(args[0]), new File(args[1]));
152 System.out.println("usage: java Copier "
153 + "sourceDirectory targetDirectory");