/* fm - File management class library Copyright (C) 2024 Ulrich Hilger This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ package de.uhilger.fm; import java.io.File; import java.io.IOException; import java.nio.file.FileVisitResult; import java.nio.file.FileVisitor; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; /** * Ein FileVisitor zum Verschieben oder Kopieren ganzer Ordnerstrukturen mit * Hilfe der Methode Files.walkFileTree von java.nio. * * @author Ulrich Hilger * @version 1, 14. Mai 2021 */ public class CopyMoveVisitor extends FileHelper implements FileVisitor { private Path targetDir; private int operation; public void setTargetDir(Path targetDir) { this.targetDir = targetDir; //logger.fine("targetDir: " + targetDir.toString()); } /** * OP_COPY oder OP_MOVE * * @param op */ public void setOperation(int op) { this.operation = op; } @Override public FileVisitResult preVisitDirectory(Object dir, BasicFileAttributes attrs) throws IOException { if (operation != Const.OP_DELETE) { if (dir instanceof Path) { Path sourceDir = (Path) dir; File destFile = targetDir.resolve(sourceDir.getFileName()).toFile(); //logger.fine("sourceDir: " + sourceDir + ", destFile: " + destFile); if (destFile.exists()) { File newDir = getNewFileName(destFile); destFile.renameTo(newDir); } destFile.mkdir(); this.targetDir = destFile.toPath(); //logger.fine("targetDir now: " + targetDir.toString()); } } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Object file, BasicFileAttributes attrs) throws IOException { if(operation != Const.OP_DELETE) { if (file instanceof Path) { Path source = (Path) file; File destFile = targetDir.resolve(source.getFileName()).toFile(); if (destFile.exists()) { destFile = getNewFileName(destFile); } if (operation == Const.OP_MOVE) { //logger.fine("move source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); Files.move(source, destFile.toPath()); } else if (operation == Const.OP_COPY) { //logger.fine("copy source: " + source.toString() + ", destFile: " + destFile.getAbsolutePath()); Files.copy(source, destFile.toPath()); } } } else { Files.delete((Path) file); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Object file, IOException exc) throws IOException { return FileVisitResult.CONTINUE; } @Override public FileVisitResult postVisitDirectory(Object dir, IOException exc) throws IOException { if (operation != Const.OP_DELETE) { if (dir instanceof Path) { Path finishedDir = (Path) dir; targetDir = targetDir.getParent(); if(operation == Const.OP_MOVE) { //logger.fine("delete " + finishedDir.toString()); Files.delete(finishedDir); } } //logger.fine("targetDir now: " + targetDir.toString()); } else { Files.delete((Path) dir); } return FileVisitResult.CONTINUE; } }