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

📄 unit1.pas

📁 ZIP压缩算法 delphi组件 含代码
💻 PAS
字号:
Unit Unit1;

Interface

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

   ztvRegister,
   ztvBase,
   ztvGbls,
   ztvZipTV,
   ztvUnZip,
   ztvUnBh,
   ztvUnArc,
   ztvUnArj,
   ztvUnCab,
   ztvUnGZip,
   ztvUnLha,
   ztvUnTar,
   ztvUnZoo,
   ztvUUDecode,
   ztvUnAce2,
   ztvUnRar,
   Err_Msgs;

Type
   TForm1 = Class(TForm)
      UnZip1: TUnZip;
      ZipTV1: TZipTV;
      Panel1: TPanel;
      Panel2: TPanel;
      Panel3: TPanel;
      Panel4: TPanel;
      RichEdit1: TRichEdit;
      ListBox1: TListBox;
      Edit1: TEdit;
      BitBtn1: TBitBtn;
      Label1: TLabel;
      OpenDialog1: TOpenDialog;
      Splitter1: TSplitter;
      Procedure BitBtn1Click(Sender: TObject);
      Procedure ListBox1Click(Sender: TObject);
   Private
      { Private declarations }
   Public
      Function CreateDecompressComponent: TUnBase;
   End;

Var
   Form1: TForm1;

Implementation

{$R *.DFM}
{$I ZipTV.inc}

Var
   DecompressComponent: TUnBase;

//-------------------------------------------------------------

// Use the TZipTV (ZipTV1) component to determine the ArcType and create
// the decompression component accordingly to the ztv.ArcType property.

Procedure TForm1.BitBtn1Click(Sender: TObject);
Var
   TSL: TStrings;
Begin
   OpenDialog1.Options := [ofHideReadOnly, ofFileMustExist, ofPathMustExist];
   OpenDialog1.Filter := LoadStr(F_TZIPTV);
   OpenDialog1.FileName := '*.*';
   OpenDialog1.Title := 'Select archive...';
   If OpenDialog1.Execute() Then
   Begin
      ZipTV1.ArchiveFile := OpenDialog1.FileName;

      DecompressComponent := CreateDecompressComponent();
      If DecompressComponent = Nil Then
         Exit;

      DecompressComponent.ArchiveFile := ZipTV1.ArchiveFile;
      If DecompressComponent.IsArcValid(DecompressComponent.ArcType) Then
      Begin
         Edit1.Text := OpenDialog1.FileName;
         TSL := TStringList.Create();
         Try
            ZipTV1.FileSpec.Clear();
            ZipTV1.FileSpec.Add('*.*');
            ZipTV1.RecurseDirs := True;
            ZipTV1.FilesInArchive(TSL);
            ListBox1.Items.Assign(TSL);
         Finally
            TSL.Free();
         End;
      End
      Else
         Edit1.Text := 'Invalid archive...';
   End;
End;
//-------------------------------------------------------------

// create the decompression component which corresponds with the value
// of ZipTV1's ArcType property.

Function TForm1.CreateDecompressComponent: TUnBase;
Begin
   Case ZipTV1.ArcType Of
      atAce, atAceExe:
         Result := TUnACE.Create(Nil);

      atArc, atArcExe:
         Result := TUnARC.Create(Nil);

      atArj, atArjExe:
         Result := TUnARJ.Create(Nil);

      atBh, atBhExe:
         Result := TUnBH.Create(Nil);

      atCab:
         Result := TUnCAB.Create(Nil);

      atGZip:
         Result := TUnGZIP.Create(Nil);

      atLha, atLhaExe, atLzh, atLzhExe:
         Result := TUnLHA.Create(Nil);

      atRar:
         Result := TUnRAR.Create(Nil);

      atTar:
         Result := TUnTAR.Create(Nil);

      atUUE:
         Result := TUUDecode.Create(Nil);

      atZip, atZipExe:
         Begin
            Result := TUnZip.Create(Nil);
            Result.ZipCmntBufSize := 32000;
         End;

      atZoo:
         Result := TUnZOO.Create(Nil);
   Else
      Result := Nil;
      Exit;
   End;
End;
//-------------------------------------------------------------

Procedure TForm1.ListBox1Click(Sender: TObject);
Var
   p: Pchar;
   FileSize: Int64;
   FileName: String;
Begin
   // clear any previous text from the RichEdit1 control
   RichEdit1.Lines.Clear();

   // give RichEdit1 time to breath before refilling with decompressed data
   RichEdit1.Update();

   // get clicked file-name for use in the following ExtractToPointer call
   FileName := ListBox1.Items[ListBox1.ItemIndex];

   // get the file's size for the following memory allocation
   FileSize := ZipTV1.GetFileUnpackedSize(FileName);

   // allocate the memory for pointer p 
   GetMem(p, FileSize + 1);
   Try
      // --------------------------------
      // decompress the file to pointer p
      DecompressComponent.ExtractToPointer(FileName, p);
      // --------------------------------

      // ----------------------------------------------
      // the decompressed data now exists in pointer p.
      // fill the RichEdit1 control with contents of p.
      //RichEdit1.Lines.Add(StrPas(p));
      //RichEdit1.Lines.LoadFromStream(  );
      RichEdit1.Text := StrPas(p);
      // ----------------------------------------------

      // ------------------------------
      // give RichEdit1 time to update
      RichEdit1.Update();
      // ------------------------------
   Finally
      // -------------------------
      // free the allocated memory
      FreeMem(p, FileSize + 1)
      // -------------------------
   End;
End;
//-------------------------------------------------------------

End.

⌨️ 快捷键说明

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