檢測某一目錄下md5相同的文件
阿新 • • 發佈:2017-12-12
mes 解壓版 color com style pac import add class
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(newFileInputStream(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); } } } } }
檢測某一目錄下md5相同的文件