1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*ef39d40dSAndrew Rist  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*ef39d40dSAndrew Rist  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19*ef39d40dSAndrew Rist  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package helper;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import java.io.File;
26cdf0e10cSrcweir import java.io.FileInputStream;
27cdf0e10cSrcweir import java.io.FileOutputStream;
28cdf0e10cSrcweir import java.io.InputStream;
29cdf0e10cSrcweir import java.io.OutputStream;
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir /**
33cdf0e10cSrcweir  * This class deliver some functionality to copy files.
34cdf0e10cSrcweir  */
35cdf0e10cSrcweir public class FileTools {
36cdf0e10cSrcweir 
37cdf0e10cSrcweir     /**
38cdf0e10cSrcweir      * Copies all files under srcDir to dstDir.
39cdf0e10cSrcweir      * If dstDir does not exist, it will be created.
40cdf0e10cSrcweir      * @param srcDir the source directory
41cdf0e10cSrcweir      * @param dstDir the destination direcotry
42cdf0e10cSrcweir      * @throws java.io.IOException throws java.io.IOException if something failes
43cdf0e10cSrcweir      */
copyDirectory(File srcDir, File dstDir)44cdf0e10cSrcweir     public static void copyDirectory(File srcDir, File dstDir)
45cdf0e10cSrcweir            throws java.io.IOException {
46cdf0e10cSrcweir         copyDirectory(srcDir, dstDir, new String[]{});
47cdf0e10cSrcweir     }
48cdf0e10cSrcweir     /**
49cdf0e10cSrcweir      * Copies all files under srcDir to dstDir except Files given in the
50cdf0e10cSrcweir      * ignore list. This files will not be copied.
51cdf0e10cSrcweir      * If dstDir does not exist, it will be created.
52cdf0e10cSrcweir      * @param srcDir the source directory
53cdf0e10cSrcweir      * @param dstDir the destination direcotry
54cdf0e10cSrcweir      * @param ignore a list of files which should not be copied
55cdf0e10cSrcweir      * @throws java.io.IOException throws java.io.IOException if something failes
56cdf0e10cSrcweir      */
copyDirectory(File srcDir, File dstDir, String[] ignore)57cdf0e10cSrcweir     public static void copyDirectory(File srcDir, File dstDir, String[] ignore)
58cdf0e10cSrcweir            throws java.io.IOException {
59cdf0e10cSrcweir 
60cdf0e10cSrcweir         for (int i=0; i<ignore.length;i++){
61cdf0e10cSrcweir             if (srcDir.getName().endsWith(ignore[i])) {
62cdf0e10cSrcweir                 return;
63cdf0e10cSrcweir             }
64cdf0e10cSrcweir         }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir         if (srcDir.isDirectory()) {
67cdf0e10cSrcweir             if (!dstDir.exists()) {
68cdf0e10cSrcweir                 dstDir.mkdir();
69cdf0e10cSrcweir             }
70cdf0e10cSrcweir 
71cdf0e10cSrcweir             String[] files = srcDir.list();
72cdf0e10cSrcweir             for (int i=0; i< files.length; i++) {
73cdf0e10cSrcweir                 copyDirectory(new File(srcDir, files[i]), new File(dstDir, files[i]), ignore);
74cdf0e10cSrcweir             }
75cdf0e10cSrcweir         } else {
76cdf0e10cSrcweir             // This method is implemented in e1071 Copying a File
77cdf0e10cSrcweir             copyFile(srcDir, dstDir);
78cdf0e10cSrcweir         }
79cdf0e10cSrcweir     }
80cdf0e10cSrcweir 
81cdf0e10cSrcweir     /**
82cdf0e10cSrcweir      * Copies src file to dst file. If the dst file does not exist, it is created
83cdf0e10cSrcweir      * @param src the source file
84cdf0e10cSrcweir      * @param dst the destination file
85cdf0e10cSrcweir      * @throws java.io.IOException throws java.io.IOException if something failes
86cdf0e10cSrcweir      */
copyFile(File src, File dst)87cdf0e10cSrcweir     public static void copyFile(File src, File dst) throws java.io.IOException {
88cdf0e10cSrcweir         InputStream in = new FileInputStream(src);
89cdf0e10cSrcweir         OutputStream out = new FileOutputStream(dst);
90cdf0e10cSrcweir 
91cdf0e10cSrcweir         // Transfer bytes from in to out
92cdf0e10cSrcweir         byte[] buf = new byte[1024];
93cdf0e10cSrcweir         int len;
94cdf0e10cSrcweir         while ((len = in.read(buf)) > 0) {
95cdf0e10cSrcweir             out.write(buf, 0, len);
96cdf0e10cSrcweir         }
97cdf0e10cSrcweir         in.close();
98cdf0e10cSrcweir         out.close();
99cdf0e10cSrcweir     }
100cdf0e10cSrcweir     /**
101cdf0e10cSrcweir      * Deletes all files and subdirectories under dir and the directory itself.
102cdf0e10cSrcweir      * Returns true if all deletions were successful.
103cdf0e10cSrcweir      * If the deletion fails, the method the method continues to delete rest
104cdf0e10cSrcweir      * of the files and returns false.
105cdf0e10cSrcweir      * @return Returns true if all deletions were successful, else false.
106cdf0e10cSrcweir      * @param dir the directory to delete
107cdf0e10cSrcweir      */
deleteDir(File dir)108cdf0e10cSrcweir     public static boolean deleteDir(File dir) {
109cdf0e10cSrcweir 
110cdf0e10cSrcweir         // if (! cleanDir(dir)) return false;
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         // The directory is now empty so delete it
113cdf0e10cSrcweir         // return dir.delete();
114cdf0e10cSrcweir         return cleanDir(dir);
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
117cdf0e10cSrcweir     /**
118cdf0e10cSrcweir      * Deletes all files and subdirectories under dir.
119cdf0e10cSrcweir      * Returns true if all deletions were successful.
120cdf0e10cSrcweir      * If a deletion fails, the method continues to delete rest of the files.
121cdf0e10cSrcweir      * @return Returns true if all deletions were successful, else false.
122cdf0e10cSrcweir      * @param dir the directory to clean from content
123cdf0e10cSrcweir      */
124cdf0e10cSrcweir     // public static boolean cleanDir(File dir){
125cdf0e10cSrcweir     //
126cdf0e10cSrcweir     //     boolean success = true;
127cdf0e10cSrcweir     //     if (dir.isDirectory()){
128cdf0e10cSrcweir     //         File [] theFiles = dir.listFiles();
129cdf0e10cSrcweir     //
130cdf0e10cSrcweir     //         if (theFiles.length != 0 )
131cdf0e10cSrcweir     //             for (int i = 0; i < theFiles.length; i++){
132cdf0e10cSrcweir     //                 success &= theFiles[i].delete();
133cdf0e10cSrcweir     //             }
134cdf0e10cSrcweir     //     }
135cdf0e10cSrcweir     //     return success;
136cdf0e10cSrcweir     // }
137cdf0e10cSrcweir 
cleanDir(File dir)138cdf0e10cSrcweir    public static boolean cleanDir(File dir)
139cdf0e10cSrcweir         {
140cdf0e10cSrcweir             if (dir.isDirectory())
141cdf0e10cSrcweir             {
142cdf0e10cSrcweir                 String[] children = dir.list();
143cdf0e10cSrcweir                 for (int i=0; i<children.length; i++)
144cdf0e10cSrcweir                 {
145cdf0e10cSrcweir                     boolean success = cleanDir(new File(dir, children[i]));
146cdf0e10cSrcweir                     if (!success)
147cdf0e10cSrcweir                     {
148cdf0e10cSrcweir                         return false;
149cdf0e10cSrcweir                     }
150cdf0e10cSrcweir                 }
151cdf0e10cSrcweir             }
152cdf0e10cSrcweir 
153cdf0e10cSrcweir             // The directory is now empty so delete it
154cdf0e10cSrcweir             return dir.delete();
155cdf0e10cSrcweir         }
156cdf0e10cSrcweir }
157