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

扫描某目录下的所有文件的MD5码并导出文件【可执行jar】的更多相关文章

  1. 在ubuntu下如何验证文件的MD5码 (转载)

    转自:http://blog.csdn.net/david_xtd/article/details/7641682 在windows下可以使用专用的工具软件如WinMD5等来查看文件的MD5码, 在u ...

  2. Python:扫描目录下的所有文件

    扫描目录下的所有文件并返回文件的绝对路径 def fileListFunc(filePathList): fileList = [] for filePath in filePathList: for ...

  3. [boost][filesystem] 扫描给定目录下所有项

    Intro. Boost的filesystem可以用来扫描给定目录下的所有项. 实现 具体实现代码如下: 需要包含的头文件和使用的命名空间: #include <boost/filesystem ...

  4. php获取指定目录下的所有文件列表

    在我们实际的开发需求中,经常用到操作文件,今天就讲一下关于获取指定目录下的所有文件的几种常用方法: 1.scandir()函数 scandir() 函数返回指定目录中的文件和目录的数组. scandi ...

  5. [No000073]C#直接删除指定目录下的所有文件及文件夹(保留目录)

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  6. [No00006B]方便的网络下载工具wget 可下载网站目录下的所有文件(可下载整个网站)

    wget是linux下命令行的下载工具,功能很强大,它能完成某些下载软件所不能做的,比如如果你想下载一个网页目录下的所有文件,如何做呢?网络用户有时候会遇到需要下载一批文件的情况,有时甚至需要把整个网 ...

  7. linux复制指定目录下的全部文件到另一个目录中

    linux复制指定目录下的全部文件到另一个目录中复制指定目录下的全部文件到另一个目录中文件及目录的复制是经常要用到的.linux下进行复制的命令为cp.假设复制源目录 为 dir1 ,目标目录为dir ...

  8. python实现查看目录下重复的文件

    该python 脚本有以下三个功能: 1. 实现查看目录下重复的文件,输出文件按修改时间升序排列 2. 将按修改时间排列比较旧的.可删除的文件列出来 3. 按目录对重复文件进行统计,比如,目录/tmp ...

  9. [转载] linux查找目录下的所有文件中是否含有某个字符串

    链接自 http://blog.sina.com.cn/s/blog_691a84f301015khx.html,并略加修订. 查找目录下的所有文件中是否含有某个字符串 find .|xargs gr ...

随机推荐

  1. 蓝牙Legacy Pairing流程概述

    Legacy pairing 从名字上看可以知道它是老式设备采用的配对方法. 配对的最终目的是为了生成key,key可以给链路加密,保证双方设备通信的安全性.那配对流程的讲述其实就是key的生成过程. ...

  2. Linux ip netns 命令

    ip netns 命令用来管理 network namespace.它可以创建命名的 network namespace,然后通过名字来引用 network namespace,所以使用起来很方便. ...

  3. [WPF]何如在Win7使用Aero2主题

    1. 问题 假设我在Windows10的环境新建一个4.6的WPF项目,添加一个ComboBox,并用Blend在这个ComboBox上右键"编辑模板"->"编辑副 ...

  4. 【redis】windows 怎样关闭redis

    安装redis之后在命令行窗口中输入 redis-server redis.windows.conf 启动redis关闭命令行窗口就是关闭 redis.---redis作为windows服务启动方式r ...

  5. 几何学观止(Lie群部分)

    上承这个页面,这次把Lie群的部分写完了 几何学观止-微分几何部分(20181102).pdf 我觉得其他部分(尤其是代数几何部分)我目前没有把握写得令自己满意,总之希望在毕业前能写完吧. 这次调整了 ...

  6. 软件扒网站? 爬虫? F12查看源码? 查看网页源代码?浏览器sources? 区别和联系!

    1.软件扒网站: 利用各类扒站网站,如仿站小工具8.0,可以按照规则将网站的未经浏览器简析的前端代码扒下来,并整理成css,js,html等文件夹,很方便.(当然看不到ajax等相关代码) 备注:如果 ...

  7. Maven安装与环境配置(Windows)

    1.下载安装包 在Maven官网下载最新版的安装包:http://maven.apache.org/download.cgi 2.解压安装包 3.配置Maven环境变量 配置M2_HOME环境变量,指 ...

  8. 每周分享之cookie详解

    本章从JS方向讲解cookie的使用.(实质上后端代码也是差不多用法,无非读取和设置两块) 基本用法:document.cookie="username=pengpeng"; 修改 ...

  9. 10-vue的介绍

    vue的作者叫尤雨溪,中国人.自认为很牛逼的人物,也是我的崇拜之神. 关于他本人的认知,希望大家读一下这篇关于他的文章,或许你会对语言,技术,产生浓厚的兴趣.https://mp.weixin.qq. ...

  10. CSS小东西

    1.表格列自动均分 table-layout:fixed; 2.单元格内容自动换行 word-wrap:break-word;