⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.pas

📁 ZIP压缩算法 delphi组件 含代码
💻 PAS
字号:
(*
  Notes:
    1. Problem:
    		A string contains a specific char to signal a EOS
         (end of string).  That char being #0.

       Compressed data can contain this char.  When the compressed
       data is inserted into a string, the position of any #0 char
       becomes the terminating char for the resulting string.

       For example:
          If the resulting string's length is actually 57 and a
          #0 char in the compressed data is located at string
          offset 40, the length of the compressed string now
          becomes 40.

       In this example, we've overcome (or worked around) this limitation.


       Solution:
       	a. Do not assign a compressed string to another string.
         	If the string contains terminating chars (#0) prior to
            the end of the string, the bytes up to the first #0 will
            be copied to the assignment string.

            Use SetLength to set the length of the new string. Use
            the move procedure to copy one string to another.

         b. Do not use Delphi's "Length" to obtain the length of a
            compressed string.  Save the length of a compressed
            string in a global variable.  There's no work around
            with this limitation... the length of a string must be
            saved.

*)
Unit Unit1;

Interface

Uses
   Windows,
   Messages,
   SysUtils,
   Classes,
   Graphics,
   Controls,
   Forms,
   Dialogs,
   ExtCtrls,
   StdCtrls,
   ComCtrls,
   ztvConsts;

Type
   TForm1 = Class(TForm)
      Memo1: TMemo;
      Memo2: TMemo;
      Panel1: TPanel;
      Panel2: TPanel;
      Panel5: TPanel;
      Panel6: TPanel;
      Label1: TLabel;
      Label2: TLabel;
      Button1: TButton;
      Button2: TButton;
      Procedure Button1Click(Sender: TObject);
      Procedure Button2Click(Sender: TObject);
   Private
      { Private declarations }
   Public
      { Public declarations }
   End;

Var
   Form1: TForm1;

Implementation

{$R *.DFM}

Uses
   ztvBase,
   ztvStreams,
   ztvInflate,
   ztvDeflate;

Var
   StrLength: _int;  // saved global string length
   CompressedStr: String;

//-------------------------------------------------------------
// convert a buffer to a string

Function BufToStr(Buf: Pointer; Size: Integer): String;
Begin
   SetLength(Result, Size);
   Move(Buf^, Result[1], Size);
End;
//-------------------------------------------------------------
// compress

Procedure TForm1.Button1Click(Sender: TObject);
Var
   CRC: u_long;
   Level: TDeflateType;
Begin
   If Length(Memo1.Text) > 0 Then
   Begin
      // =======================================================
      // Possible values for level:
      //   dtDeflateS: Stored (no compression - fastest speed)
      //   dtDeflateF: Fast (less compression, increased speed)
      //   dtDeflateN: Normal (medium compression - medium speed)
      //   dtDeflateX: eXtra (most compression - less speed)
      Level := dtDeflateN;
      // =======================================================

      CompressedStr := Memo1.Text;
      StrLength := Length(Memo1.Text);
      CRC := ztvCompress_String_Method1(CompressedStr, StrLength, Level);

      // CRC_MASK defined in ztvConsts.pas
      If CRC <> CRC_MASK Then
      Begin
         Memo2.Text := BufToStr(Pchar(CompressedStr), StrLength);
         Memo1.Text := '';
      End
      Else
         ShowMessage('Compression failed');

   End;
End;
//-------------------------------------------------------------
// decompress

Procedure TForm1.Button2Click(Sender: TObject);
Begin
   If Length(Memo2.Text) > 0 Then
   Begin
      Memo1.Text := ztvDecompress_String_Method2(Memo2.Text, StrLength);
      Memo2.Text := '';
   End;
End;
//-------------------------------------------------------------

End.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -