# HG changeset patch # User Ingo Weinzierl # Date 1326105883 0 # Node ID 0723859f776f8c93377c089cb1fb48af51e59bd1 # Parent 4d3298295a6433738eece3a6d1b9379b600401df Added new FileTools functions to copy files and directories. artifacts/trunk@3627 c6561f87-3c4e-4783-a992-168aeb5c3f6f diff -r 4d3298295a64 -r 0723859f776f ChangeLog --- a/ChangeLog Mon Jan 09 08:15:35 2012 +0000 +++ b/ChangeLog Mon Jan 09 10:44:43 2012 +0000 @@ -1,3 +1,9 @@ +2012-01-09 Ingo Weinzierl + + * artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java: + Added new functions to copy files (copyFile()) and to copy directories + (copyDirectory()). + 2012-01-09 Ingo Weinzierl * artifact-database/src/main/java/de/intevation/artifactdatabase/state/State.java, diff -r 4d3298295a64 -r 0723859f776f artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java --- a/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java Mon Jan 09 08:15:35 2012 +0000 +++ b/artifacts-common/src/main/java/de/intevation/artifacts/common/utils/FileTools.java Mon Jan 09 10:44:43 2012 +0000 @@ -12,7 +12,9 @@ import java.io.IOException; import java.io.InputStream; import java.io.FileInputStream; +import java.io.FileOutputStream; import java.io.OutputStream; +import java.nio.channels.FileChannel; import java.util.Stack; import java.util.List; @@ -399,5 +401,134 @@ } out.closeEntry(); } + + + /** + * Copies a src file to target. + * + * @param src A file (not a directory) that should be copied. + * @param target The destination. This might be a file or a directory. + * + * @return true, if src has been successfully copied; otherwise + * false. + */ + public static boolean copyFile(File src, File target) + throws IOException + { + if (src == null || !src.exists()) { + log.warn("Source file does not exist!"); + return false; + } + + if (!src.canRead()) { + log.warn("Cannot read Source file!"); + return false; + } + + if (src.isDirectory()) { + log.warn("Source is a directory!"); + return false; + } + + if (target.isDirectory()) { + target = new File(target, src.getName()); + } + + FileInputStream in = null; + FileOutputStream out = null; + + try { + in = new FileInputStream(src); + out = new FileOutputStream(target); + + FileChannel inChannel = in.getChannel(); + FileChannel outChannel = out.getChannel(); + + inChannel.transferTo(0l, inChannel.size(), outChannel); + + return true; + } + catch (IOException ioe) { + log.warn(ioe, ioe); + } + finally { + if (in != null) { + try { + in.close(); + } + catch (IOException ioe) { /* do nothing here */ } + } + + if (out != null) { + try { + out.close(); + } + catch (IOException ioe) { /* do nothing here */ } + } + } + + return false; + } + + + /** + * Copies a directory source to a destination path dest. + * + * @param source A directory that should be copied. + * @param dest A destination directory which is created if it is not + * existing yet. + * + * @return true, if the directory has been successfully copied; otherwise + * false. + */ + public static boolean copyDirectory(final File source, final File dest) { + if (source == null || !source.exists()) { + log.warn("Source directory does not exist!"); + return false; + } + + if (!source.isDirectory()) { + log.warn("Source is not a directory!"); + return false; + } + + if (dest == null) { + log.warn("Destination directory is null!"); + return false; + } + + if (!dest.exists()) { + if (!dest.mkdir()) { + log.warn("Cannot create destination directory!"); + return false; + } + } + + File[] children = source.listFiles(); + int failed = 0; + + if (children != null && children.length > 0) { + for (File child: children) { + if (child.isFile()) { + try { + if (!copyFile(child, dest)) { + failed++; + } + } + catch (IOException ioe) { + log.warn(ioe, ioe); + failed++; + } + } + else if (child.isDirectory()) { + copyDirectory(child, new File(dest, child.getName())); + } + } + } + + log.debug("Failed to copy " + failed + " files."); + + return true; + } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :