Subversion Repositories autosfx

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 daniel-mar 1
unit mainunit;
2
// demo5 of Delphi Zip - this simple program makes an .EXE archive
3
(************************************************************************
4
 Copyright (C) 2009, 2010  by Russell J. Peters, Roger Aelbrecht,
5
      Eric W. Engler and Chris Vleghert.
6
 
7
   This file is part of TZipMaster Version 1.9.
8
 
9
    TZipMaster is free software: you can redistribute it and/or modify
10
    it under the terms of the GNU Lesser General Public License as published by
11
    the Free Software Foundation, either version 3 of the License, or
12
    (at your option) any later version.
13
 
14
    TZipMaster is distributed in the hope that it will be useful,
15
    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
    GNU Lesser General Public License for more details.
18
 
19
    You should have received a copy of the GNU Lesser General Public License
20
    along with TZipMaster.  If not, see <http://www.gnu.org/licenses/>.
21
 
22
    contact: problems@delphizip.org (include ZipMaster in the subject).
23
    updates: http://www.delphizip.org
24
    DelphiZip maillist subscribe at http://www.freelists.org/list/delphizip
25
************************************************************************)
26
 
27
 
28
{$INCLUDE '..\..\ZipVers19.inc'}
29
{$IFDEF VERD6up}
30
{$WARN UNIT_PLATFORM OFF}
31
{$WARN SYMBOL_PLATFORM OFF}
32
{$ENDIF}
33
 
34
interface
35
 
36
uses
37
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
38
  StdCtrls, Grids, ExtCtrls, {SortGrid,} ZipMstr19, ShlObj;
39
 
40
type
41
  TMainform = class( TForm )
42
    Panel1:       TPanel;
43
    Panel2:       TPanel;
44
    Panel3:       TPanel;
45
    Panel4:       TPanel;
46
    Bevel1:       TBevel;
47
    Bevel2:       TBevel;
48
    Label1:       TLabel;
49
    Label2:       TLabel;
50
    ZipFName:     TLabel;
51
    FilesLabel:   TLabel;
52
    DeleteZipBut: TButton;
53
    NewZipBut:    TButton;
54
    CloseBut:     TButton;
55
    DeleteBut:    TButton;
56
    AddBut:       TButton;
57
    StringGrid1:  TStringGrid;
58
    OpenDialog1:  TOpenDialog;
59
    ZipMaster1: TZipMaster19;
60
 
61
    procedure CloseButClick( Sender: TObject );
62
    procedure NewZipButClick( Sender: TObject );
63
    procedure DeleteZipButClick( Sender: TObject );
64
    procedure ZipMaster1DirUpdate( Sender: TObject );
65
    procedure FormCreate( Sender: TObject );
66
    procedure FillGrid;
67
    procedure AddButClick( Sender: TObject );
68
    procedure ZipMaster1Message( Sender: TObject; ErrCode: Integer; Message: string );
69
    procedure DeleteButClick( Sender: TObject );
70
    procedure FormDestroy( Sender: TObject );
71
  private
72
    { Private declarations }
73
  public
74
    { Public declarations }
75
    DoIt:     Boolean;
76
    FirstDir: String;
77
 
78
    procedure SetZipFName( aCaption: String );
79
    function  GetSpecialFolder( aFolder: Integer; var Location: String ): LongWord;
80
  end;
81
 
82
var
83
  Mainform: TMainform;
84
  Cancelled: Boolean;
85
 
86
implementation
87
 
88
uses addunit;
89
 
90
{$R *.DFM}
91
//{$R 'Res\dzsfx_all.res'}  // bin files
92
 
93
procedure TMainform.CloseButClick( Sender: TObject );
94
begin
95
   Close;
96
end;
97
 
98
procedure TMainform.NewZipButClick( Sender: TObject );
99
begin
100
   if FirstDir = '' then
101
      GetSpecialFolder( CSIDL_DESKTOPDIRECTORY, FirstDir );
102
   with OpenDialog1 do
103
   begin
104
      Title      := 'Create New EXE File';
105
      Options    := Options + [ofHideReadOnly,ofShareAware];
106
      Options    := Options - [ofPathMustExist,ofFileMustExist];
107
      Filter     := 'EXE Files (*.EXE)|*.exe';
108
      InitialDir := FirstDir;
109
      FileName   := '';
110
      if NOT Execute then
111
         Exit;
112
 
113
      FirstDir := ExtractFilePath( FileName );
114
      if Pos( '.', Filename ) = 0 then
115
         // user did not specify extension
116
         FileName := FileName + '.exe'
117
      else
118
         if Uppercase( ExtractFileExt( Filename ) ) <> '.EXE' then
119
         begin
120
            ShowMessage( 'Error: your new archive must end in .EXE' );
121
            Exit;
122
         end;
123
      if FileExists( FileName ) then
124
      begin
125
         if MessageDlg( 'Overwrite Existing File: ' + FileName + '?',
126
                       mtConfirmation, [mbYes,mbNo], 0 ) = mrYes then
127
            DeleteFile( FileName )
128
         else
