home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 March / Chip_1998-03_cd.bin / zkuste / delphi / komprese / zip / DELZIP12.ZIP / DEMO1.ZIP / MAINUNIT.PAS < prev    next >
Pascal/Delphi Source File  |  1997-09-28  |  13KB  |  434 lines

  1. unit mainunit;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  7.   StdCtrls, Grids, ExtCtrls, SortGrid, ZipMstr;
  8.  
  9. type
  10.   TMainform = class(TForm)
  11.     Panel2: TPanel;
  12.     StringGrid1: TSortGrid;
  13.     OpenDialog1: TOpenDialog;
  14.     ZipMaster1: TZipMaster;
  15.     Panel1: TPanel;
  16.     CloseBut: TButton;
  17.     Label1: TLabel;
  18.     FilesLabel: TLabel;
  19.     Bevel2: TBevel;
  20.     Panel3: TPanel;
  21.     DeleteZipBut: TButton;
  22.     NewZipBut: TButton;
  23.     ZipOpenBut: TButton;
  24.     Panel4: TPanel;
  25.     ZipFName: TLabel;
  26.     Label2: TLabel;
  27.     Bevel1: TBevel;
  28.     DeleteBut: TButton;
  29.     AddBut: TButton;
  30.     ExtractBut: TButton;
  31.     procedure ZipOpenButClick(Sender: TObject);
  32.     procedure CloseButClick(Sender: TObject);
  33.     procedure NewZipButClick(Sender: TObject);
  34.     procedure DeleteZipButClick(Sender: TObject);
  35.     procedure ExtractButClick(Sender: TObject);
  36.     procedure ZipMaster1DirUpdate(Sender: TObject);
  37.     procedure FormCreate(Sender: TObject);
  38.     procedure FillGrid;
  39.     procedure AddButClick(Sender: TObject);
  40.     procedure ZipMaster1Message(Sender: TObject; ErrCode: Integer;
  41.       Message: string);
  42.     procedure ZipMaster1Progress(Sender: TObject; ProgrType: ProgressType;
  43.       FileName: string; FileSize: Longint);
  44.     procedure DeleteButClick(Sender: TObject);
  45.     procedure FormDestroy(Sender: TObject);
  46.   private
  47.     { Private declarations }
  48.   public
  49.     { Public declarations }
  50.   end;
  51.  
  52. var
  53.   Mainform: TMainform;
  54.   ExtractDir: String;
  55.   ExpandDirs: Boolean;
  56.   OverWr: Boolean;
  57.   AllFiles: Boolean;
  58.   Canceled: Boolean;
  59.  
  60. implementation
  61.  
  62. uses extrunit, msgunit, addunit;
  63.  
  64. {$R *.DFM}
  65.  
  66. procedure TMainform.CloseButClick(Sender: TObject);
  67. begin
  68.    Close;
  69. end;
  70.  
  71. procedure TMainform.ZipOpenButClick(Sender: TObject);
  72. begin
  73.    with OpenDialog1 do
  74.    begin
  75.       Title:='Open Existing ZIP File';
  76.       Options:=Options+[ofHideReadOnly,ofShareAware,ofPathMustExist,ofFileMustExist];
  77.       Filter :='ZIP Files (*.ZIP, *.EXE)|*.zip;*.exe';
  78.       if Execute then
  79.       begin
  80.          try
  81.             { assigning the filename will cause the table of contents to be read }
  82.             Screen.Cursor := crHourglass;
  83.             ZipMaster1.ZipFileName:=FileName;
  84.          finally
  85.             Screen.Cursor := crDefault;
  86.             { Set the caption after assigning the filename. This
  87.               way, the filename will be null if the open failed. }
  88.             ZipFName.Caption:=ZipMaster1.ZipFileName;
  89.          end;
  90.       end;
  91.    end;
  92. end;
  93.  
  94. procedure TMainform.NewZipButClick(Sender: TObject);
  95. var
  96.    ans:Boolean;
  97. begin
  98.    with OpenDialog1 do
  99.    begin
  100.       Title:='Create New ZIP File';
  101.       Options:=Options+[ofHideReadOnly,ofShareAware];
  102.       Options:=Options-[ofPathMustExist,ofFileMustExist];
  103.       Filter :='ZIP Files (*.ZIP)|*.zip';
  104.       if Execute then
  105.       begin
  106.         if Pos('.zip', lowercase(Filename)) = 0 then
  107.            FileName:=FileName+'.zip';
  108.         if FileExists(FileName) then
  109.         begin
  110.            Ans:=MessageDlg('Overwrite Existing File: ' + FileName + '?',
  111.                           mtConfirmation,[mbYes,mbNo],0)=mrYes;
  112.            if Ans then
  113.               DeleteFile(FileName)
  114.            else
  115.               Exit;  { Don't use the new name }
  116.         end;
  117.         ZipFName.Caption:=Filename;
  118.         ZipMaster1.ZipFileName:=FileName; { updates the zip dir }
  119.       end;
  120.    end; { end with }
  121. end;
  122.  
  123. procedure TMainform.DeleteZipButClick(Sender: TObject);
  124. var
  125.    ans:Boolean;
  126. begin
  127.    if FileExists(ZipFName.Caption) then
  128.    begin
  129.       Ans:=MessageDlg('Are you sure you want to delete: ' + ZipFName.Caption
  130.             + '?', mtConfirmation,[mbYes,mbNo],0)=mrYes;
  131.       if Ans then
  132.       begin
  133.          DeleteFile(ZipFName.Caption);
  134.          ZipFName.Caption:='<none>';
  135.          StringGrid1.RowCount:=1; { empty }
  136.       end
  137.       else
  138.          Exit;  { Don't use the new name }
  139.    end
  140.    else
  141.       ShowMessage('Zip file not found: ' + ZipFName.Caption);
  142. end;
  143.  
  144. procedure TMainform.ExtractButClick(Sender: TObject);
  145. var
  146.    i: Integer;
  147. begin
  148.    if not FileExists(ZipFName.Caption) then
  149.    begin
  150.       ShowMessage('Error: file not found: ' + ZipFName.Caption);
  151.       Exit;
  152.    end;
  153.    Extract.ShowModal;
  154.    if (ExtractDir = '') or Canceled then
  155.       Exit;  
  156.  
  157.    with StringGrid1 do
  158.    begin
  159.       if (RowCount - 1) < 1 then
  160.       begin
  161.          ShowMessage('Error - no files to extract');
  162.          Exit;
  163.       end;
  164.       ZipMaster1.FSpecArgs.Clear;
  165.       { Get fspecs of selected files, unless user wants all files extracted }
  166.       if not AllFiles then
  167.       begin
  168.          for i := Selection.Top to Selection.Bottom do
  169.          begin
  170.             ZipMaster1.FSpecArgs.Add(Cells[0,i]);
  171.           { ShowMessage('Selecting ' + Cells[0,i]); } { for debugging }
  172.          end; { end for }
  173.          if ZipMaster1.FSpecArgs.Count < 1 then
  174.          begin
  175.             ShowMessage('Error - no files selected');
  176.             Exit;
  177.          end;
  178.       end;
  179.    end; { end with }
  180.  
  181.    MsgForm.Memo1.Clear;
  182.    MsgForm.Show;
  183.    { Put this message into the message form's memo }
  184.    ZipMaster1Message(self,0,'Beginning Extract from ' + ZipMaster1.ZipFileName);
  185.  
  186.    with ZipMaster1 do
  187.    begin
  188.       ExtrBaseDir:=ExtractDir;
  189.       Verbose:=True;
  190.       Trace:=False;
  191.       ExtrOptions:=[];
  192.       if ExpandDirs then
  193.          ExtrOptions:=ExtrOptions+[ExtrDirNames];
  194.       if Overwr then
  195.          ExtrOptions:=ExtrOptions+[ExtrOverwrite];
  196.       try
  197.          Extract;
  198.       except
  199.          ShowMessage('Error in Extract; Fatal DLL Exception in mainunit');
  200.       end;
  201.       ShowMessage(IntToStr(SuccessCnt)+' files were extracted');
  202.    end; { end with }
  203. end;
  204.  
  205. procedure TMainform.ZipMaster1DirUpdate(Sender: TObject);
  206. begin
  207.    FillGrid;
  208.    FilesLabel.Caption:=IntToStr(ZipMaster1.Count);
  209. end;
  210.  
  211. procedure TMainform.FormCreate(Sender: TObject);
  212. begin
  213.   with StringGrid1 do
  214.   begin
  215.     { Make sure "goColMoving" is false in object inspector. This lets the
  216.       TSortGrid use Mouse Clicks on the col headers. }
  217.     FixedRows:=0;
  218.     RowCount:=1;  { first row is fixed, and used for column headers }
  219.     ColCount:=4;
  220.     Cells[0,0] := 'File Name       (Click on a column header to sort)';
  221.     Cells[1,0] := 'Compr Size';
  222.     Cells[2,0] := 'Uncmpr Size';
  223.     Cells[3,0] := 'Date/Time';
  224.     ColWidths[0]:=316;
  225.     ColWidths[1]:=84;
  226.     ColWidths[2]:=84;
  227.     ColWidths[3]:=120;
  228.   end;
  229.   ZipMaster1.Load_Zip_Dll;
  230.   ZipMaster1.Load_Unz_Dll;
  231. end;
  232.  
  233. procedure TMainForm.FillGrid;
  234. var
  235.   i: Integer;
  236. begin
  237.   with StringGrid1 do
  238.   begin
  239.     { Empty data from string grid }
  240.     FixedRows:=0;
  241.     RowCount:=1; { remove everything from grid except col titles }
  242.     if ZipMaster1.Count = 0 then
  243.        Exit;
  244.  
  245.     for i:=0 to ZipMaster1.Count-1 do
  246.     begin
  247.        RowCount := RowCount + 1;
  248.        { We have to set fixed rows after the rowcount is more than 1}
  249.        FixedRows:=1;
  250.        with ZipDirEntry(ZipMaster1.ZipContents[i]^) do
  251.        begin
  252.           { The "-1" below is an offset for the row titles }
  253.           Cells[0,RowCount-1] := FileName;
  254.           Cells[1,RowCount-1] := IntToStr(CompressedSize);
  255.           Cells[2,RowCount-1] := IntToStr(UncompressedSize);
  256.           Cells[3,RowCount-1] := FormatDateTime('ddddd  t',FileDateToDateTime(DateTime));
  257.        end; // end with
  258.     end; // end for
  259.   end; // end with
  260. end;
  261.  
  262. procedure TMainform.AddButClick(Sender: TObject);
  263. begin
  264.    if ZipMaster1.ZipFileName = '' then
  265.    begin
  266.       ShowMessage('Error - open a zip file first');
  267.       Exit;
  268.    end;
  269.    if LowerCase(Copy(ZipMaster1.ZipFileName,Length(ZipMaster1.ZipFileName)-3,4)) = '.exe' then
  270.    begin
  271.      ShowMessage('Error - this pgm can NOT add files to a self-extracting archive');
  272.      // actually it can, but the resulting CRC value for the overall file
  273.      // will be wrong, so I've disabled it
  274.      Exit;
  275.    end;
  276.    AddForm.Left:=Left;
  277.    AddForm.Top:=Top;
  278.    AddForm.Width:=Width;
  279.    AddForm.Height:=Height;
  280.    Canceled:=False;
  281.    AddForm.ShowModal;  { let user pick filenames to add }
  282.    if Canceled then
  283.       Exit;
  284.    if AddForm.SelectedList.Items.Count = 0 then
  285.    begin
  286.       ShowMessage('No files selected');
  287.       Exit;
  288.    end;
  289.    MsgForm.Memo1.Clear;
  290.    MsgForm.Show;
  291.    { Put this message into the message form's memo }
  292.    ZipMaster1Message(self,0,'Beginning Add to ' + ZipMaster1.ZipFileName);
  293.  
  294.    with ZipMaster1 do
  295.    begin
  296.       { We want any DLL error messages to show over the top
  297.         of the message form. }
  298.       Verbose:=True;
  299.       Trace:=False;
  300.       AddOptions:=[];
  301.       if AddForm.RecurseCB.Checked then
  302.          AddOptions:=AddOptions+[AddRecurseDirs];  { we want recursion }
  303.       if AddForm.DirnameCB.Checked then
  304.          AddOptions:=AddOptions+[AddDirNames];  { we want dirnames }
  305.       FSpecArgs.Clear;
  306.       FSpecArgs.Assign(AddForm.SelectedList.Items); { specify filenames }
  307.       AddForm.SelectedList.Clear;
  308.       try
  309.          Add;
  310.       except
  311.          ShowMessage('Error in Add; Fatal DLL Exception in mainunit');
  312.       end;
  313.       ShowMessage(IntToStr(SuccessCnt)+' files were added');
  314.    end; { end with }
  315. end;
  316.  
  317. procedure TMainform.ZipMaster1Message(Sender: TObject; ErrCode: Integer;
  318.   Message: string);
  319. begin
  320.    MsgForm.Memo1.Lines.Add(Message);
  321.    if ErrCode > 0 then
  322.       ShowMessage('Error Msg from DLL: ' + Message);
  323. end;
  324.  
  325. procedure TMainform.ZipMaster1Progress(Sender: TObject;
  326.   ProgrType: ProgressType; FileName: string; FileSize: Longint);
  327. begin
  328.    if ProgrType = NewFile then
  329.    begin
  330.       {ShowMessage('in OnProgress type 1, size= ' + IntToStr(FileSize));}
  331.       MsgForm.FileBeingZipped.Caption:=FileName;
  332.       with MsgForm.ProgressBar1 do
  333.       begin
  334.          min:=1;    { first step }
  335.          max:=10;   { reasonable value for now ... }
  336.          step:=1;   { no. of steps for each "StepIt" }
  337.          position:=min; { current position of bar }
  338.  
  339.          { Max is assigned the approximate # of callbacks }
  340.          if (FileSize div 32768) > 1 then
  341.             Max := FileSize div 32768  { total no of steps }
  342.          else
  343.             Max := 1;
  344.          if (FileSize < 32768) then
  345.             StepIt;   { max out progress for small files }
  346.       end;
  347.    end;
  348.  
  349.    if ProgrType = ProgressUpdate then
  350.    begin
  351.       {ShowMessage('in OnProgress type 2'); }
  352.       with MsgForm.ProgressBar1 do
  353.          if position < Max then
  354.             StepIt;
  355.    end;
  356.  
  357.    if ProgrType = EndOfBatch then
  358.    begin
  359.       { reset the progress bar and filename }
  360.       {ShowMessage('In OnProgress type 3');}
  361.       MsgForm.FileBeingZipped.Caption:='';
  362.       with MsgForm.ProgressBar1 do
  363.       begin
  364.          min:=1;
  365.          max:=10;
  366.          step:=1;
  367.          position:=min;
  368.       end;
  369.    end;
  370.    Application.ProcessMessages;
  371. end;
  372.  
  373. procedure TMainform.DeleteButClick(Sender: TObject);
  374. var
  375.    i: Integer;
  376.    Ans: Boolean;
  377. begin
  378.    if LowerCase(Copy(ZipMaster1.ZipFileName,Length(ZipMaster1.ZipFileName)-3,4)) = '.exe' then
  379.    begin
  380.       ShowMessage('Error - this pgm can NOT delete files from a self-extracting archive');
  381.       // actually it can, but it will corrupt a winzip .exe, so I've disabled it
  382.       Exit;
  383.    end;
  384.  
  385.    with StringGrid1 do
  386.    begin
  387.       if (RowCount - 1) < 1 then
  388.       begin
  389.          ShowMessage('Error - no files to delete');
  390.          Exit;
  391.       end;
  392.       Ans:=MessageDlg('Delete selected files from: '
  393.         + ZipMaster1.ZipFileName + '?',
  394.                    mtConfirmation,[mbYes,mbNo],0)=mrYes;
  395.       if not Ans then
  396.          Exit;
  397.  
  398.       ZipMaster1.FSpecArgs.Clear;
  399.       for i := Selection.Top to Selection.Bottom do
  400.       begin
  401.          ZipMaster1.FSpecArgs.Add(Cells[0,i]);
  402.         { ShowMessage('Selecting ' + Cells[0,i]); for debugging }
  403.       end; { end for }
  404.  
  405.       if ZipMaster1.FSpecArgs.Count < 1 then
  406.       begin
  407.          ShowMessage('Error - no files selected');
  408.          Exit;
  409.       end;
  410.    end; { end with }
  411.  
  412.    MsgForm.Memo1.Clear;
  413.    MsgForm.Show;
  414.    { Put this message into the message form's memo }
  415.    ZipMaster1Message(self,0,'Beginning delete from ' + ZipMaster1.ZipFileName);
  416.  
  417.    ZipMaster1.Verbose:=True;
  418.    ZipMaster1.Trace:=False;
  419.    try
  420.       ZipMaster1.Delete;
  421.    except
  422.       ShowMessage('Fatal error trying to delete');
  423.    end;
  424.    ShowMessage(IntToStr(ZipMaster1.SuccessCnt)+' files were Deleted');
  425. end;
  426.  
  427. procedure TMainform.FormDestroy(Sender: TObject);
  428. begin
  429.   ZipMaster1.Unload_Zip_Dll;
  430.   ZipMaster1.Unload_Unz_Dll;
  431. end;
  432.  
  433. end.
  434.