小智 12
确切地说,正如Nickolay O.所说,你应该在块中读取你的流并使用CompareMem.这是一个例子(包括尺寸测试)......
function IsIdenticalStreams(Source, Destination: TStream): boolean;
const Block_Size = 4096;
var Buffer_1: array[0..Block_Size-1] of byte;
Buffer_2: array[0..Block_Size-1] of byte;
Buffer_Length: integer;
begin
Result := False;
if Source.Size <> Destination.Size then
Exit;
while Source.Position < Source.Size do
begin
Buffer_Length := Source.Read(Buffer_1, Block_Size);
Destination.Read(Buffer_2, Block_Size);
if not CompareMem(@Buffer_1, @Buffer_2, Buffer_Length) then
Exit;
end;
Result := True;
end;
Run Code Online (Sandbox Code Playgroud)
小智 8
daemon_x发布的IsIdenticalStreams函数非常出色 - 但需要一次调整才能正常工作.(Uwe Raabe已经解决了这个问题.)在开始循环之前重置流位置至关重要 - 或者如果在此函数之外已经访问了两个流,则此过程可能会返回错误的TRUE.
这是每次都有效的最终解决方案.我刚刚重命名该函数以适合我的命名约定.感谢daemon_x提供优雅的解决方案.
function StreamsAreIdentical(Stream1, Stream2: TStream): boolean;
const
Block_Size = 4096;
var
Buffer_1: array[0..Block_Size-1] of byte;
Buffer_2: array[0..Block_Size-1] of byte;
Buffer_Length: integer;
begin
Result := False;
if Stream1.Size <> Stream2.Size then exit;
// These two added lines are critical for proper operation
Stream1.Position := 0;
Stream2.Position := 0;
while Stream1.Position < Stream1.Size do
begin
Buffer_Length := Stream1.Read(Buffer_1, Block_Size);
Stream2.Read(Buffer_2, Block_Size);
if not CompareMem(@Buffer_1, @Buffer_2, Buffer_Length) then exit;
end;
Result := True;
end;
Run Code Online (Sandbox Code Playgroud)