方案1:

直接贴代码了:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace FileCompareDemo
{
public class FileHelper
{
const int BYTES_TO_READ = sizeof(Int64); public static bool FilesAreEqual(FileInfo first, FileInfo second)
{
if (first.Length != second.Length)
return false; if (string.Equals(first.FullName, second.FullName, StringComparison.OrdinalIgnoreCase))
return true; int iterations = (int)Math.Ceiling((double)first.Length / BYTES_TO_READ); using (FileStream fs1 = first.OpenRead())
using (FileStream fs2 = second.OpenRead())
{
byte[] one = new byte[BYTES_TO_READ];
byte[] two = new byte[BYTES_TO_READ]; for (int i = ; i < iterations; i++)
{
fs1.Read(one, , BYTES_TO_READ);
fs2.Read(two, , BYTES_TO_READ); if (BitConverter.ToInt64(one, ) != BitConverter.ToInt64(two, ))
return false;
}
} return true;
} public static bool FilesAreEqual_OneByte(FileInfo first, FileInfo second)
{
if (first.Length != second.Length)
return false; if (string.Equals(first.FullName, second.FullName, StringComparison.OrdinalIgnoreCase))
return true; using (FileStream fs1 = first.OpenRead())
using (FileStream fs2 = second.OpenRead())
{
for (int i = ; i < first.Length; i++)
{
if (fs1.ReadByte() != fs2.ReadByte())
return false;
}
} return true;
} public static bool FilesAreEqual_Hash(FileInfo first, FileInfo second)
{
byte[] firstHash = MD5.Create().ComputeHash(first.OpenRead());
byte[] secondHash = MD5.Create().ComputeHash(second.OpenRead()); for (int i = ; i < firstHash.Length; i++)
{
if (firstHash[i] != secondHash[i])
return false;
}
return true;
}
}
}

方案2:

直接贴代码了:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FileCompareDemo
{
public static class FileHelper2
{
public static bool FilesContentsAreEqual(FileInfo fileInfo1, FileInfo fileInfo2)
{
if (fileInfo1 == null)
{
throw new ArgumentNullException(nameof(fileInfo1));
} if (fileInfo2 == null)
{
throw new ArgumentNullException(nameof(fileInfo2));
} if (string.Equals(fileInfo1.FullName, fileInfo2.FullName, StringComparison.OrdinalIgnoreCase))
{
return true;
} if (fileInfo1.Length != fileInfo2.Length)
{
return false;
}
else
{
using (var file1 = fileInfo1.OpenRead())
{
using (var file2 = fileInfo2.OpenRead())
{
return StreamsContentsAreEqual(file1, file2);
}
}
}
} private static int ReadFullBuffer(Stream stream, byte[] buffer)
{
int bytesRead = ;
while (bytesRead < buffer.Length)
{
int read = stream.Read(buffer, bytesRead, buffer.Length - bytesRead);
if (read == )
{
// Reached end of stream.
return bytesRead;
} bytesRead += read;
} return bytesRead;
} private static bool StreamsContentsAreEqual(Stream stream1, Stream stream2)
{
const int bufferSize = * sizeof(Int64);
var buffer1 = new byte[bufferSize];
var buffer2 = new byte[bufferSize]; while (true)
{
int count1 = ReadFullBuffer(stream1, buffer1);
int count2 = ReadFullBuffer(stream2, buffer2); if (count1 != count2)
{
return false;
} if (count1 == )
{
return true;
} int iterations = (int)Math.Ceiling((double)count1 / sizeof(Int64));
for (int i = ; i < iterations; i++)
{
if (BitConverter.ToInt64(buffer1, i * sizeof(Int64)) != BitConverter.ToInt64(buffer2, i * sizeof(Int64)))
{
return false;
}
}
}
}
}
}

方案3(异步版本):

