赞
踩
- function CompareStream(// 比较两个流是否相等
- mStream1, mStream2: TStream // 两个流
- ): Boolean;// 返回两个流是否相等
- var
- vBuffer1, vBuffer2: array[0..$1000-1] of Char;
- vLength1, vLength2: Integer;
- begin
- Result := mStream1 = mStream2;
- if Result then Exit;
- if not Assigned(mStream1) or not Assigned(mStream2) then Exit;// 其中一个为空
- while True do
- begin
- vLength1 := mStream1.Read(vBuffer1, SizeOf(vBuffer1));
- vLength2 := mStream2.Read(vBuffer2, SizeOf(vBuffer2));
- if vLength1 <> vLength2 then Exit;
- if vLength1 =0 then Break;
- if not CompareMem(@vBuffer1[0],@vBuffer2[0], vLength1) then Exit;
- end;
- Result := True;
- end;{ CompareStream }
-
- function CompareFile(// 比较两个文件是否相等
- mFileName1, mFileName2: string // 两个文件
- ): Boolean;// 返回两个文件是否相等
- var
- vFileHandle1, vFileHandle2: THandle;
- vFileStream1, vFileStream2: TFileStream;
-
- vShortPath1, vShortPath2: array[0..MAX_PATH] of Char;
- begin
- Result := False;
- if not FileExists(mFileName1) or not FileExists(mFileName2) then Exit;// 其中一个文件不存在
- GetShortPathName(PChar(mFileName1), vShortPath1, SizeOf(vShortPath1));
- GetShortPathName(PChar(mFileName2), vShortPath2, SizeOf(vShortPath2));
- Result := SameText(vShortPath1, vShortPath2);// 两个文件名是否相同
- if Result then Exit;
- vFileHandle1 := _lopen(PChar(mFileName1), OF_READ or OF_SHARE_DENY_NONE);
- vFileHandle2 := _lopen(PChar(mFileName2), OF_READ or OF_SHARE_DENY_NONE);
- Result :=(Integer(vFileHandle1)>0) and (Integer(vFileHandle2)>0);// 文件是否可以访问
- if not Result then
- begin
- _lclose(vFileHandle1);
- _lclose(vFileHandle2);
- Exit;
- end;
- Result := GetFileSize(vFileHandle1, nil)= GetFileSize(vFileHandle2, nil);// 文件大小是否一致
- if not Result then
- begin
- _lclose(vFileHandle1);
- _lclose(vFileHandle2);
- Exit;
- end;
- vFileStream1 := TFileStream.Create(vFileHandle1);
- vFileStream2 := TFileStream.Create(vFileHandle2);
- try
- Result := CompareStream(vFileStream1, vFileStream2);// 比较两个文件内容是否相同
- finally
- vFileStream1.Free;
- vFileStream2.Free;
- end;
- end;{ CompareFile }
-
- procedure TForm1.FormCreate(Sender: TObject);
- begin
- Caption := BoolToStr(
- CompareFile('c:/temp/temp1.txt','c:/temp/temp2.txt'), True);
- end;
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。