如何用java或者c#写个扫描整个文件夹及子文件夹,验证多个文件的md5值,并将md5值重复但不同

如何用java或者c#写个扫描整个文件夹及子文件夹,验证多个文件的md5值,并将md5值重复但不同文件名的文件删掉的软件?我D盘视频文件夹文件名重复的太多了,手动看了内容删太累

java的,我已经测试可以使用。

但是单个文件很大的话可能算md5会比较慢

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Main
{
ArrayList<String> md5List = new ArrayList<String>();

public String getMd5(File file){
String value = null;
FileInputStream in = null;
try {
in = new FileInputStream(file);
MappedByteBuffer byteBuffer = in.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest md5 = MessageDigest.getInstance("MD5");
md5.update(byteBuffer);
BigInteger bi = new BigInteger(1, md5.digest());
value = bi.toString(16);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return value;
}

public void run(File file) {
        if(file != null){  
         if(file.isDirectory()){  
         File f[] = file.listFiles();
         if(f != null) {
         for(int i = 0; i < f.length; i++)  
         run(f[i]);
         }
            } else {
             String md5 = getMd5(file);
             if(md5List.contains(md5)) {
             System.out.println(file.toString());
             file.delete();
             } else {
             md5List.add(md5);
             }
            }  
        }  
}

public static void main(String args[]) {
Main m = new Main();
String path = "D:\\test";
m.run(new File(path));
}
}

温馨提示:内容为网友见解,仅供参考
第1个回答  2015-08-04
计算md5需要使用System.Security.Cryptography.MD5类
用法如下:
MD5 md5 = MD5.Create();
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read);
byte[] hash = md5.ComputeHash(fs);
fs.Close();

两段代码连上 一个遍历 再加上你自己的思维逻辑。 可以实现
相似回答