dol: initial dol commit
[jump.git] / dol / src / dol / util / Copier.java
1 /* $Id: Copier.java 1 2010-02-24 13:03:05Z haidw $ */
2 package dol.util;
3
4 import java.io.File;
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;
10
11 /**
12  * Class to recursively copy a directory.
13  */
14 public class Copier {
15
16     String _destination; //target directory
17
18     /**
19      * Copy a file.
20      *
21      * @param from source file
22      * @param to destination file
23      */
24     public void copyFile(File from, File to)
25             throws IOException {
26
27         FileChannel srcChannel = new FileInputStream(from).getChannel();
28         FileChannel dstChannel = new FileOutputStream(to).getChannel();
29         try {
30             dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
31             srcChannel.close();
32             dstChannel.close();
33         }
34         finally {
35           if (srcChannel != null)
36               srcChannel.close();
37
38           if (dstChannel != null)
39               dstChannel.close();
40         }
41     }
42
43     /**
44      * Method which defines what is done for each file found in the
45      * directory tree: It is copied to the corresponding target directory.
46      *
47      * @param filename file to process
48      * @param directory the subdirectory currently processed
49      */
50     protected void processFile(String filename, String directory)
51             throws IOException {
52         File from = new File(filename);
53         File to = new File(_destination + directory + from.getName());
54         copyFile(from, to);
55         //System.out.println("Copied " + from.getPath() + " to "
56         //        + to.getPath());
57     }
58
59     /**
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.
63      *
64      * @param path the path of the directory to be browsed
65      * @param currentDir the subdirectory currently explored
66      */
67     protected void browseDirectoryTree(String path, String currentDir)
68             throws IOException {
69         File file = new File(path);
70
71         if (!file.exists()) return;
72         if (!file.isDirectory()) return;
73
74         //loop through files in directory
75         String[] files = file.list();
76
77         for (int k = 0; k < files.length; k++) {
78             File newfile = new File(file.getPath(), files[k]);
79             if (newfile.isFile())
80                 processFile(path + System.getProperty("file.separator")
81                         + files[k], currentDir);
82             else if (newfile.isDirectory()) {
83                 File newDirectory = new File(_destination + currentDir
84                         + files[k]);
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"));
92             }
93         }
94     }
95
96     /**
97      * Recursively copy a directory.
98      *
99      * @param source source directory
100      * @param destination target directory
101      */
102     public void copy(File source, File destination)
103             throws IOException {
104         try {
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("\\\\", "/");
113                 
114                 JarFile jar = new JarFile(jarName);
115                 JarCopier copier = new JarCopier();
116                 copier.copy(jar, sourceName, destination.toString());
117                 return;
118             }
119
120             _destination = destination.getPath();
121
122             if (!source.isDirectory())
123                 throw (new IOException("Source is not a directory."));
124
125             File destinationDir = new File(_destination);
126             destinationDir.mkdirs();
127
128             browseDirectoryTree(source.getPath(),
129                     System.getProperty("file.separator"));
130         }
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());
136             throw e;
137         }
138     }
139
140     /**
141      * Test the implementation.
142      *
143      * @param args args[0] specifies the source directory,
144      *             args[1] specifies the destination directory
145      */
146     public static void main(String args[])
147             throws Exception {
148         Copier copier = new Copier();
149         if (args.length == 2)
150             copier.copy(new File(args[0]), new File(args[1]));
151         else
152             System.out.println("usage: java Copier "
153                     + "sourceDirectory targetDirectory");
154     }
155 }