main.pas
Upload User: qtt827150
Upload Date: 2022-01-11
Package Size: 298k
Code Size: 4k
Development Platform:

C++ Builder

  1. unit main;
  2. interface
  3. {*********************************************************************
  4. 压缩测试单元
  5. 作者:崔东伟
  6. Email:cuidongwei@yeah.net 或 s72002004@yahoo.com.cn
  7. 发布这一文件的目的是希望它有用,但没有任何担保。甚至没有适合特定目的
  8. 而隐含的担保。作者不承担由此带来的任何问题
  9. *********************************************************************}
  10. uses
  11.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  12.   StdCtrls, Buttons;
  13. const
  14.   BufferSize=2048;
  15. type
  16.   Tmainfm = class(TForm)
  17.     Edit1: TEdit;
  18.     Edit2: TEdit;
  19.     Label1: TLabel;
  20.     Label2: TLabel;
  21.     BitBtn1: TBitBtn;
  22.     BitBtn2: TBitBtn;
  23.     GroupBox1: TGroupBox;
  24.     BitBtn3: TBitBtn;
  25.     BitBtn4: TBitBtn;
  26.     GroupBox2: TGroupBox;
  27.     BitBtn5: TBitBtn;
  28.     BitBtn6: TBitBtn;
  29.     GroupBox3: TGroupBox;
  30.     BitBtn7: TBitBtn;
  31.     BitBtn8: TBitBtn;
  32.     GroupBox4: TGroupBox;
  33.     BitBtn10: TBitBtn;
  34.     BitBtn11: TBitBtn;
  35.     OpenDialog1: TOpenDialog;
  36.     SaveDialog1: TSaveDialog;
  37.     Memo1: TMemo;
  38.     Label3: TLabel;
  39.     procedure BitBtn3Click(Sender: TObject);
  40.     procedure BitBtn1Click(Sender: TObject);
  41.     procedure BitBtn2Click(Sender: TObject);
  42.   private
  43.     { Private declarations }
  44.   public
  45.     { Public declarations }
  46.   end;
  47. var
  48.   mainfm: Tmainfm;
  49. implementation
  50. {$R *.DFM}
  51. uses
  52.   lz77,arith,zlib,lh5unit;
  53.   procedure ZCompress(InStr, OutStr: TStream);
  54.   var
  55.     Zstream:TCustomZlibStream;
  56.     CompressionLevel:TCompressionLevel;
  57.   begin
  58.     CompressionLevel:=clMax;
  59.     ZStream := TCompressionStream.Create(CompressionLevel, OutStr);
  60.     try
  61.       ZStream.CopyFrom(InStr, 0);
  62.     finally
  63.       ZStream.Free;
  64.     end;
  65.   end;
  66.   procedure ZExpand(InStr, OutStr: TStream);
  67.   var
  68.     Zstream:TCustomZlibStream;
  69.     Buffer : Array[0 .. BufferSize - 1] of Byte;
  70.     count:integer;
  71.   begin
  72.     ZStream := TDecompressionStream.Create(InStr);
  73.     try
  74.       while True do
  75.       begin
  76.         Count := ZStream.Read(Buffer, BufferSize);
  77.         if Count <> 0 then OutStr.WriteBuffer(Buffer, Count) else Break;
  78.       end;
  79.     finally
  80.       ZStream.Free;
  81.     end;
  82.   end;
  83. procedure Tmainfm.BitBtn3Click(Sender: TObject);
  84. const
  85.   cstr:array[1..8] of string=
  86.   ('lz77Compress',
  87.    'lz77Expand',
  88.    'ArithCompress',
  89.    'ArithExpand',
  90.    'LHACompress',
  91.    'LHAExpand',
  92.    'ZCompress',
  93.    'ZExpand');
  94. var
  95.   infn,outfn:string;
  96.   infile,outfile:TStream;
  97. begin
  98.   infn:=Edit1.text;
  99.   if not fileexists(infn) then
  100.     raise exception.Create('源文件不存在!');
  101.   outfn:=Edit2.text;
  102.   if fileexists(outfn) then
  103.   begin
  104.     if application.messagebox('输出文件已经存在,要覆盖该文件吗?',
  105.       '警告',MB_ICONQUESTION or MB_YESNO or MB_DEFBUTTON2)=IDNO then exit;
  106.     deletefile(outfn);
  107.   end;
  108.   InFile := TFileStream.Create(infn, fmOpenRead);
  109.   try
  110.     OutFile := TFileStream.Create(outfn, fmCreate);
  111.     try
  112.       case TComponent(Sender).tag of
  113.       1:lz77Compress(Infile,OutFile);
  114.       2:lz77Expand(Infile,OutFile);
  115.       3:ArithCompress(Infile,OutFile);
  116.       4:ArithExpand(Infile,OutFile);
  117.       5:LHACompress(Infile,OutFile);
  118.       6:LHAExpand(Infile,OutFile);
  119.       7:zCompress(Infile,OutFile);
  120.       8:zExpand(Infile,OutFile);
  121.       end;
  122.       if TComponent(Sender).tag in [1,3,5,7] then
  123.       memo1.Lines.add(format('%s 输入%d :bytes 输出:%d bytes 压缩比:%5.2f',
  124.         [cstr[TComponent(Sender).tag],InFile.size,outfile.size,outfile.size*100/InFile.size])+'%')
  125.       else
  126.       memo1.Lines.add(format('%s 输入%d :bytes 输出:%d bytes 压缩比:%5.2f',
  127.         [cstr[TComponent(Sender).tag],InFile.size,outfile.size,InFile.size*100/outfile.size])+'%');
  128.     finally
  129.       outfile.free;
  130.     end;
  131.   finally
  132.     InFile.Free;
  133.   end;
  134. end;
  135. procedure Tmainfm.BitBtn1Click(Sender: TObject);
  136. begin
  137.   if OpenDialog1.Execute then
  138.     edit1.Text:=OpenDialog1.FileName;
  139. end;
  140. procedure Tmainfm.BitBtn2Click(Sender: TObject);
  141. begin
  142.   if SaveDialog1.Execute then
  143.     edit2.Text:=SaveDialog1.FileName;
  144. end;
  145. end.