扫描某目录下的所有文件的MD5码并导出文件【可执行jar】
pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>geostack</groupId> <artifactId>geostack-md5</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.10</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <archive> <manifest> <mainClass>com.nihaorz.util.md5.MD5Util</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> </plugin> </plugins> </build> </project>
MD5Util.java
package com.nihaorz.util.md5; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.IOUtils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class MD5Util { public static void main(String[] args) { String[] _args = new String[]{ "D:\\.m2\\repository\\geostack\\geostack-balance-core\\2.0.0.0-SNAPSHOT", "md5", "D:\\Nihaorz\\Desktop\\md5.txt" }; run(args); } private static void run(String[] args) { if (args == null || args.length == 0) { System.out.println("该jar文件功能为打印或导出某单个文件或者某个目录下所有文件的的MD5码"); System.out.println("第一个参数【必填】为文件路径或文件夹路径"); System.out.println("第二个参数【必填】为排序规则,name:以文件名升序,md5:以md5码升序"); System.out.println("第三个参数【选填】为导出文件路径,配绝对地址,包含文件名,不存在时自动创建,不填时数据将只打印在控制台"); } else { if (args.length >= 2) { System.out.println("文件扫描中......"); System.out.println("---------------------------------------------------------------------------"); String path = args[0]; File file = new File(path); if (file.exists()) { String sort = args[1]; if ("name".equalsIgnoreCase(sort) || "md5".equalsIgnoreCase(sort)) { Long time = null; long start = System.currentTimeMillis(); Map<String, File> map = getMD5ByPath(file); Map<String, String> map1 = new HashMap<String, String>(); StringBuilder sb = new StringBuilder(); if (map.size() > 0) { List<String> list = new ArrayList<String>(); Iterator<String> it = map.keySet().iterator(); while (it.hasNext()) { String md5 = it.next(); map1.put(map.get(md5).getName(), md5); if ("name".equalsIgnoreCase(sort)) { list.add(map.get(md5).getName()); } else if ("md5".equalsIgnoreCase(sort)) { list.add(md5); } } String[] strings = new String[list.size()]; strings = list.toArray(strings); Arrays.sort(strings, String.CASE_INSENSITIVE_ORDER); if ("name".equalsIgnoreCase(sort)) { for (String string : strings) { sb.append(map1.get(string)).append(" -- ").append(string).append("\n"); System.out.println(map1.get(string) + " -- " + string); } } else if ("md5".equalsIgnoreCase(sort)) { for (String string : strings) { sb.append(string).append(" -- ").append(map.get(string).getName()).append("\n"); System.out.println(string + " -- " + map.get(string).getName()); } } System.out.println("---------------------------------------------------------------------------"); System.out.println("扫描路径:" + args[0]); System.out.println("扫描文件总数:" + map.size()); time = System.currentTimeMillis() - start; } if (time != null) { System.out.println("扫描耗时:" + time + "毫秒"); } else { long end = System.currentTimeMillis(); System.out.println("扫描耗时:" + (end - start) + "毫秒"); } if (args.length >= 3) { String outPath = args[2]; File outFile = new File(outPath); if (!outFile.exists()) { try { outFile.createNewFile(); } catch (IOException e) { System.out.println("文件创建失败,请检查导出路径配置!"); } } FileOutputStream fos = null; try { fos = new FileOutputStream(outFile); } catch (FileNotFoundException e) { e.printStackTrace(); } PrintWriter pw = new PrintWriter(fos); StringBuilder result = new StringBuilder(); result.append("扫描路径:").append(args[0]).append("\n") .append("扫描文件总数:").append(map.size()).append("\n") .append("扫描耗时:").append(time).append("毫秒").append("\n") .append("导出文件:").append(args[2]).append("\n") .append("---------------------------------------------------------------------------\n") .append(sb); pw.write(result.toString().toCharArray()); pw.flush(); pw.close(); System.out.println("导出文件:"+args[2]); } } else { System.out.println("排序规则不合法!"); } } else { System.out.println("文件路径或文件夹路径不存在!"); } } } } /** * 获取某指定文件或某目录下的文件的MD5码map * * @param path * @return */ private static Map<String, File> getMD5ByPath(File path) { Map<String, File> result = new HashMap<String, File>(); if (path.exists()) { if (path.isDirectory()) { File[] files = path.listFiles(); for (File file : files) { try { if (!file.isDirectory()) { String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(new FileInputStream(file))); result.put(md5, file); } } catch (IOException e) { e.printStackTrace(); } } } else { try { String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(new FileInputStream(path))); result.put(md5, path); } catch (IOException e) { e.printStackTrace(); } } } return result; } }
进到项目根目录下执行
mvn clean compile assembly:single
然后到target目录找到以-jar-with-dependencies.jar结尾的jar文件,即可以使用java -jar命令执行
以下为附件,解压可用:
geostack-md5-1.0-SNAPSHOT-jar-with-dependencies.jar.zip