public class DirDiff {
public static String getFileMD5(File file) {
if (!file.isFile()) {
return null;
}
MessageDigest digest = null;
byte buffer[] = new byte[1024];
int len;
try (FileInputStream in = new FileInputStream(file);){
digest = MessageDigest.getInstance("MD5");
while ((len = in.read(buffer, 0, 1024)) != -1) {
digest.update(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return new BigInteger(1, digest.digest()).toString(16);
}
public static Map<String, String> getDirMD5(File file, boolean listChild) {
if (!file.isDirectory()) {
return null;
}
Map<String, String> map = new HashMap<String, String>();
String md5;
File files[] = file.listFiles();
for (File f : files) {
if (f.isDirectory() && listChild) {
map.putAll(getDirMD5(f, listChild));
} else {
md5 = getFileMD5(f);
if (md5 != null) {
map.put(f.getPath(), md5);
}
}
}
return map;
}
public static void main(String[] args) {
String dir1 = "C:\\IDOS\\dev\\automation\\verification\\today_day_end_files\\BIS2_baseinfo";
String dir2 = "C:\\IDOS\\dev\\automation\\verification\\yesterday_day_end_files\\BIS2_baseinfo";
Map<String, String> map = getDirMD5(new File(dir1), true);
Map<String, String> map2 = getDirMD5(new File(dir2), true);
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String fullPath = it.next();
String key2 = fullPath.replace(dir1, dir2);
String md5Val = map.get(fullPath);
String value2 = map2.remove(key2);
if (value2 == null) {
System.out.println("File is NOT exist! -----> " + key2);
} else if (!md5Val.equals(value2)) {
System.out.println("File content is different! ==> " + fullPath);
}
}
it = map2.keySet().iterator();
while (it.hasNext()) {
String key = it.next();
String key2 = key.replace(dir2, dir1);
System.out.println("File is NOT exist! -----> " + key2);
}
}
public String dirDiff(String orgPath, String otherPath, String... expectedFileList) {
System.out.println("orgPath :" + orgPath);
System.out.println("otherPath :" + otherPath);
List<String> msgs = new ArrayList<>();
File orgFile = new File(orgPath);
File otherFile = new File(otherPath);
if (!orgFile.isDirectory() || !otherFile.isDirectory()) {
return String.format("Comparison is invalid between %s and %s", orgFile.getName(), otherFile.getName());
}
Map<String, String> map = getDirMD5(orgFile, true);
Map<String, String> map2 = getDirMD5(otherFile, true);
Iterator<String> it = map.keySet().iterator();
while (it.hasNext()) {
String fullPath = it.next();
String key2 = fullPath.replace(orgPath, otherPath);
String md5Val = map.get(fullPath);
String value2 = map2.remove(key2);
if (value2 == null) {
msgs.add("File is NOT exist! -----> " + key2);
} else if (!md5Val.equals(value2)) {
msgs.add("File content is different! ==> " + fullPath);
}
}
it = map2.keySet().iterator();
while (it.hasNext()) {
String fullPath = it.next();
String key2 = fullPath.replace(otherPath, orgPath);
System.out.println("File is NOT exist! -----> " + key2);
msgs.add("File is NOT exist! -----> " + key2);
}
return StringUtils.join(msgs, "\n");
}
}