www.pudn.com > 计算文件的MD5结果.rar > Main.pas


unit Main; 
 
interface 
 
uses 
  Windows, SysUtils, Types, Classes, Graphics, Controls, Forms, Dialogs, 
  StdCtrls, ComCtrls, HyperLink, ShellAPI; 
 
type 
  TForm1 = class(TForm) 
    Label1: TLabel; 
    ListBox1: TListBox; 
    ButtonAdd: TButton; 
    ButtonDelete: TButton; 
    ButtonCalculate: TButton; 
    ListBox2: TListBox; 
    ButtonSave: TButton; 
    OpenDialog1: TOpenDialog; 
    SaveDialog1: TSaveDialog; 
    Label2: TLabel; 
    Label3: TLabel; 
    ButtonExit: TButton; 
    ButtonClear: TButton; 
    Label4: TLabel; 
    Label5: TLabel; 
    ProgressBarCurrent: TProgressBar; 
    ProgressBarTotal: TProgressBar; 
    ButtonAbort: TButton; 
    HyperLink1: THyperLink; 
    procedure ButtonAddClick(Sender: TObject); 
    procedure ButtonDeleteClick(Sender: TObject); 
    procedure ButtonClearClick(Sender: TObject); 
    procedure ButtonCalculateClick(Sender: TObject); 
    procedure ButtonSaveClick(Sender: TObject); 
    procedure ButtonExitClick(Sender: TObject); 
    procedure ListBox1Click(Sender: TObject); 
    procedure ButtonCopyrightClick(Sender: TObject); 
    procedure ButtonAbortClick(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure HyperLink1Click(Sender: TObject); 
    procedure ListBox2Click(Sender: TObject); 
  private 
    Calculating, UserAbort: boolean; 
    procedure DoCalc; 
  public 
    { Public declarations } 
  end; 
 
var 
  Form1: TForm1; 
 
implementation 
 
uses MD5; 
 
{$R *.dfm} 
 
procedure TForm1.ButtonAddClick(Sender: TObject); 
begin 
  if OpenDialog1.Execute then 
    ListBox1.Items.AddStrings(OpenDialog1.Files); 
end; 
 
procedure TForm1.ButtonDeleteClick(Sender: TObject); 
begin 
  if ListBox1.ItemIndex >= 0 then 
    begin 
    if ListBox2.ItemIndex >= 0 then 
      begin 
      if ListBox2.Items.Count = ListBox1.Items.Count then 
        try 
          ListBox2.Items.Delete(ListBox1.ItemIndex); 
          ListBox1.Items.Delete(ListBox1.ItemIndex); 
        except 
        end; 
      end 
    end 
end; 
 
procedure TForm1.ButtonClearClick(Sender: TObject); 
begin 
  ListBox1.Clear; 
end; 
 
procedure TForm1.DoCalc; 
const 
  cnBufferSize = 16384; 
var 
  i, j: integer; 
  InputFile: TFileStream; 
  FileName: string; 
  FileSize: Int64; 
  CurrentSize: Int64; 
  TotalSize: Int64; 
  Buffer: pointer; 
  BufferString: string; 
  VisibleString: string; 
  NewStep, OldStep: integer; 
  Context: MD5Context; 
  Digest: MD5Digest; 
begin 
  SetLength (BufferString, cnBufferSize); 
  Buffer := Addr (BufferString[1]); 
  try 
//  try 
    for i := 0 to ListBox1.Items.Count-1 do 
      begin 
      ListBox1.ItemIndex := i; 
      Application.ProcessMessages; 
      if UserAbort then Abort; 
 
      if FileExists (ListBox1.Items[i]) then 
        begin 
        FileName := ListBox1.Items.Strings[i]; 
        InputFile := TFileStream.Create (FileName, fmOpenRead); 
        FileSize := InputFile.Size; 
        TotalSize := FileSize; 
        CurrentSize := 0; 
        OldStep := 0; 
        ProgressBarCurrent.Position := 0; 
 
	MD5Init(Context); 
 
        While FileSize > cnBufferSize do 
          begin 
          Application.ProcessMessages; 
          if UserAbort then Abort; 
          InputFile.Read(Buffer^, cnBufferSize); 
          FileSize := FileSize - cnBufferSize; 
 
          MD5Update(Context, Buffer, cnBufferSize); 
 
          CurrentSize := CurrentSize + cnBufferSize; 
          NewStep := Round (CurrentSize * 100 / TotalSize); 
          if NewStep > OldStep then 
            begin 
            ProgressBarCurrent.StepIt; 
            OldStep := NewStep; 
            end; 
          end; 
        InputFile.Read (Buffer^, FileSize); 
        InputFile.Free; 
        MD5Update(Context, Buffer, FileSize); 
	MD5Final(Context, Digest); 
        ListBox2.Items.Add(MD5Print(Digest)); 
        ListBox2.ItemIndex := i; 
        {idCoderMD5.GetCodedData} 
        end; 
      ProgressBarTotal.StepIt; 
      end; 
//  except 
//    ShowMessage ('Something wrong'); 
//    end; 
  finally 
    end; 
end; 
 
procedure TForm1.ButtonCalculateClick(Sender: TObject); 
begin 
  UserAbort := false; 
  Calculating := true; 
  ButtonAbort.Enabled := true; 
  ButtonCalculate.Enabled := false; 
  ButtonAdd.Enabled := false; 
  ButtonDelete.Enabled := false; 
  ButtonClear.Enabled := false; 
  ButtonSave.Enabled := false; 
  ButtonExit.Enabled := false; 
  ProgressBarCurrent.Position := 0; 
  ProgressBarTotal.Position := 0; 
  ProgressBarTotal.Min := 0; 
  ProgressBarTotal.Max := ListBox1.Items.Count; 
  ProgressBarTotal.Step := 1; 
  ListBox2.Items.Clear; 
 
  Screen.Cursor := crHourGlass; 
 
  DoCalc; 
 
  Screen.Cursor := crDefault; 
 
  ButtonAbort.Enabled := false; 
  ButtonCalculate.Enabled := true; 
  ButtonAdd.Enabled := true; 
  ButtonDelete.Enabled := true; 
  ButtonClear.Enabled := true; 
  ButtonSave.Enabled := true; 
  ButtonExit.Enabled := true; 
  Calculating := false; 
  ProgressBarCurrent.Position := 0; 
  ProgressBarTotal.Position := 0; 
end; 
 
procedure TForm1.ButtonSaveClick(Sender: TObject); 
var 
  OutputFile: TextFile; 
  i: integer; 
begin 
  if SaveDialog1.Execute then 
    begin 
    AssignFile (OutputFile, SaveDialog1.FileName); 
    ReWrite (OutputFile); 
    for i := 0 to ListBox1.Items.Count - 1 do 
      begin 
      WriteLn (OutputFile, ListBox2.Items.Strings[i], ' --- ', ListBox1.Items[i]); 
      end; 
    CloseFile (OutputFile); 
    end; 
end; 
 
procedure TForm1.ButtonExitClick(Sender: TObject); 
begin 
  Close; 
end; 
 
procedure TForm1.ListBox1Click(Sender: TObject); 
var 
  Index: integer; 
begin 
  Index := ListBox1.ItemIndex; 
  if ListBox2.Items.Count >=Index then 
    ListBox2.ItemIndex := Index; 
end; 
 
procedure TForm1.ButtonCopyrightClick(Sender: TObject); 
begin 
//                     fork 
end; 
 
procedure TForm1.ButtonAbortClick(Sender: TObject); 
begin 
  UserAbort := true; 
end; 
 
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
  if Calculating then Action := caNone; 
end; 
 
{ 
procedure TForm1.DoCalc; 
const 
  cnBufferSize = 4096; 
var 
  i, j: integer; 
  InputFile: TFileStream; 
  FileName: string; 
  FileSize: Int64; 
  CurrentSize: Int64; 
  TotalSize: Int64; 
  Buffer: pointer; 
  BufferString: string; 
  OutputString: string; 
  VisibleString: string; 
  NewStep, OldStep: integer; 
begin 
  SetLength (BufferString, cnBufferSize); 
  Buffer := Addr (BufferString[1]); 
  try 
//  try 
    for i := 0 to ListBox1.Items.Count-1 do 
      begin 
      ListBox1.ItemIndex := i; 
      Application.ProcessMessages; 
      if UserAbort then Abort; 
      idCoderMD5.Reset; 
      idCoderMD5.AutoCompleteInput := false; 
      if FileExists (ListBox1.Items[i]) then 
        begin 
        FileName := ListBox1.Items.Strings[i]; 
        InputFile := TFileStream.Create (FileName, fmOpenRead); 
        FileSize := InputFile.Size; 
        TotalSize := FileSize; 
        CurrentSize := 0; 
        OldStep := 0; 
        ProgressBarCurrent.Position := 0; 
        While FileSize > cnBufferSize do 
          begin 
          Application.ProcessMessages; 
          if UserAbort then Abort; 
          InputFile.Read(Buffer^, cnBufferSize); 
          FileSize := FileSize - cnBufferSize; 
          CurrentSize := CurrentSize + cnBufferSize; 
          idCoderMD5.CodeString(BufferString); 
 
          NewStep := Round (CurrentSize * 100 / TotalSize); 
          if NewStep > OldStep then 
            begin 
            ProgressBarCurrent.StepIt; 
            OldStep := NewStep; 
            end; 
          end; 
        InputFile.Read (Buffer^, FileSize); 
        InputFile.Free; 
        idCoderMD5.CodeString(BufferString); 
        OutputString := idCoderMD5.CompletedInput; 
        VisibleString := ''; 
        for j := 3 to Length (OutputString) do // 1 and 2 are "0;" 
          VisibleString := VisibleString + IntToHex (Ord (OutputString [j]), 2); 
        ListBox2.Items.Add(VisibleString); 
        ListBox2.ItemIndex := i; 
        // idCoderMD5.GetCodedData 
        end; 
      ProgressBarTotal.StepIt; 
      end; 
//  except 
//    ShowMessage ('Something wrong'); 
//    end; 
  finally 
    end; 
end; 
} 
 
procedure TForm1.HyperLink1Click(Sender: TObject); 
var 
  TempHInst: HInst; 
begin 
  TempHInst := ShellExecute (Self.Handle, 'open', 'http://www.wavecn.com', nil, nil, SW_SHOW); 
end; 
 
procedure TForm1.ListBox2Click(Sender: TObject); 
var 
  Index: integer; 
begin 
  Index := ListBox2.ItemIndex; 
  if ListBox1.Items.Count >=Index then 
    ListBox1.ItemIndex := Index; 
end; 
 
end.