Subversion Repositories autosfx

Rev

Blame | Last modification | View Log | RSS feed

  1. { unit1.pas   a demo of freeware ZIP/UNZIP DLLs for Delphi.
  2.   This is the main unit of the advanced Zip/Unzip Demo projoect, demo3.
  3.   The DLL is required to run this program: DelZip190.DLL.
  4.   Also, both of these VCL's must be installed before opening this
  5.   project in Delphi: ZipMaster and SortGrid. }
  6. (************************************************************************
  7.  Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht,
  8.       Eric W. Engler and Chris Vleghert.
  9.  
  10.    This file is part of TZipMaster Version 1.9.
  11.  
  12.     TZipMaster is free software: you can redistribute it and/or modify
  13.     it under the terms of the GNU Lesser General Public License as published by
  14.     the Free Software Foundation, either version 3 of the License, or
  15.     (at your option) any later version.
  16.  
  17.     TZipMaster is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU Lesser General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU Lesser General Public License
  23.     along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
  24.  
  25.     contact: problems@delphizip.org (include ZipMaster in the subject).
  26.     updates: http://www.delphizip.org
  27.     DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
  28. ************************************************************************)
  29.  
  30.  
  31. unit Unit1;
  32.  
  33. interface
  34. {$include '..\..\ZipVers19.inc'}
  35. uses
  36.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  37.   StdCtrls, ExtCtrls, TZipList, Extrunit, ComCtrls, ZipMstr19;
  38.  
  39. type
  40.   TForm1 = class(TForm)
  41.     Panel1: TPanel;
  42.     RadioTraceOpt: TRadioGroup;
  43.     Panel2: TPanel;
  44.     RadioVerboseOpt: TRadioGroup;
  45.     Label1: TLabel;
  46.     Label2: TLabel;
  47.     Label3: TLabel;
  48.     Edit1: TEdit;
  49.     Edit2: TEdit;
  50.     RadioRecurse: TRadioGroup;
  51.     Panel3: TPanel;
  52.     Panel4: TPanel;
  53.     ProgressBar1: TProgressBar;
  54.     FileBeingZipped: TLabel;
  55.     NewBut: TButton;
  56.     OpenBut: TButton;
  57.     ZipFName: TLabel;
  58.     OpenDialog: TOpenDialog;
  59.     VersionBut: TButton;
  60.     ListBut: TButton;
  61.     AddBut: TButton;
  62.     DeleteBut: TButton;
  63.     ExtractBut: TButton;
  64.     AbortBut: TButton;
  65.     ExitBut: TButton;
  66.     RichEdit1: TRichEdit;
  67.     RadioDirNames: TRadioGroup;
  68.     ZipMaster1: TZipMaster19;
  69.     procedure FormCreate(Sender: TObject);
  70.     procedure VersionButClick(Sender: TObject);
  71.     procedure ExitButClick(Sender: TObject);
  72.     procedure ListButClick(Sender: TObject);
  73.     procedure AddButClick(Sender: TObject);
  74.     procedure DeleteButClick(Sender: TObject);
  75.     procedure AbortButClick(Sender: TObject);
  76.     procedure ResetProgressBar;
  77.     procedure ExtractButClick(Sender: TObject);
  78.     procedure OpenButClick(Sender: TObject);
  79.     procedure NewButClick(Sender: TObject);
  80.     procedure FormDestroy(Sender: TObject);
  81.     procedure ZipMaster1Progress(Sender: TObject; details: TZMProgressDetails);
  82.     procedure ZipMaster1Message(Sender: TObject; ErrCode: Integer;
  83.       Message: String);
  84.   private
  85.     { Private declarations }
  86.     procedure SetNewZipFile(FName: String; NewFile: Boolean);
  87.   public
  88.     { Public declarations }
  89.     ExtractDir: String;
  90.     ExpandDirs: Boolean;
  91.     Overwrite: Boolean;
  92.     TotalSize1, TotalProgress1: Int64;
  93. end;
  94.  
  95. var
  96.   Form1: TForm1;
  97.  
  98. implementation
  99.  
  100. {$R *.DFM}
  101.  
  102. {----------------------------------------------------------------}
  103.  
  104. procedure TForm1.ResetProgressBar;
  105. begin
  106.    FileBeingZipped.Caption:='';
  107.    with ProgressBar1 do
  108.    begin   { reset the bar: make it empty }
  109.       min:=1;
  110.       max:=10;
  111.       step:=1;
  112.       position:=min;
  113.    end;
  114. end;
  115.  
  116. procedure TForm1.FormCreate(Sender: TObject);
  117. begin
  118.    RadioTraceOpt.ItemIndex:=0;  { default to no tracing }
  119.    RadioVerboseOpt.ItemIndex:=1;{ default to show verbose msgs }
  120.    RadioRecurse.ItemIndex:=0;   { dflt to no recursive adds of files }
  121.    RadioDirNames.ItemIndex:=1;  { dflt to save dir names }
  122.    AbortBut.Enabled:=False;
  123.    ResetProgressBar;
  124.    { IMPORTANT!  Either make sure you're in the same dir as all your
  125.      files, or else use full pathnames on all your files. }
  126.    Caption:='ZIP Demo3 - ' + GetCurrentDir;
  127.    ZipMaster1.DLL_Load := true;
  128. end;
  129.  
  130. procedure TForm1.VersionButClick(Sender: TObject);
  131. begin
  132.   ShowMessage('DelZip179.dll is ' + ZipMaster1.Dll_Version + #13#10#13#10
  133.    + ' at ' + ZipMaster1.Dll_Path);
  134. end;
  135.  
  136. procedure TForm1.ExitButClick(Sender: TObject);
  137. begin
  138.    Close;
  139. end;
  140.  
  141. procedure TForm1.ListButClick(Sender: TObject);
  142. begin
  143.    { I'm making this modal bec. I don't want the zipfile to be
  144.      changed while it's contents are being viewed. }
  145.    if not FileExists(ZipFName.Caption) then
  146.    begin
  147.       ShowMessage('Error: file not found: ' + ZipFName.Caption);
  148.       exit;
  149.    end;
  150.    VersionBut.Enabled:=False;
  151.    DeleteBut.Enabled:=False;
  152.    AddBut.Enabled:=False;
  153.    ExitBut.Enabled:=False;
  154.    ListBut.Enabled:=False;
  155.    ExtractBut.Enabled:=False;
  156.    ZipForm.ShowModal;  { we're using a separate form for the List function }
  157.    VersionBut.Enabled:=True;
  158.    DeleteBut.Enabled:=True;
  159.    AddBut.Enabled:=True;
  160.    ExitBut.Enabled:=True;
  161.    ListBut.Enabled:=True;
  162.    ExtractBut.Enabled:=True;
  163. end;
  164.  
  165. procedure TForm1.AddButClick(Sender: TObject);
  166. begin
  167.    if ((Length(ZipFName.Caption) = 0) or (Length(Edit1.Text) = 0)) then
  168.    begin
  169.       ShowMessage('Error: you need at least name of zip, and 1st filespec to add');
  170.       exit;
  171.    end;
  172.    VersionBut.Enabled:=False;
  173.    DeleteBut.Enabled:=False;
  174.    AddBut.Enabled:=False;
  175.    ExitBut.Enabled:=False;
  176.    ListBut.Enabled:=False;
  177.    ExtractBut.Enabled:=False;
  178.  
  179.    ZipMaster1.FSpecArgs.Add(Edit1.Text);
  180.    if Edit2.Text > '' then
  181.       ZipMaster1.FSpecArgs.Add(Edit2.Text);
  182.  
  183.    with ZipMaster1 do
  184.    begin
  185.       if RadioVerboseOpt.ItemIndex = 0 then
  186.          Verbose := False
  187.       else
  188.          Verbose := True;
  189.  
  190.       if RadioTraceOpt.ItemIndex = 0 then
  191.          Trace := False
  192.       else
  193.          Trace := True;
  194.  
  195.       AddOptions:=[];
  196.       if RadioDirNames.ItemIndex = 1 then
  197.          AddOptions := AddOptions + [AddDirNames];
  198.  
  199.       if RadioRecurse.ItemIndex = 1 then
  200.          AddOptions := AddOptions + [AddRecurseDirs];
  201.    end;
  202.  
  203.    Cursor:=crHourGlass;
  204.    AbortBut.Enabled:=True;
  205.    ZipMaster1.Add;
  206.    Cursor:=crDefault;
  207.    if ZipMaster1.SuccessCnt < 1 then
  208.       ShowMessage('Error adding files')
  209.    else
  210.       ShowMessage('DONE: Number of files zipped up: ' + IntToStr(ZipMaster1.SuccessCnt));
  211.  
  212.    ResetProgressBar;
  213.    VersionBut.Enabled:=True;
  214.    DeleteBut.Enabled:=True;
  215.    AddBut.Enabled:=True;
  216.    ExitBut.Enabled:=True;
  217.    ListBut.Enabled:=True;
  218.    AbortBut.Enabled:=False;
  219.    ExtractBut.Enabled:=True;
  220. end;
  221.  
  222. procedure TForm1.DeleteButClick(Sender: TObject);
  223. begin
  224.    VersionBut.Enabled:=False;
  225.    DeleteBut.Enabled:=False;
  226.    AddBut.Enabled:=False;
  227.    ExitBut.Enabled:=False;
  228.    ListBut.Enabled:=False;
  229.    ExtractBut.Enabled:=False;
  230.  
  231.    if ((Length(ZipFName.Caption) = 0) or (Length(Edit1.Text) = 0)) then
  232.    begin
  233.       ShowMessage('Error: you need at least name of zip, and 1st filespec to add');
  234.       exit;
  235.    end;
  236.    if not FileExists(ZipFName.Caption) then
  237.    begin
  238.       ShowMessage('Error: file not found: ' + ZipFName.Caption);
  239.       exit;
  240.    end;
  241.  
  242.    ZipMaster1.FSpecArgs.Add(Edit1.Text);
  243.    if Edit2.Text > '' then
  244.       ZipMaster1.FSpecArgs.Add(Edit2.Text);
  245.  
  246.    with ZipMaster1 do
  247.    begin
  248.       if RadioVerboseOpt.ItemIndex = 0 then
  249.          Verbose := False
  250.       else
  251.          Verbose := True;
  252.  
  253.       if RadioTraceOpt.ItemIndex = 0 then
  254.          Trace := False
  255.       else
  256.          Trace := True;
  257.    end;
  258.    
  259.    Cursor:=crHourGlass;
  260.    AbortBut.Enabled:=False;  { The DELETE option doesn't support abort }
  261.    ZipMaster1.Delete;
  262.    Cursor:=crDefault;
  263.    if ZipMaster1.SuccessCnt < 1 then
  264.       ShowMessage('Error deleting files')
  265.    else
  266.       ShowMessage('DONE: Number of files deleted: ' + IntToStr(ZipMaster1.SuccessCnt));
  267.  
  268.    ResetProgressBar;
  269.    VersionBut.Enabled:=True;
  270.    DeleteBut.Enabled:=True;
  271.    AddBut.Enabled:=True;
  272.    ExitBut.Enabled:=True;
  273.    ListBut.Enabled:=True;
  274.    AbortBut.Enabled:=False;
  275.    ExtractBut.Enabled:=True;
  276. end;
  277.  
  278. procedure TForm1.AbortButClick(Sender: TObject);
  279. begin
  280.    { This will be passed back to the DLL upon finishing processing
  281.      of the next callback }
  282.    ZipMaster1.Cancel:=True;
  283.    AbortBut.Enabled:=False;
  284. end;
  285.  
  286. procedure TForm1.ExtractButClick(Sender: TObject);
  287. var
  288.    SaveDir: String;
  289. begin
  290.    if (Length(ZipFName.Caption) = 0) then
  291.    begin
  292.       ShowMessage('Error: you need name of zipfile');
  293.       exit;
  294.    end;
  295.    ExtractDir:='';
  296.    SaveDir:=GetCurrentDir;
  297.    { let user select extract directory,
  298.      whether to expand the zip file's dir's,
  299.      and whether to overwrite existing files }
  300.    Extract.ShowModal;
  301.    if Length(ExtractDir) = 0 then
  302.       exit;
  303.  
  304.    SetCurrentDir(ExtractDir);
  305.    if (GetCurrentDir <> ExtractDir) then
  306.    begin
  307.       ShowMessage('Error selecting dir: ' + ExtractDir);
  308.       Exit;
  309.    end;
  310.  
  311.    VersionBut.Enabled:=False;
  312.    DeleteBut.Enabled:=False;
  313.    AddBut.Enabled:=False;
  314.    ExitBut.Enabled:=False;
  315.    ListBut.Enabled:=False;
  316.    ExtractBut.Enabled:=False;
  317.  
  318.    ZipMaster1.FSpecArgs.Clear;
  319.    if Edit1.Text > '' then
  320.       ZipMaster1.FSpecArgs.Add(Edit1.Text);
  321.    if Edit2.Text > '' then
  322.       ZipMaster1.FSpecArgs.Add(Edit2.Text);
  323.  
  324.    with ZipMaster1 do
  325.    begin
  326.       if RadioVerboseOpt.ItemIndex = 0 then
  327.          Verbose := False
  328.       else
  329.          Verbose := True;
  330.  
  331.       if RadioTraceOpt.ItemIndex = 0 then
  332.          Trace := False
  333.       else
  334.          Trace := True;
  335.  
  336.       ExtrOptions:=[];
  337.       if ExpandDirs then
  338.          ExtrOptions := ExtrOptions + [ExtrDirNames];
  339.  
  340.       if OverWrite then
  341.          ExtrOptions := ExtrOptions + [ExtrOverWrite];
  342.    end;
  343.    Cursor:=crHourGlass;
  344.    AbortBut.Enabled:=True;
  345.    RichEdit1.Lines.Add('Unzip base directory: ' + ExtractDir);
  346.    ZipMaster1.Extract;
  347.    Cursor:=crDefault;
  348.    ShowMessage('DONE: Number of files Unzipped: ' + IntToStr(ZipMaster1.SuccessCnt));
  349.  
  350.    VersionBut.Enabled:=True;
  351.    DeleteBut.Enabled:=True;
  352.    AddBut.Enabled:=True;
  353.    ExitBut.Enabled:=True;
  354.    ListBut.Enabled:=True;
  355.    AbortBut.Enabled:=False;
  356.    ExtractBut.Enabled:=True;
  357.  
  358.    SetCurrentDir(SaveDir);
  359.    if (GetCurrentDir <> SaveDir) then
  360.       ShowMessage('Error re-selecting dir: ' + SaveDir);
  361. end;
  362.  
  363. procedure TForm1.OpenButClick(Sender: TObject);
  364. begin
  365.    with OpenDialog do
  366.    begin
  367.       Title:='Open Existing ZIP File';
  368.       Options:=Options+[ofHideReadOnly,ofShareAware,ofPathMustExist,ofFileMustExist];
  369.       Filter :='ZIP Files (*.ZIP)|*.zip';
  370.       if Execute then
  371.          SetNewZipFile(Filename, False);
  372.    end;
  373. end;
  374.  
  375. procedure TForm1.SetNewZipFile(FName: String; NewFile: Boolean);
  376. var
  377.    Ans: Boolean;
  378.    i: Integer;
  379.    Extension: String;
  380. begin
  381.    { get the extension of the filename }
  382.    for i:=Length(FName)-1 downto 0 do
  383.       if FName[i] = '.' then
  384.       begin
  385.          Extension:=Copy(FName, i, Length(FName) - i + 1);
  386.          break;
  387.       end;
  388.    { if the extension isn't ZIP, then append a .zip extension onto it }
  389.    if (CompareText(Extension,'.zip') <> 0) then
  390.       FName:=FName+'.zip';
  391.  
  392.    { see if user wants a new zipfile, and if it already exists }
  393.    if NewFile and FileExists(FName) then
  394.    begin
  395.       Ans:=MessageDlg('Overwrite Existing File: ' + FName + '?',
  396.                           mtConfirmation,[mbYes,mbNo],0)=mrYes;
  397.       if Ans then
  398.          DeleteFile(FName)
  399.       else
  400.          Exit;  { Don't use the new name }
  401.    end;
  402.  
  403.    ZipFName.Caption:=FName;
  404.    ZipMaster1.ZipFilename:=FName;
  405.    { Change to the new drive/directory, so all filespecs will
  406.      be relative to the directory of the ZIP file. This is
  407.      very important for most ZIP application programs!  A
  408.      failure to do this will cause files and directories to
  409.      become all mixed up. }
  410.    SetCurrentDir(ExtractFileDir(FName));
  411.    Caption:='ZIP Demo3 - ' + GetCurrentDir;
  412.  
  413.    VersionBut.Enabled:=True;
  414.    DeleteBut.Enabled:=True;
  415.    AddBut.Enabled:=True;
  416.    ExitBut.Enabled:=True;
  417.    ListBut.Enabled:=True;
  418.    AbortBut.Enabled:=False;
  419.    ExtractBut.Enabled:=True;
  420.    RichEdit1.Lines.Clear;
  421. end;
  422.  
  423. procedure TForm1.NewButClick(Sender: TObject);
  424. begin
  425.    with OpenDialog do
  426.    begin
  427.       Title:='Create New ZIP File';
  428.       Options:=Options+[ofHideReadOnly,ofShareAware];
  429.       Options:=Options-[ofPathMustExist,ofFileMustExist];
  430.       Filter :='ZIP Files (*.ZIP)|*.zip';
  431.       if Execute then
  432.          SetNewZipFile(Filename, True);
  433.    end; { end with }
  434. end;
  435.  
  436. procedure TForm1.FormDestroy(Sender: TObject);
  437. begin
  438.   ZipMaster1.Dll_Load := false;
  439. end;
  440.  
  441. procedure TForm1.ZipMaster1Progress(Sender: TObject; details: TZMProgressDetails);
  442. begin
  443.   Case details.Order Of
  444.     TotalSize2Process:
  445.       Begin
  446.             RichEdit1.Lines.Add('Total uncompressed size: ' + IntToStr(details.TotalSize div 1024 ) + ' Kb');
  447.             with ProgressBar1 do
  448.             begin
  449.                Max        := 100;
  450.                Position   := 1;      // Current position of bar.
  451.                Step       := 1;
  452.             end;
  453.             TotalSize1     := details.TotalSize;
  454.             TotalProgress1 := 0;
  455.       End;
  456.     TotalFiles2Process:
  457.       Begin
  458.             RichEdit1.Lines.Add(IntToStr(details.TotalCount) + ' files to add');
  459.       End;
  460.     NewFile:
  461.       Begin
  462.             FileBeingZipped.Caption := details.ItemName;
  463.       End;
  464.     ProgressUpdate:
  465.       Begin
  466.             ProgressBar1.Position := details.TotalPerCent;
  467.       End;
  468.     EndOfBatch: // Reset the progress bar and filename.
  469.       Begin
  470.             FileBeingZipped.Caption   := '';
  471.             ProgressBar1.Position     := 1;
  472.       End;
  473.   End;
  474.    Application.ProcessMessages;
  475. end;
  476.  
  477. procedure TForm1.ZipMaster1Message(Sender: TObject; ErrCode: Integer;
  478.   Message: String);
  479. begin
  480.    RichEdit1.Lines.Add(Message);
  481.    PostMessage(RichEdit1.Handle, EM_SCROLLCARET, 0, 0);
  482.    Application.ProcessMessages;
  483.    if (ErrCode > 0) and not ZipMaster1.Unattended then
  484.       ShowMessage( 'Error Msg: ' + Message );
  485. end;
  486.  
  487. end.
  488.  
  489.