X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2FFsUtils.java;h=23839db74c3f93c8c2f82a740089c39499b756d8;hb=f3ea14abccc33b1c3326417a87c91145be776c72;hp=b317f4bc95bbfaa6570487c614e9bc8225276849;hpb=2752f591462f846423020ef1c1989766b20cd554;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/FsUtils.java b/org.argeo.util/src/org/argeo/util/FsUtils.java index b317f4bc9..23839db74 100644 --- a/org.argeo.util/src/org/argeo/util/FsUtils.java +++ b/org.argeo.util/src/org/argeo/util/FsUtils.java @@ -9,6 +9,43 @@ import java.nio.file.attribute.BasicFileAttributes; /** Utilities around the standard Java file abstractions. */ public class FsUtils { + + /** + * Deletes this path, recursively if needed. Does nothing if the path does not + * exist. + */ + public static void copyDirectory(Path source, Path target) { + if (!Files.exists(source) || !Files.isDirectory(source)) + throw new IllegalArgumentException(source + " is not a directory"); + if (Files.exists(target) && !Files.isDirectory(target)) + throw new IllegalArgumentException(target + " is not a directory"); + try { + Files.createDirectories(target); + Files.walkFileTree(source, new SimpleFileVisitor() { + @Override + public FileVisitResult postVisitDirectory(Path directory, IOException e) throws IOException { + if (e != null) + throw e; + Path relativePath = source.relativize(directory); + Path targetDirectory = target.resolve(relativePath); + Files.createDirectory(targetDirectory); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { + Path relativePath = source.relativize(file); + Path targetFile = target.resolve(relativePath); + Files.copy(file, targetFile); + return FileVisitResult.CONTINUE; + } + }); + } catch (IOException e) { + throw new RuntimeException("Cannot copy " + source + " to " + target, e); + } + + } + /** * Deletes this path, recursively if needed. Does nothing if the path does not * exist.