直接贴代码了:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace FileCompareDemo
{
public static class FileCompareAsyncHelper
{
static void test(string filePath1, string filePath2)
{
var fi1 = new FileInfo(filePath1);
var fi2 = new FileInfo(filePath2);
Console.WriteLine(FilesContentsAreEqualAsync(fi1, fi2).GetAwaiter().GetResult());
} public static async Task<bool> FilesContentsAreEqualAsync(FileInfo fileInfo1, FileInfo fileInfo2)
{
if (fileInfo1 == null)
{
throw new ArgumentNullException(nameof(fileInfo1));
} if (fileInfo2 == null)
{
throw new ArgumentNullException(nameof(fileInfo2));
} if (string.Equals(fileInfo1.FullName, fileInfo2.FullName, StringComparison.OrdinalIgnoreCase))
{
return true;
} if (fileInfo1.Length != fileInfo2.Length)
{
return false;
}
else
{
using (var file1 = fileInfo1.OpenRead())
{
using (var file2 = fileInfo2.OpenRead())
{
return await StreamsContentsAreEqualAsync(file1, file2).ConfigureAwait(false);
}
}
}
} private static async Task<int> ReadFullBufferAsync(Stream stream, byte[] buffer)
{
int bytesRead = ;
while (bytesRead < buffer.Length)
{
int read = await stream.ReadAsync(buffer, bytesRead, buffer.Length - bytesRead).ConfigureAwait(false);
if (read == )
{
// Reached end of stream.
return bytesRead;
} bytesRead += read;
} return bytesRead;
} private static async Task<bool> StreamsContentsAreEqualAsync(Stream stream1, Stream stream2)
{
const int bufferSize = * sizeof(Int64);
var buffer1 = new byte[bufferSize];
var buffer2 = new byte[bufferSize]; while (true)
{
int count1 = await ReadFullBufferAsync(stream1, buffer1).ConfigureAwait(false);
int count2 = await ReadFullBufferAsync(stream2, buffer2).ConfigureAwait(false); if (count1 != count2)
{
return false;
} if (count1 == )
{
return true;
} int iterations = (int)Math.Ceiling((double)count1 / sizeof(Int64));
for (int i = ; i < iterations; i++)
{
if (BitConverter.ToInt64(buffer1, i * sizeof(Int64)) != BitConverter.ToInt64(buffer2, i * sizeof(Int64)))
{
return false;
}
}
}
}
}
}

谢谢浏览!

最新文章

  1. jQuery个性化图片轮播效果
  2. Visual C++ 的代码折叠
  3. 海康威视 NET_DVR_FindNextFile 的错误
  4. python 自动化测试资料
  5. sql 增加字段
  6. HW--漂亮度2(测试通过)
  7. Linux系统备份与还原
  8. Python自动化运维之21、CSS
  9. 探究css !important的应用之道
  10. TCP三次握手中,为什么需要第三次握手?
  11. 2017年总结的前端文章——一劳永逸的搞定 flex 布局
  12. JAVA经典算法40题(原题+分析)之原题
  13. python爬虫requests模块
  14. 用C#学习数据结构之链表
  15. LiveCharts文档-4基本绘图-3其他
  16. vc for python2.7
  17. RabbitMQ None of the specified endpoints were reachable
  18. ADO.NET怎删改+vs 2013 C#
  19. selenium2 用Yaml文件进行元素管理 (五)
  20. Web.xml过滤器配置及执行顺序概念

热门文章

  1. javascript在数组的循环中删除元素
  2. HashMap中 工具方法tableSizeFor的作用
  3. requests库的使用、安装及方法的简单介绍
  4. Vue实现简单的列表金额计算效果(简易购物车)
  5. PDF目录编辑器使用介绍
  6. Java技巧——比较两个日期相差的天数
  7. Hystrix失败处理逻辑解析
  8. arm-linux-gcc-4.5.1安装方法
  9. 配置docker服务器支持远程连接
  10. axios如何先请求A接口然后在请求B接口