[英]Fastest way to check file authenticity in C#
I have an running windows service. 我有一个正在运行的Windows服务。 after downloading a zip file the file should be checked.
下载zip文件后,应检查文件。 At the moment it will be checked with a MD5 hash.
目前,将使用MD5哈希值对其进行检查。 But this is not really fast with file sizes about 1 GB.
但是对于大约1 GB的文件,这并不是真的很快。 Another reason to change this is that I get an outofmemory exception on a windows 7 64 bit machine while reading the file into a byte array.
更改此错误的另一个原因是,我在Windows 7 64位计算机上将文件读入字节数组时收到内存不足的异常。
Thanks for helping. 感谢您的帮助。
Maybe MD5 looks simplier, but something like this should work also (and it's not memory relevant): 也许MD5看起来更简单,但是类似的事情也应该起作用(并且与内存无关):
static bool Compare(string filePath1, string filePath2)
{
using (FileStream file = File.OpenRead(filePath1))
{
using (FileStream file2 = File.OpenRead(filePath2))
{
if (file.Length != file2.Length)
{
return false;
}
int count;
const int size = 0x1000000;
var buffer = new byte[size];
var buffer2 = new byte[size];
while ((count = file.Read(buffer, 0, buffer.Length)) > 0)
{
file2.Read(buffer2, 0, buffer2.Length);
for (int i = 0; i < count; i++)
{
if (buffer[i] != buffer2[i])
{
return false;
}
}
}
}
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.