129
            Exit;  { Don't use the new name }
130
      end;
131
      SetZipFName( Filename );
132
   end; { end with }
133
 
134
  if MessageDlg( 'Protect the archive with a password?', mtConfirmation,[mbYes,mbNo],0) = mrYes then
135
  begin
136
          ZipMaster1.Password := ZipMaster1.GetAddPassword;
137
      if ZipMaster1.Password = '' then
138
      begin
139
         { The 2 password's entered by user didn't match. }
140
         { We'll give him one more try }
141
         if MessageDlg( 'Do you want another chance to enter the password?',
142
            mtConfirmation, [mbYes,mbNo], 0 ) = mrYes then
143
                 begin
144
                        ZipMaster1.Password := ZipMaster1.GetAddPassword;
145
            if ZipMaster1.Password = '' then
146
               Close;
147
         end
148
         else
149
            Close;
150
      end;
151
  end;
152
  ZipMaster1.ZipFileName := ZipFName.Caption;
153
end;
154
 
155
procedure TMainform.DeleteZipButClick( Sender: TObject );
156
var
157
   ans: Boolean;
158
begin
159
   if FileExists( ZipFName.Caption ) then
160
   begin
161
      Ans:=MessageDlg('Are you sure you want to delete: ' + ZipFName.Caption
162
            + '?', mtConfirmation, [mbYes,mbNo], 0) = mrYes;
163
      if Ans then
164
      begin
165
         DeleteFile( ZipFName.Caption );
166
         SetZipFName( '<none>' );
167
         StringGrid1.RowCount := 2; { empty }
168
         StringGrid1.Rows[1].Clear;
169
         ZipMaster1.ZipFilename := '';
170
         FilesLabel.Caption := '0';
171
      end
172
      else
173
         Exit;  { Don't use the new name }
174
   end
175
   else
176
      ShowMessage( 'File not found: ' + ZipFName.Caption );
177
end;
178
 
179
procedure TMainform.ZipMaster1DirUpdate( Sender: TObject );
180
begin
181
   FillGrid;
182
   FilesLabel.Caption:=IntToStr( ZipMaster1.Count );
183
   SetZipFName( ZipMaster1.ZipFilename );
184
end;
185
 
186
procedure TMainform.FormCreate( Sender: TObject );
187
begin
188
  chdir( 'C:\' );
189
 
190
  with StringGrid1 do
191
  begin
192
    { Make sure "goColMoving" is false in object inspector. This lets the
193
      TSortGrid use Mouse Clicks on the col headers. }
194
    RowCount     :=   2;  { first row is fixed, and used for column headers }
195
    Cells[0,0]   := 'File Name';
196
    Cells[1,0]   := 'Compr Size';
197
    Cells[2,0]   := 'Uncmpr Size';
198
    Cells[3,0]   := 'Date/Time';
199
    ColWidths[0] := 316;
200
    ColWidths[1] :=  84;
201
    ColWidths[2] :=  94;
202
    ColWidths[3] := 120;
203
  end;
204
  ZipMaster1.Dll_Load := true;
205
//  ZipMaster1.Load_Zip_Dll;
206
end;
207
 
208
procedure TMainform.FormDestroy( Sender: TObject );
209
begin
210
  ZipMaster1.Dll_Load := false;
211
//  ZipMaster1.Unload_Zip_Dll;
212
end;
213
 
214
procedure TMainForm.FillGrid;
215
var
216
  i: Integer;
217
begin
218
  with StringGrid1 do
219
  begin
220
    { remove everything from grid except col titles }
221
    RowCount := 2;
222
    Rows[1].Clear;
223
    if ZipMaster1.Count = 0 then
224
       Exit;
225
 
226
    StringGrid1.RowCount := ZipMaster1.Count + 1;
227
    for i := 1 to ZipMaster1.Count do
228
    begin
229
//       with ZipDirEntry( ZipMaster1.ZipContents[i - 1]^ ) do  // old
230
           with ZipMaster1.DirEntry[i - 1]{^} do        // new
231
       begin
232
          Cells[0, i] := FileName;
233
          Cells[1, i] := IntToStr( CompressedSize );
234
          Cells[2, i] := IntToStr( UncompressedSize );
235
          Cells[3, i] := FormatDateTime( 'ddddd  t', FileDateToDateTime( DateTime ) );
236
       end; // end with
237
    end; // end for
238
  end; // end with
239
end;
240
 
241
procedure TMainform.AddButClick( Sender: TObject );
242
var
243
   IsOne: String;
244
begin
245
   { In this program, the first Add will cause the SFX code to be added,
246
     and will make the SFX control block.  So, we need to set the SFX
247
     properties now! }
248
   ZipMaster1.SFXDefaultDir := 'C:\'; // default extract directory
249
 
250
   if ZipMaster1.ZipFileName = '' then
251
   begin
252
      ShowMessage( 'Error - create a new archive first' );
253
      Exit;
254
   end;
255
   AddForm.Left   := Left;
256
   AddForm.Top    := Top;
257
   AddForm.Width  := Width;
258
   AddForm.Height := Height;
259
   Cancelled      := False;
260
 
261
   AddForm.ShowModal;  { let user pick filenames to add }
262
   if Cancelled then
263
      Exit;
264
 
265
   if AddForm.SelectedList.Items.Count = 0 then
266
   begin
267
      ShowMessage( 'No files selected' );
268
      Exit;
269
   end;
270
 
271
   with ZipMaster1 do
272
   begin
273
      Verbose    := False;
274
      Trace      := False;
275
      AddOptions := [];
276
      if Password > '' then
277
         AddOptions := AddOptions + [AddEncrypt]  { we want a password }
278
      else
279
         AddOptions := AddOptions - [AddEncrypt]; // don't forget to turn this off!
280
      FSpecArgs.Clear;
281
      FSpecArgs.Assign( AddForm.SelectedList.Items ); { specify filenames }
282
      AddForm.SelectedList.Clear;
283
      Screen.Cursor := crHourGlass;
284
      try
285
         Add;
286
      except
287
         Screen.Cursor := crDefault;
288
         ShowMessage( 'Error in Add; Fatal DLL Exception in mainunit' );
289
      end;
290
      Screen.Cursor := crDefault;
291
      if SuccessCnt = 1 then
292
         IsOne := ' was'
293
      else
294
         IsOne := 's were';
295
      ShowMessage( IntToStr( SuccessCnt ) + ' file' + IsOne + ' added' );
296
   end; { end with }
297
end;
298
 
299
// This is the "OnMessage" event handler
300
procedure TMainform.ZipMaster1Message( Sender: TObject; ErrCode: Integer; Message: string );
301
begin
302
   if ErrCode > 0 then
303
      ShowMessage( 'Error Msg from DLL: ' + Message );
304
end;
305
 
306
procedure TMainform.DeleteButClick( Sender: TObject );
307
var
308
   i:     Integer;
309
   Ans:   Boolean;
310
   IsOne: String;
311
begin
312
   with StringGrid1 do
313
   begin
314
      if ZipMaster1.Count < 1 then
315
      begin
316
         ShowMessage( 'Error - no files to delete' );
317
         Exit;
318
      end;
319
      Ans:=MessageDlg( 'Delete selected files from: ' + ZipMaster1.ZipFileName + '?',
320
               mtConfirmation, [mbYes, mbNo], 0 ) = mrYes;
321
      if not Ans then
322
         Exit;
323
 
324
      ZipMaster1.FSpecArgs.Clear;
325
      for i := Selection.Top to Selection.Bottom do
326
      begin
327
         ZipMaster1.FSpecArgs.Add( Cells[0, i] );
328
      end; { end for }
329
 
330
      if ZipMaster1.FSpecArgs.Count < 1 then
331
      begin
332
         ShowMessage( 'Error - no files selected' );
333
         Exit;
334
      end;
335
   end; { end with }
336
 
337
   ZipMaster1.Verbose := False;
338
   ZipMaster1.Trace   := False;
339
   Screen.Cursor := crHourGlass;
340
   try
341
      ZipMaster1.Delete;
342
   except
343
      Screen.Cursor := crDefault;
344
      ShowMessage( 'Fatal error trying to delete' );
345
   end;
346
   Screen.Cursor := crDefault;
347
   if ZipMaster1.SuccessCnt = 1 then
348
      IsOne := ' was'
349
   else
350
      IsOne := 's were';
351
   ShowMessage( IntToStr( ZipMaster1.SuccessCnt ) + ' file' + IsOne + ' deleted' );
352
end;
353
 
354
procedure TMainform.SetZipFName( aCaption: String );
355
begin
356
   ZipFName.Caption := aCaption;
357
   Font.Assign( ZipFName.Font );
358
   if Canvas.TextWidth( aCaption ) > ZipFName.Width then
359
   begin
360
      ZipFName.Hint     := aCaption;
361
      ZipFName.ShowHint := True;
362
   end
363
   else
364
      ZipFName.ShowHint := False;
365
end;
366
 
367
//---------------------------------------------------------------------------
368
{* Folder types are a.o.
369
 *      CSIDL_DESKTOPDIRECTORY, CSIDL_STARTMENU, CSIDL_SENDTO,
370
 * CSIDL_PROGRAMS, CSIDL_STARTUP etc.
371
 *}
372
function TMainform.GetSpecialFolder( aFolder: Integer; var Location: String ): LongWord;
373
var
374
   pidl:      PItemIDList;
375
   hRes:      HRESULT;
376
   RealPath:  Array[0..MAX_PATH] of Char;
377
   Success:   Boolean;
378
begin
379
   Result := 0;
380
   hRes   := SHGetSpecialFolderLocation( Handle, aFolder, pidl );
381
   if hRes = NO_ERROR then
382
   begin
383
      Success := SHGetPathFromIDList( pidl, RealPath );
384
      if Success then
385
         Location := String( RealPath ) + '\'
386
      else
387
         Result := LongWord( E_UNEXPECTED );
388
      GlobalFreePtr( pidl );
389
   end else
390
      Result := hRes;
391
end;
392
 
393
end.