if (!directoryExists(sourceDirectory)) {
throw new IllegalArgumentException("Directory does not exist " + sourceDirectory);
}
try {
final FileSystem fileSystem = descriptor.getFileSystem();
final Path sourcePath = fileSystem.provider().getPath(URI.create(descriptor.getStringRepositoryRoot() + sourceDirectory));
if (!Files.isDirectory(sourcePath)) {
return false;
}
if (name == null) {
name = sourcePath.getFileName().toString();
}
final String destinationPathRoot = descriptor.getStringRepositoryRoot() + location + fileSystem.getSeparator() + name;
Files.walkFileTree(sourcePath, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path currentFile, BasicFileAttributes basicFileAttributes) throws IOException {
Path destinationPath = fileSystem.provider().getPath(URI.create(destinationPathRoot
+ fileSystem.getSeparator() + sourcePath.relativize(currentFile)));
createIfNotExists(destinationPath);
fileSystem.provider().move(currentFile, destinationPath, StandardCopyOption.REPLACE_EXISTING);
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException e) throws IOException {
if (e == null) {
try {
Path destinationPath = fileSystem.provider().getPath(URI.create(destinationPathRoot
+ fileSystem.getSeparator() + sourcePath.relativize(dir)));
createIfNotExists(destinationPath);
fileSystem.provider().move(dir, destinationPath, StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e1) {
fileSystem.provider().deleteIfExists(dir);