Exemplo n.º 1
0
 private static void doCopyDirectory(
     final File srcDir, final File destDir, final List<String> exclusionList) throws IOException {
   final File[] files = srcDir.listFiles();
   if (files == null) { // null if security restricted
     throw new IOException("Failed to list contents of " + srcDir);
   }
   if (destDir.exists()) {
     if (!destDir.isDirectory()) {
       throw new IOException("Destination '" + destDir + "' exists but is not a directory");
     }
   } else {
     if (!destDir.mkdirs()) {
       throw new IOException("Destination '" + destDir + "' directory cannot be created");
     }
   }
   if (!destDir.canWrite()) {
     throw new IOException("Destination '" + destDir + "' cannot be written to");
   }
   for (final File file : files) {
     final File copiedFile = new File(destDir, file.getName());
     if (exclusionList == null || !exclusionList.contains(file.getCanonicalPath())) {
       if (file.isDirectory()) {
         doCopyDirectory(file, copiedFile, exclusionList);
       } else {
         copy(file, copiedFile);
       }
     }
   }
 }
Exemplo n.º 2
0
  public static void copyDirectory(final File srcDir, final File destDir) throws IOException {
    if (srcDir == null) {
      throw new NullPointerException("Source must not be null");
    }
    if (destDir == null) {
      throw new NullPointerException("Destination must not be null");
    }
    if (!srcDir.exists()) {
      throw new FileNotFoundException("Source '" + srcDir + "' does not exist");
    }
    if (!srcDir.isDirectory()) {
      throw new IOException("Source '" + srcDir + "' exists but is not a directory");
    }
    if (srcDir.getCanonicalPath().equals(destDir.getCanonicalPath())) {
      throw new IOException(
          "Source '" + srcDir + "' and destination '" + destDir + "' are the same");
    }

    // Cater for destination being directory within the source directory (see IO-141)
    List<String> exclusionList = null;
    if (destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath())) {
      final File[] srcFiles = srcDir.listFiles();
      if (srcFiles != null && srcFiles.length > 0) {
        exclusionList = new ArrayList<String>(srcFiles.length);
        for (final File srcFile : srcFiles) {
          final File copiedFile = new File(destDir, srcFile.getName());
          exclusionList.add(copiedFile.getCanonicalPath());
        }
      }
    }
    doCopyDirectory(srcDir, destDir, exclusionList);
  }