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

📄 unit1.pas

📁 ZIP压缩算法 delphi组件 含代码
💻 PAS
字号:
(* This demo tests the functionality of accepting an archive as a stream and
   decompressing it's files to disk.  The extracted files are not extracted
   as streams.  The files are extracted to disk.

   To demonstrate the decompressors streaming, an archive is opened from disk
   as a TFileStream originally.  For test purposes, the FileStream is copied
   into a MemoryStream, and the FileStream is destroyed, leaving the
   MemoryStream to be decompressed.

   Note the use of the new GetArcType method (ZipTV v5.6.3).  Most any
   component in the ZipTV Component package can be used with this method
   to determine the archive format (see TArcType defination in ztvGbls.pas).
   *)
Unit Unit1;

Interface

Uses
   Windows,
   Messages,
   SysUtils,
   Classes,
   Graphics,
   Controls,
   Forms,
   Dialogs,
   StdCtrls,
   ztvRegister,
   ztvBase,
   ztvStreams,
   ztvGbls,
   ztvUnArc,
   ztvUnArj,
   ztvUnBh,
   ztvUnCab,
   ztvUnGZip,
   ztvUnLha,
   ztvUnRar,
   ztvUnTar,
   ztvUnZip,
   ztvUnZoo,
   ztvUUDecode;

Type
   TForm1 = Class(TForm)
		Button1: TButton;
      UnZip1: TUnZip;
    	Edit1: TEdit;
    	Label1: TLabel;
      Procedure Button1Click(Sender: TObject);
   Private
      { Private declarations }
   Public
      Function DefineDecompressor(ArcType: TArcType): TUnBase;
   End;

Var
   Form1: TForm1;

Implementation

Uses Unit2;

{$R *.DFM}

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

Function TForm1.DefineDecompressor(ArcType: TArcType): TUnBase;
Begin
   Try
      Case ArcType Of
         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;
   Except
      Result := Nil;
   End;
End;

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

// NOTE: do not assign the ArchiveFile property when calling the
// Extract(s: TStream32) streaming method.

Procedure TForm1.Button1Click(Sender: TObject);
Var
   FS: TStream32;
   MS: TMemoryStream32;
   ArcType: TArcType;
   ExtractComponent: TUnBase;           (* Base class for all decompression components *)
Begin

   If Edit1.Text = '' Then Exit;

   If (Not FileExists(Edit1.Text)) Then
   Begin
      ShowMessage(Edit1.Text +
         ' does not exist, please change filename in Button1.OnClick event handler');
      Exit;
   End;

   // 1. open the file as a TFileStream32
   // 2. copy it's contents to a TMemStream32
   // 3. close the file
   FS := TFileStream32.Create(Edit1.Text, fmOpenRead Or fmShareDenyWrite);
   Try
      Try
         MS := TMemoryStream32.Create();
         // copy the filestream to a memorystream
         MS.CopyFrom(FS, FS.Size);
      Finally
         FS.Free();
      End;
   Except
      Exit;
   End;

   // show the extract-to form
   Form2.ShowModal();

   // use UnZip1 (since it already exists on the form) to make
   // a call to GetArcType.  Any of the compress or decompress
   // components can be used to call the GetArcType method.
   // Normally, you will not need to call GetArcType.  It is
   // called internally, when setting the ArchiveFile property.
   ArcType := UnZip1.GetArcType(MS);

   If Not UnZip1.IsArcValid(ArcType) Then
   Begin
      ShowMessage('Not a valid (or supported) archive');
      Exit;
   End;

   ExtractComponent := DefineDecompressor(ArcType);

   // using the TMemoryStream (see above notes) as the ArchiveFile,
   // use one of the decompressors to extract the file(s) to disk.
   If ExtractComponent <> Nil Then
      Try
         ExtractComponent.FileSpec.Clear();
         ExtractComponent.FileSpec.Add('*.*');
         ExtractComponent.ExtractDir := Form2.Edit1.Text;

         (* 'Restore original folders' CheckBox      	*)
         (* control in unit4.pas 							*)
         ExtractComponent.UseStoredDirs := Form2.CheckBox1.Checked;

         (* DateAttribute property *)
         ExtractComponent.DateAttribute := TDateAttribute(Form2.ComboBox1.ItemIndex);

         (* Handle file overwrites *)
         ExtractComponent.ConfirmOverwrites := Form2.CheckBox2.Checked;
         If Not ExtractComponent.ConfirmOverwrites Then
            ExtractComponent.OverwriteMode := omOverwrite;

         (* RestoreFileAttr property *)
         ExtractComponent.RestoreFileAttr := Form2.CheckBox3.Checked;

         (* CreateStoredDirs property / this property creates any dirs
            stored in an archive *)
         ExtractComponent.CreateStoredDirs := Form2.CheckBox4.Checked;

         (* TranslateOemChar property *)
         ExtractComponent.TranslateOemChar := True; // ...foreign language chars

         (* RecurseDirs property *)
         ExtractComponent.RecurseDirs := Form2.cbRecurseDirs.Checked;

         // ====== Call the Extract method ======
         ExtractComponent.ExtractStreamToDisk(MS);
         // =====================================
      Finally
         MS.Free();
         ExtractComponent.Free();
      End;
End;
//-------------------------------------------------------------

End.

⌨️ 快捷键说明

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