From 3aa9e559cb1350cf758c7f48b134056397bc4d60 Mon Sep 17 00:00:00 2001 From: Claus Stadler Date: Mon, 7 Oct 2024 15:40:01 +0200 Subject: [PATCH] Added FileUtils.copyDirectory based on java nio. --- .../org/aksw/commons/io/util/FileUtils.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/aksw-commons-io-parent/aksw-commons-io-utils/src/main/java/org/aksw/commons/io/util/FileUtils.java b/aksw-commons-io-parent/aksw-commons-io-utils/src/main/java/org/aksw/commons/io/util/FileUtils.java index e347a66f..d39f2616 100644 --- a/aksw-commons-io-parent/aksw-commons-io-utils/src/main/java/org/aksw/commons/io/util/FileUtils.java +++ b/aksw-commons-io-parent/aksw-commons-io-utils/src/main/java/org/aksw/commons/io/util/FileUtils.java @@ -6,6 +6,7 @@ import java.io.OutputStream; import java.nio.channels.FileChannel; import java.nio.file.AtomicMoveNotSupportedException; +import java.nio.file.CopyOption; import java.nio.file.DirectoryStream; import java.nio.file.FileAlreadyExistsException; import java.nio.file.FileVisitResult; @@ -18,6 +19,7 @@ import java.nio.file.attribute.BasicFileAttributes; import java.nio.file.attribute.FileTime; import java.util.ConcurrentModificationException; +import java.util.Iterator; import java.util.List; import java.util.Objects; import java.util.Random; @@ -394,4 +396,32 @@ public static Path findInAncestors(Path start, String fileName) { .orElse(null); } + public static void copyDirectory(Path source, Path target, CopyOption... options) throws IOException { + Path src = source.toAbsolutePath(); + Path tgt = target.toAbsolutePath(); + Files.createDirectories(tgt); + copyDirectoryInternal(src, tgt, options); + } + + protected static void copyDirectoryInternal(Path source, Path target, CopyOption... options) throws IOException { + try (Stream children = Files.list(source)) { + Iterator it = children.iterator(); + while (it.hasNext()) { + Path src = it.next(); + Path relPath = source.relativize(src); + + // Need to go via segments because source and target path may be in different file systems + String[] segments = PathUtils.getPathSegments(relPath); + Path tgt = PathUtils.resolve(target, segments); + + // TODO Coypu symbolic links? + if (Files.isDirectory(src)) { + Files.createDirectories(tgt); + copyDirectoryInternal(src, tgt, options); + } else if (Files.isRegularFile(src)) { + Files.copy(src, tgt, options); + } + } + } + } }