利用MD5码检查图片资源是否重复

本次实验中,我检查的是项目中的哪些贴图是否被重用。代码如下:

首先,就是MD5码的生成,这个是最重要的:

using System.IO;
using System.Security.Cryptography;
using System.Text;

public class FileHelper{
    public static string MD5Stream(Stream inputStream){
        MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
        md5.ComputeHash(inputStream);
        byte[] bt = md5.Hash;
        
        StringBuilder sb = new StringBuilder();
        for(int i =0;i < bt.Length; i++){
            sb.Append(bt[i].ToString("X2"));
        }
        return sb.ToString();
    }

    public static string MD5Stream(string filePath){
        try{
            using(FileStream stream = File.Open(filePath,FileMode.Open)){
                return MD5Stream(stream);
            }
        }
         catch(System.Exception e){
            Debug.Log("抛出异常:" + e);
            return;
        }
    }
}

然后我们只需要提供需要检查资源的路径就可以了。

using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

public class MD5Test:Editor{
    private static Dictionary<string, List<string>> ;
    private static List<string> listPaths;

    [MenuItem("MD5/SearchTexture")]
    public static void SearchAllTexture(){
        dics = new Dictionary<string,List<string>>();
        listPaths = new List<string>();

        string[] guids = AssetDatabase.FindAssets("t:" + "Texture2D",new string[]{"Assets"});
        for(int i =0;i<guids.Length;i++){
            string texturePath = AssetDatabase.GUIDToAssetPath(guids[i]);
            string code = FileHelper.MD5Stream(texturePath);
            listPaths.clear();
            if(!dics.ContainsKey(ecode)){
                listPaths.Add(texturePath);
                dics.Add(ecode,listPaths);
            }
            else
            {
                dics[ecode].Add(listPaths);
            }
        }
    }
}

这样的话,我们就获得了所有的Texture2D的MD5码。

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
利用MD5查找目录下的相同重复文件,可以按照以下步骤进行: 1. 遍历目标目录并获取所有文件的MD5值 可以使用Java的MessageDigest类计算文件的MD5值。具体实现可以参考以下代: ```java public static String getFileMD5(File file) { try { MessageDigest md5 = MessageDigest.getInstance("MD5"); FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) != -1) { md5.update(buffer, 0, length); } fis.close(); byte[] digest = md5.digest(); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b)); } return sb.toString(); } catch (Exception e) { e.printStackTrace(); return null; } } ``` 可以编写一个递归函数来遍历目标目录下的所有文件,并计算它们的MD5值。这个函数可以返回一个Map,其中键是MD5值,值是文件路径列表。示例代如下: ```java public static Map<String, List<String>> getDuplicateFiles(String directoryPath) { File directory = new File(directoryPath); Map<String, List<String>> fileMap = new HashMap<>(); if (directory.isDirectory()) { for (File file : directory.listFiles()) { if (file.isDirectory()) { fileMap.putAll(getDuplicateFiles(file.getAbsolutePath())); } else { String md5 = getFileMD5(file); if (md5 != null) { List<String> fileList = fileMap.get(md5); if (fileList == null) { fileList = new ArrayList<>(); fileMap.put(md5, fileList); } fileList.add(file.getAbsolutePath()); } } } } return fileMap; } ``` 2. 查找重复的文件 现在我们已经有了一个Map,其中键是MD5值,值是文件路径列表。现在我们需要查找所有MD5值相同的文件,并将它们的路径打印出来。示例代如下: ```java public static void printDuplicateFiles(Map<String, List<String>> fileMap) { for (String md5 : fileMap.keySet()) { List<String> fileList = fileMap.get(md5); if (fileList.size() > 1) { System.out.println("Duplicate files for MD5 " + md5 + ":"); for (String filePath : fileList) { System.out.println(filePath); } } } } ``` 现在我们可以将上述两个函数组合起来,以便在目标目录下查找所有重复的文件。示例代如下: ```java public static void main(String[] args) { String directoryPath = "/path/to/directory"; Map<String, List<String>> fileMap = getDuplicateFiles(directoryPath); printDuplicateFiles(fileMap); } ``` 这样就可以在目标目录下查找所有重复的文件了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值