C# 中利用 CRC32 值判斷文件是否重復


需要在 NuGet 中引用 Crc32.NET 包

直接貼代碼了:

using Force.Crc32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Crc32NETDemo.ConApp.Codes;

namespace Crc32NETDemo.ConApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string folderPath = @"D:\music\";
            Console.WriteLine(string.Format("正在分析路徑 {0} 下的文件的 CRC32 值。", folderPath));
            IEnumerable<string> filePathList = Directory.EnumerateFiles(folderPath, "*", SearchOption.AllDirectories);
            Dictionary<string, uint> dic = new Dictionary<string, uint>();
            foreach (string filePath in filePathList)
            {
                byte[] zipdata = File.ReadAllBytes(filePath);
                uint crc = Crc32CAlgorithm.Compute(zipdata);
                dic.Add(filePath, crc);
            }

            Console.WriteLine("重復的文件如下:" + Environment.NewLine);
            IEnumerable<uint> doubledCrc32List = dic.Values.Iterative();
            if (doubledCrc32List.Any())
            {
                var tempList = dic.Where(c => doubledCrc32List.Contains(c.Value)).OrderBy(c => c.Value).ToList();
                foreach (var item in tempList)
                {
                    Console.WriteLine(string.Format("路徑:{0}, CRC32 值:{1}", item.Key, item.Value));
                }
            }
            else
            {
                Console.WriteLine("沒有找到重復的文件!");
            }
            Console.WriteLine();
            Console.ReadLine();
        }
    }
}

 

謝謝瀏覽!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM