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