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

📄 unit1.pas

📁 ziptv为delphi控件
💻 PAS
字号:
// ==========================================
// Note: For Delphi versions prior v6, search
// for, and unrem {$IFNDEF DEL6_OR_HIGHER}
// conditionals.
// ==========================================
Unit Unit1;

Interface

Uses
   Windows,
   Messages,
   SysUtils,
   Classes,
   Graphics,
   Controls,
   Forms,
   Dialogs,
   StdCtrls,
   ExtCtrls,
   ComCtrls,
   Menus,
   ztvBase,
   ztvGbls,
   ztvStreams,
   ztvZipTV,
   ztvUnZip,
   ztvUnBh,
   ztvUnArc,
   ztvUnArj,
   ztvUnCab,
   ztvUnGZip,
   ztvUnLha,
   ztvUnTar,
   ztvUnZoo,
   ztvUUDecode,
   ztvUnAce2,
   ztvUnRar,
   Err_Msgs;

Type
   TForm1 = Class(TForm)
      Image1: TImage;
      MainMenu1: TMainMenu;
      mnuFile1: TMenuItem;
      mnuOpen1: TMenuItem;
      mnuN1: TMenuItem;
      mnuExit1: TMenuItem;
      OpenDialog1: TOpenDialog;
      Panel1: TPanel;
      Label4: TLabel;
      Label3: TLabel;
      Label1: TLabel;
      Label2: TLabel;
      Label5: TLabel;
      Label6: TLabel;
      Memo1: TMemo;
      UpDown1: TUpDown;
      Procedure FormCreate(Sender: TObject);
      Procedure FormDestroy(Sender: TObject);
      Procedure mnuOpen1Click(Sender: TObject);
      Procedure mnuExit1Click(Sender: TObject);
      Procedure CreateDecompressComponent(Var Component: TUnBase);
      Procedure UpDown1Click(Sender: TObject; Button: TUDBtnType);
   Private
      { Private declarations }
   Public
      { Public declarations }
   End;

Var
   Form1: TForm1;

Implementation

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

Var
   DecompressedStream: TDecompMemoryStream;

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

Procedure TForm1.FormCreate(Sender: TObject);
Begin
   DecompressedStream := TDecompMemoryStream.Create();
   UpDown1.Max := 0;
End;
//-------------------------------------------------------------

Procedure TForm1.FormDestroy(Sender: TObject);
Begin
   DecompressedStream.Destroy();
End;
//-------------------------------------------------------------
(* Use the TZipTV component to determine the ArcType and create the	*)
(* decompression component accordingly to the ztv.ArcType property. *)

Procedure TForm1.CreateDecompressComponent(Var Component: TUnBase);
Var
   ztv: TZipTV;
Begin
   ztv := TZipTV.Create(Nil);
   Try
      ztv.ArchiveFile := OpenDialog1.FileName;
      Case ztv.ArcType Of
         atArc, atArcExe:
            Component := TUnARC.Create(Nil);

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

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

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

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

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

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

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

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

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

         atZoo:
            Component := TUnZOO.Create(Nil);
      Else
         Component := Nil;
         Exit;
      End;
   Finally
      ztv.Destroy;
   End;
End;
//-------------------------------------------------------------

// Decompress archive to (only files matching FileSpec) to DecompressedStream.
// Be sure to free destroy DecompressedStream to free memory.  In this demo
// we do this in Form1.Destory.

Procedure TForm1.mnuOpen1Click(Sender: TObject);
Var
   DecompressComponent: TUnBase;
Begin
   OpenDialog1.Title := 'Open Archive...';
   OpenDialog1.Filter := LoadStr(F_TZIPTV); //F_TZIPTV in err_msgs.pas & err_msgs.rc
   OpenDialog1.Options := [ofHideReadOnly, ofFileMustExist, ofPathMustExist];
   If OpenDialog1.Execute Then
   Begin
      Caption := OpenDialog1.FileName;

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

      Try

         (* clear DecompressedStream, releasing all allocated memory *)
         (* from previously displayed archives.								*)
         DecompressedStream.ClearMemory();

         (*** activate the decompression ***)
         DecompressComponent.ArchiveFile := OpenDialog1.FileName;
         DecompressComponent.FileSpec.Add('*.*');
          DecompressComponent.ExtractToMemoryStream(DecompressedStream);
      Finally
         DecompressComponent.Free();
      End;

      (* set the UpDown control's Min, Max, and Position properties *)
      UpDown1.Min := 0;
      UpDown1.Max := DecompressedStream.FileCount - 1;
      If UpDown1.Max > 0 Then
      Begin
         UpDown1.Position := 0;
         UpDown1Click(Self, btNext);
      End;

      Label6.Caption := IntToStr(DecompressedStream.FileCount);
   End;
End;
//-------------------------------------------------------------

Procedure TForm1.mnuExit1Click(Sender: TObject);
Begin
   Close();
End;
//-------------------------------------------------------------

Procedure TForm1.UpDown1Click(Sender: TObject; Button: TUDBtnType);
Var
   Signattr: Integer;                   //opened file's 2 to 4 char signattr
   FileSize: Integer;                   //size of file in decompressed stream
   StreamPos: Integer; //Position of begining of file in decompressed stream
   FileName: String;
//{$IFNDEF DEL6_OR_HIGHER}
//   DelphiCompatibleStrm: TMemoryStream;
//   FileStream: TFileStream;             //stream used to copy specific files from the
//{$ELSE}
   FileStream: TFileStream32;           //stream used to copy specific files from the
//{$ENDIF}
Const
   (* File signatures *)
   IconSignature = 65536;
   MetaSignature = -1698247209;
   BitmapSignature = 19778;
Begin
   (* display its index *)
   Label2.Caption := IntToStr(UpDown1.Position + 1);

   (* Retrieve the specific file's filename *)
   Label4.Caption := DecompressedStream.FileName[UpDown1.Position];

   (* Get the specific file's size from DecompressedStream *)
   FileSize := DecompressedStream.FileSize[UpDown1.Position];
   If FileSize > 32000 Then
      FileSize := 32000;

   (* Get the offset of the desired file from Decompressed Stream *)
   StreamPos := DecompressedStream.FileOffset[UpDown1.Position];

   (* Get the compressed file's first four bytes to check its signature *)
   DecompressedStream.Position := StreamPos;
   DecompressedStream.Read(Signattr, SizeOf(Signattr));

   (* reset the position to beginning of Signattr *)
   DecompressedStream.Position := StreamPos;
   If FileSize > 0 Then
   Begin

      FileName := GetTempFileNameStr(GetTempPathStr());

      //{$IFNDEF DEL6_OR_HIGHER}
      //DelphiCompatibleStrm := Nil;
      //FileStream := TFileStream.Create(FileName, fmCreate);
      //{$ELSE}
      FileStream := TFileStream32.Create(FileName, fmCreate);
      //{$ENDIF}

      Try
         Try

            (* Copy the specific file from the DecompressedStream to FileStream *)

//{$IFNDEF DEL6_OR_HIGHER}
//            // Delphi version prior to version 6 must copy the TZipTV stream
//            // (DecompressStream) into a Delphi compatible TFileStream
//            // (DelphiCompatibleStrm).  There are no compatible problems in
//            // Delphi version 6+
//            DelphiCompatibleStrm := TMemoryStream.Create();
//
//            ztvStreamToTStream(DecompressedStream, DelphiCompatibleStrm, FileSize);
//
//            DelphiCompatibleStrm.Position := 0;
//            If FileStream.CopyFrom(DelphiCompatibleStrm, FileSize) = FileSize Then
//{$ELSE}
            If FileStream.CopyFrom(DecompressedStream, FileSize) = FileSize Then
//{$ENDIF}
            Begin
               FileStream.Position := 0;

               (* if the specified file is an icon... *)
               If Signattr = IconSignature Then
                  Image1.Picture.Icon.LoadFromStream(FileStream)

                  (* if the specified file is a MetaFile... *)
               Else
                  If Signattr = MetaSignature Then
                     Image1.Picture.Metafile.LoadFromStream(FileStream)

                     (* if the specified file is a BitMap... *)
                  Else
                     If LoWord(Signattr) = BitmapSignature Then
                        Image1.Picture.BitMap.LoadFromStream(FileStream)

                        (* if the specified file is a text or any other type of file... *)
                     Else
                        Memo1.Lines.LoadFromStream(FileStream);

            End;
         Except
            Memo1.Clear();
         End;
      Finally
         FileStream.Free();

         If FileExists(FileName) Then
            DeleteFile(FileName);

//{$IFNDEF DEL6_OR_HIGHER}
//         If DelphiCompatibleStrm <> Nil Then
//            DelphiCompatibleStrm.Free();
//{$ENDIF}
      End;

   End
   Else
      Memo1.Clear();

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

End.

⌨️ 快捷键说明

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