delphi 对比两个文件是否相同的函数


本文整理自网络,侵删。

 function CompFile(const f1,f2: string): Boolean;
var
ms1,ms2: TMemoryStream;
i,p: Integer;
b1,b2: Byte;
begin
Result := False;
if not (FileExists(f1) and FileExists(f2)) then Exit;

ms1 := TMemoryStream.Create;
ms2 := TMemoryStream.Create;

ms1.LoadFromFile(f1);
ms2.LoadFromFile(f2);

if ms1.Size <> ms2.Size then
begin
ms1.Free;
ms2.Free;
Exit;
end;

Result := True;
Randomize;
for i := 0 to 9 do
begin
p := Random(ms1.Size);
ms1.Position := p;
ms2.Position := p;
ms1.ReadBuffer(b1,1);
ms2.ReadBuffer(b2,1);
if b1 <> b2 then
begin
Result := False;
Break;
end;
end;

ms1.Free;
ms2.Free;
end;
--------------------------------------------------------------------------------
根据 "峪飞鹰" 的指点, 略作修改; 不过暂不用 CompareMem 对比所有内存了, 10 次抽样可能会更快一点.
--------------------------------------------------------------------------------

function CompFile(const f1,f2: string): Boolean;
var
fs1,fs2: TFileStream;
ms: TMemoryStream;
i,p: Integer;
b1,b2: Byte;
begin
Result := False;
if not (FileExists(f1) and FileExists(f2)) then Exit;
fs1 := TFileStream.Create(f1, fmOpenRead);
fs2 := TFileStream.Create(f2, fmOpenRead);

if fs1.Size <> fs2.Size then
begin
fs1.Free;
fs2.Free;
Exit;
end;

Result := True;
Randomize;
for i := 0 to 9 do
begin
p := Random(fs1.Size);
fs1.Position := p;
fs2.Position := p;
fs1.ReadBuffer(b1,1);
fs2.ReadBuffer(b2,1);
if b1 <> b2 then
begin
Result := False;
Break;
end;
end;

fs1.Free;
fs2.Free;
end;

相关阅读 >>

Delphi rest application 与 webbroker application 区别

Delphi 10.3 中安装程序自动升级插件autoupgrader_pro_v5.2

Delphi xe5 android平台 调用 webservice

Delphi ansiendstext 用法之一(路径结尾自动加\)

Delphi 网站后台扫描例子

Delphi idhttp1 xmlpost

Delphi 判断图像格式bmp jpg gif pcx png psd ras sgi tiff err

Delphi 网上获取北京时间 tinifile.readsection 方法在 android 下的应用及各种字符编码问题

Delphi xe5 for android 地理定位gps

Delphi firedac 下的 sqlite [6] - 加密

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...