1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 package helper;
28 
29 import java.io.File;
30 import java.io.FileInputStream;
31 import java.io.FileOutputStream;
32 import java.io.InputStream;
33 import java.io.OutputStream;
34 
35 
36 /**
37  * This class deliver some functionality to copy files.
38  */
39 public class FileTools {
40 
41     /**
42      * Copies all files under srcDir to dstDir.
43      * If dstDir does not exist, it will be created.
44      * @param srcDir the source directory
45      * @param dstDir the destination direcotry
46      * @throws java.io.IOException throws java.io.IOException if something failes
47      */
48     public static void copyDirectory(File srcDir, File dstDir)
49            throws java.io.IOException {
50         copyDirectory(srcDir, dstDir, new String[]{});
51     }
52     /**
53      * Copies all files under srcDir to dstDir except Files given in the
54      * ignore list. This files will not be copied.
55      * If dstDir does not exist, it will be created.
56      * @param srcDir the source directory
57      * @param dstDir the destination direcotry
58      * @param ignore a list of files which should not be copied
59      * @throws java.io.IOException throws java.io.IOException if something failes
60      */
61     public static void copyDirectory(File srcDir, File dstDir, String[] ignore)
62            throws java.io.IOException {
63 
64         for (int i=0; i<ignore.length;i++){
65             if (srcDir.getName().endsWith(ignore[i])) {
66                 return;
67             }
68         }
69 
70         if (srcDir.isDirectory()) {
71             if (!dstDir.exists()) {
72                 dstDir.mkdir();
73             }
74 
75             String[] files = srcDir.list();
76             for (int i=0; i< files.length; i++) {
77                 copyDirectory(new File(srcDir, files[i]), new File(dstDir, files[i]), ignore);
78             }
79         } else {
80             // This method is implemented in e1071 Copying a File
81             copyFile(srcDir, dstDir);
82         }
83     }
84 
85     /**
86      * Copies src file to dst file. If the dst file does not exist, it is created
87      * @param src the source file
88      * @param dst the destination file
89      * @throws java.io.IOException throws java.io.IOException if something failes
90      */
91     public static void copyFile(File src, File dst) throws java.io.IOException {
92         InputStream in = new FileInputStream(src);
93         OutputStream out = new FileOutputStream(dst);
94 
95         // Transfer bytes from in to out
96         byte[] buf = new byte[1024];
97         int len;
98         while ((len = in.read(buf)) > 0) {
99             out.write(buf, 0, len);
100         }
101         in.close();
102         out.close();
103     }
104     /**
105      * Deletes all files and subdirectories under dir and the directory itself.
106      * Returns true if all deletions were successful.
107      * If the deletion fails, the method the method continues to delete rest
108      * of the files and returns false.
109      * @return Returns true if all deletions were successful, else false.
110      * @param dir the directory to delete
111      */
112     public static boolean deleteDir(File dir) {
113 
114         // if (! cleanDir(dir)) return false;
115 
116         // The directory is now empty so delete it
117         // return dir.delete();
118         return cleanDir(dir);
119     }
120 
121     /**
122      * Deletes all files and subdirectories under dir.
123      * Returns true if all deletions were successful.
124      * If a deletion fails, the method continues to delete rest of the files.
125      * @return Returns true if all deletions were successful, else false.
126      * @param dir the directory to clean from content
127      */
128     // public static boolean cleanDir(File dir){
129     //
130     //     boolean success = true;
131     //     if (dir.isDirectory()){
132     //         File [] theFiles = dir.listFiles();
133     //
134     //         if (theFiles.length != 0 )
135     //             for (int i = 0; i < theFiles.length; i++){
136     //                 success &= theFiles[i].delete();
137     //             }
138     //     }
139     //     return success;
140     // }
141 
142    public static boolean cleanDir(File dir)
143         {
144             if (dir.isDirectory())
145             {
146                 String[] children = dir.list();
147                 for (int i=0; i<children.length; i++)
148                 {
149                     boolean success = cleanDir(new File(dir, children[i]));
150                     if (!success)
151                     {
152                         return false;
153                     }
154                 }
155             }
156 
157             // The directory is now empty so delete it
158             return dir.delete();
159         }
160 }
161