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