garydgregory commented on a change in pull request #31: DirectoryUtils - isEqual to compare directories URL: https://github.com/apache/commons-io/pull/31#discussion_r350257878
########## File path: src/main/java/org/apache/commons/io/DirectoryUtils.java ########## @@ -0,0 +1,149 @@ +package org.apache.commons.io; + +import java.io.File; +import java.io.IOException; +import java.nio.file.DirectoryStream; +import java.nio.file.Files; +import java.nio.file.Path; + +/** + * General directory manipulation utilities. + * <p> + * Facilities are provided in the following areas: + * <ul> + * <li>comparing directories + * </ul> + * <p> + * + * Original code: http://stackoverflow.com/questions/14522239/test-two-directory-trees-for-equality + * + */ +public class DirectoryUtils { + + /** + * checks if the directory file lists are equal. + * + * If checkFileContent is true, then also if the file content is equal + * + * @param directory + * the directory + * @param compareDirectory + * the directory to compare with + * @param checkFileContent + * also compare file content + * @return true if directory and compareDirectory are equal + * @throws IOException + */ + public static boolean isEqual(Path directory, Path compareDirectory, boolean checkFileContent) + throws IOException { + + boolean check = isEverythingInCompareDirectory(directory, compareDirectory, checkFileContent); + + // we only need to check file content in on direction. + boolean checkOppositeFileContent = false; + + boolean checkOpposite = check + && isEverythingInCompareDirectory(compareDirectory, directory, checkOppositeFileContent); + return check && checkOpposite; + + } + + /** + * checks if the directory file lists and file content is equal + * + * @param directory + * the directory + * @param compareDirectory + * the directory to compare with + * @param checkFileContent + * also compare file content + * @return true if directory and compareDirectory are equal + * @throws IOException + */ + private static boolean isEverythingInCompareDirectory(Path directory, Path compareDirectory, + boolean checkFileContent) throws IOException { + + if (directory != null && compareDirectory != null) { + // LOGGER.info("checking directory " + directory); + // LOGGER.info("checking compareDirectory " + compareDirectory); + + File directoryFile = directory.toFile(); + File compareFile = compareDirectory.toFile(); + + // check, if there is the same number of files/subdirectories + File[] directoryFiles = directoryFile.listFiles(); Review comment: Don't use `listFiles()`, this will kill performance on large file trees. Use Java's streaming APIs. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
