Subversion Repositories delphiutils

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. unit FileMD5Main;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, StdCtrls, ShellAPI;
  8.  
  9. type
  10.   TForm1 = class(TForm)
  11.     Edit1: TEdit;
  12.     Edit2: TEdit;
  13.     Label1: TLabel;
  14.     Label2: TLabel;
  15.     procedure FormShow(Sender: TObject);
  16.   private
  17.     choosenfile: string;
  18.   protected
  19.     procedure ChooseFile(f: string);
  20.     procedure CalcMD5();
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.dfm}
  29.  
  30. uses
  31.   MD5, DropFiles;
  32.  
  33. resourcestring
  34.   lng_file_not_found = 'The file "%s" was not found!';
  35.   lng_is_a_dir = 'MD5 checksum can only calculated for files.';
  36.  
  37. function MyFileSize(fn: string): int64;
  38. var
  39.   fs: TFileStream;
  40. begin
  41.   fs := TFileStream.Create(fn, fmOpenRead or fmShareDenyWrite);
  42.   try
  43.     result := fs.Size;
  44.   finally
  45.     fs.Free;
  46.   end;
  47. end;
  48.  
  49. procedure TForm1.CalcMD5();
  50. const
  51.   ZERO_HASH = 'd41d8cd98f00b204e9800998ecf8427e';
  52. resourcestring
  53.   lng_fatal_error = 'Fatal error!';
  54. var
  55.   h: string;
  56. begin
  57.   h := MD5Print(MD5File(choosenfile));
  58.  
  59.   // Additional check
  60.   if (MyFileSize(choosenfile) <> 0) and (h = ZERO_HASH) then
  61.   begin
  62.     ShowMessage(lng_fatal_error);
  63.     Exit;
  64.   end;
  65.  
  66.   Edit1.Text := h;
  67. end;
  68.  
  69. procedure TForm1.ChooseFile(f: string);
  70. begin
  71.   choosenfile := f;
  72.   Label1.Caption := ExtractFileName(f);
  73.   Edit2.Text := f;
  74. end;
  75.  
  76. procedure TForm1.FormShow(Sender: TObject);
  77. var
  78.   f, nf: string;
  79.   i: Integer;
  80. resourcestring
  81.   lng_syntax = 'Syntax: %s filename';
  82. begin
  83.   if ParamCount() < 1 then
  84.   begin
  85.     // ShowMessageFmt(lng_syntax, [ExtractFileName(Application.ExeName)]);
  86.     Form2.SetMsg(Format(lng_syntax, [ExtractFileName(Application.ExeName)]));
  87.     Form2.SetCap(Caption);
  88.     Form2.ShowModal;
  89.     Close;
  90.     Exit;
  91.   end;
  92.  
  93.   f := ParamStr(1);
  94.  
  95.   if DirectoryExists(f) then
  96.   begin
  97.     ShowMessage(lng_is_a_dir);
  98.     Close;
  99.     Exit;
  100.   end;
  101.  
  102.   if not FileExists(f) then
  103.   begin
  104.     ShowMessageFmt(lng_file_not_found, [f]);
  105.     Close;
  106.     Exit;
  107.   end;
  108.  
  109.   if ParamCount() > 1 then
  110.   begin
  111.     for i := 2 to ParamCount() do
  112.     begin
  113.       nf := ParamStr(i);
  114.  
  115.       ShellExecute(Handle, 'open', PChar('"'+Application.ExeName+'"'),
  116.         PChar('"'+nf+'"'), PChar('"'+ExtractFilePath(Application.ExeName)+'"'), SW_NORMAL);
  117.     end;
  118.   end;
  119.  
  120.   ChooseFile(f);
  121.   CalcMD5();
  122. end;
  123.  
  124. end.
  125.