文件比较
2014-10-24 16:19:21 访问(1862) 赞(0) 踩(0)
/// <summary>
/// byte比较
/// </summary>
/// <param name="one"></param>
/// <param name="two"></param>
/// <returns></returns>
public bool CompareByteArray(byte[] one, byte[] two)
{
if (one == null && two == null)
return true;
if (one.Length != two.Length)
return false;
int iLen = one.Length;
for (int i = 0; i < iLen; ++i)
{
if (one[i] != two[i])
return false;
}
return true;
}
/// <summary>
/// 比较两个文件是否相同
/// </summary>
/// <param name="src"></param>
/// <param name="desc"></param>
/// <returns></returns>
protected string ExcuteFileCompare(string src,string desc)
{
FileInfo info = new FileInfo(src);
FileInfo descInfo = new FileInfo(desc);
if (!info.Exists)
{
return info.FullName + " 文件不存在。";
}
if (!descInfo.Exists)
{
return descInfo.FullName + " 文件不存在。";
}
if (info.Length != descInfo.Length)
{
return "文件大小不一致。";
}
FileStream sourceFileStream = null;
FileStream destFileStream = null;
string tip = "";
try
{
int iSize = 1024;
int sourceReadCount = 0;
int destReadCount = 0;
byte[] sourceBuffer = new byte[iSize];
byte[] destBuffer = new byte[iSize];
sourceFileStream = new FileStream(info.FullName, FileMode.Open);
destFileStream = new FileStream(descInfo.FullName, FileMode.Open);
if (sourceFileStream.Length != destFileStream.Length)
{
return "文件大小不一致。";
}
sourceFileStream.Seek(0, SeekOrigin.Begin);
while (true)
{
sourceReadCount = sourceFileStream.Read(sourceBuffer, 0, iSize);
destReadCount = destFileStream.Read(destBuffer, 0, iSize);
if (sourceReadCount != destReadCount)
{
tip = "文件大小不一致。";
break;
}
if (sourceReadCount == 0)
break;
// 比较Buffer //
if (!CompareByteArray(sourceBuffer, destBuffer))
{
tip = "不相同。";
break;
}
}
destFileStream.Close();
sourceFileStream.Close();
}
catch (Exception err)
{
tip = err.Message;
}
finally
{
if (sourceFileStream != null)
sourceFileStream.Close();
if (destFileStream != null)
destFileStream.Close();
}
return tip;
}
(如果tip==""代表相同,否则就是相应的错误提示)
标签:
文件比较 


上一条:
下一条:
相关评论
发表评论