Cha*_*lie 28
这是一个简单的解决方案,它只读取两个文件并比较数据.它应该不比哈希方法慢,因为两种方法都必须读取整个文件.编辑正如其他人所指出的,由于其简单性,这种实现实际上比哈希方法慢一些.请参阅下面的更快方法.
static bool FilesAreEqual( string f1, string f2 )
{
// get file length and make sure lengths are identical
long length = new FileInfo( f1 ).Length;
if( length != new FileInfo( f2 ).Length )
return false;
// open both for reading
using( FileStream stream1 = File.OpenRead( f1 ) )
using( FileStream stream2 = File.OpenRead( f2 ) )
{
// compare content for equality
int b1, b2;
while( length-- > 0 )
{
b1 = stream1.ReadByte();
b2 = stream2.ReadByte();
if( b1 != b2 )
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)
您可以将其修改为一次读取多个字节,但内部文件流应该已经在缓冲数据,因此即使这个简单的代码也应该相对较快.
编辑感谢您对速度的反馈.我仍然认为compare-all-bytes方法可以和MD5方法一样快,因为两种方法都必须读取整个文件.我怀疑(但不确定)一旦读取了文件,compare-all-bytes方法需要较少的实际计算.无论如何,我为我的初始实现重复了你的性能观察,但是当我添加一些简单的缓冲时,compare-all-bytes方法同样快.以下是缓冲实现,请随时进一步评论!
编辑 Jon B提出另一个好处:在文件实际上不同的情况下,此方法可以在找到第一个不同的字节时立即停止,而哈希方法必须在每种情况下都读取整个文件.
static bool FilesAreEqualFaster( string f1, string f2 )
{
// get file length and make sure lengths are identical
long length = new FileInfo( f1 ).Length;
if( length != new FileInfo( f2 ).Length )
return false;
byte[] buf1 = new byte[4096];
byte[] buf2 = new byte[4096];
// open both for reading
using( FileStream stream1 = File.OpenRead( f1 ) )
using( FileStream stream2 = File.OpenRead( f2 ) )
{
// compare content for equality
int b1, b2;
while( length > 0 )
{
// figure out how much to read
int toRead = buf1.Length;
if( toRead > length )
toRead = (int)length;
length -= toRead;
// read a chunk from each and compare
b1 = stream1.Read( buf1, 0, toRead );
b2 = stream2.Read( buf2, 0, toRead );
for( int i = 0; i < toRead; ++i )
if( buf1[i] != buf2[i] )
return false;
}
}
return true;
}
Run Code Online (Sandbox Code Playgroud)