SAVOIR SI DEUX FICHIERS SONT IDENTIQUES

Contenu du snippet

function CompareFile(const FilePathA, FilePathB: AnsiString): Boolean;
Var
 H1, H2, M1, M2, Size: Longword;
 P1, P2: Pointer;
begin
 P1 := nil;
 P2 := nil;
 M1 := 0;
 M2 := 0;
 H1 := INVALID_HANDLE_VALUE;
 H2 := INVALID_HANDLE_VALUE;
 Result := False;
 try
  H1 := CreateFile(PAnsiChar(FilePathA), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
  if H1 <> INVALID_HANDLE_VALUE then
   begin
    H2 := CreateFile(PAnsiChar(FilePathB), GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL or FILE_FLAG_SEQUENTIAL_SCAN, 0);
     if H2 <> INVALID_HANDLE_VALUE then
      begin
       Size := GetFileSize(H1, nil);
       if Size <> GetFileSize(H2, nil) then Exit;
       if Size = 0 then 
       begin 
         Result := True; 
         Exit; 
       end; 
        M1 := CreateFileMapping(H1, nil, PAGE_READONLY, 0, 0, nil);
       if M1 <> 0 then
        begin
         M2 := CreateFileMapping(H2, nil, PAGE_READONLY, 0, 0, nil);
         if M2 <> 0 then
          begin
           P1 := MapViewOfFile(M1, FILE_MAP_READ, 0, 0, 0);
           if P1 <> nil then
            begin
             P2 := MapViewOfFile(M2, FILE_MAP_READ, 0, 0, 0);
             if P2 <> nil then Result := CompareMem(P1, P2, Size);
            end;
          end;
        end;
      end;
   end;
 finally
  if P2 <> nil then UnmapViewOfFile(P2);
  if P1 <> nil then UnmapViewOfFile(P1);
  if M2 <> 0 then CloseHandle(M2);
  if M1 <> 0 then CloseHandle(M1);
  if H2 <> INVALID_HANDLE_VALUE then CloseHandle(H2);
  if H1 <> INVALID_HANDLE_VALUE then CloseHandle(H1);
 end;
end;


Compatibilité : Delphi 5

Disponible dans d'autres langages :

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.