import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; /** * 检测某一目录下md5相同的文件 */ public class Test { public static void main(String[] args) throws IOException { Map<String, String> map = new HashMap<String, String>(); Map<String, List<String>> map1 = new HashMap<String, List<String>>(); String path = "D:\\Nihaorz\\Documents\\work\\综合运维基线包\\实时解压版本\\OperationCenter-1.8\\OperationCenter\\WEB-INF\\lib"; File dir = new File(path); File[] files = dir.listFiles(); for (File file : files) { String fileName = file.getName(); String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(new FileInputStream(file))); if (!map.containsKey(md5)) { map.put(md5, fileName); } else { if (!map1.containsKey(md5)) { List<String> list = new ArrayList<String>(); list.add(fileName); map1.put(md5, list); } else { map1.get(md5).add(fileName); } } } if (map1.size() > 0) { System.out.println("有如下jar包md5码重复"); System.out.println("------------------------------------------"); Iterator<String> it = map1.keySet().iterator(); while (it.hasNext()) { String md5 = it.next(); System.out.println("MD5:" + md5); System.out.println("fileName:" + map.get(md5)); List<String> fileNames = map1.get(md5); for (String fileName : fileNames) { System.out.println("fileName:" + fileName); } } } } }
时间: 2024-11-08 13:14:48