Debian Codegraph @

plugins/wcx/zip/src/fparchive/abzipper.pas

raw
1 (* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1
3 *
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
8 *
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
12 * License.
13 *
14 * The Original Code is TurboPower Abbrevia
15 *
16 * The Initial Developer of the Original Code is
17 * TurboPower Software
18 *
19 * Portions created by the Initial Developer are Copyright (C) 1997-2002
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * ***** END LICENSE BLOCK ***** *)
25
26 {*********************************************************}
27 {* ABBREVIA: AbZipper.pas *}
28 {*********************************************************}
29 {* ABBREVIA: Non-visual Component with Zip support *}
30 {*********************************************************}
31
32 unit AbZipper;
33
34 {$I AbDefine.inc}
35
36 interface
37
38 uses
39 Classes,
40 AbBrowse, AbZBrows, AbArcTyp, AbZipTyp;
41
42 type
43 TAbCustomZipper = class(TAbCustomZipBrowser)
44 protected {private}
45 FAutoSave : Boolean;
46 FCompressionMethodToUse : TAbZipSupportedMethod;
47 FDeflationOption : TAbZipDeflationOption;
48 FDOSMode : Boolean;
49 FOnConfirmSave : TAbArchiveConfirmEvent;
50 FOnSave : TAbArchiveEvent;
51 FOnArchiveSaveProgress : TAbArchiveProgressEvent;
52 FArchiveSaveProgressMeter : IAbProgressMeter;
53
54 FStoreOptions : TAbStoreOptions;
55
56 protected {methods}
57 procedure DoConfirmSave(Sender : TObject; var Confirm : Boolean);
58 virtual;
59 procedure DoSave(Sender : TObject);
60 virtual;
61 procedure DoArchiveSaveProgress(Sender : TObject; Progress : Byte;
62 var Abort : Boolean);
63
64 procedure InitArchive;
65 override;
66 procedure SetAutoSave(Value : Boolean);
67 procedure SetCompressionMethodToUse(Value : TAbZipSupportedMethod);
68 procedure SetDeflationOption(Value : TAbZipDeflationOption);
69 procedure SetDOSMode( Value : Boolean );
70 procedure SetFileName(const aFileName : string);
71 override;
72 procedure SetStoreOptions( Value : TAbStoreOptions );
73 procedure SetArchiveSaveProgressMeter(const Value: IAbProgressMeter);
74 procedure SetZipfileComment(const Value : AnsiString);
75 override;
76 procedure ZipProc(Sender : TObject; Item : TAbArchiveItem;
77 OutStream : TStream);
78 procedure ZipFromStreamProc(Sender : TObject; Item : TAbArchiveItem;
79 OutStream, InStream : TStream );
80 procedure Notification(Component: TComponent;
81 Operation: TOperation); override;
82 procedure ResetMeters; override;
83
84 protected {properties}
85 property AutoSave : Boolean
86 read FAutoSave
87 write SetAutoSave;
88 property CompressionMethodToUse : TAbZipSupportedMethod
89 read FCompressionMethodToUse
90 write SetCompressionMethodToUse
91 default AbDefCompressionMethodToUse;
92 property DeflationOption : TAbZipDeflationOption
93 read FDeflationOption
94 write SetDeflationOption
95 default AbDefDeflationOption;
96 property DOSMode : Boolean
97 read FDOSMode
98 write SetDOSMode;
99 property StoreOptions : TAbStoreOptions
100 read FStoreOptions
101 write SetStoreOptions
102 default AbDefStoreOptions;
103 property ArchiveSaveProgressMeter : IAbProgressMeter
104 read FArchiveSaveProgressMeter
105 write SetArchiveSaveProgressMeter;
106
107
108 protected {events}
109 property OnConfirmSave : TAbArchiveConfirmEvent
110 read FOnConfirmSave
111 write FOnConfirmSave;
112 property OnSave : TAbArchiveEvent
113 read FOnSave
114 write FOnSave;
115 property OnArchiveSaveProgress : TAbArchiveProgressEvent
116 read FOnArchiveSaveProgress
117 write FOnArchiveSaveProgress;
118
119 public {methods}
120 constructor Create(AOwner : TComponent);
121 override;
122 destructor Destroy;
123 override;
124 procedure AddFiles(const FileMask : string; SearchAttr : Integer);
125 procedure AddFilesEx(const FileMask, ExclusionMask : string; SearchAttr : Integer);
126 procedure AddFromStream(const NewName : string; FromStream : TStream);
127 procedure DeleteAt(Index : Integer);
128 procedure DeleteFiles(const FileMask : string);
129 procedure DeleteFilesEx(const FileMask, ExclusionMask : string);
130 procedure DeleteTaggedItems;
131 procedure FreshenFiles(const FileMask : string);
132 procedure FreshenFilesEx(const FileMask, ExclusionMask : string);
133 procedure FreshenTaggedItems;
134 procedure Move(aItem : TAbArchiveItem; const NewStoredPath : string);
135 procedure Save;
136 procedure Replace(aItem : TAbArchiveItem);
137 end;
138
139 type
140 TAbZipper = class(TAbCustomZipper)
141 published
142 property ArchiveProgressMeter;
143 property ArchiveSaveProgressMeter;
144 property ItemProgressMeter;
145 property AutoSave;
146 property BaseDirectory;
147 property CompressionMethodToUse;
148 property DeflationOption;
149 property DOSMode;
150 property SpanningThreshold;
151 property LogFile;
152 property Logging;
153 property OnArchiveProgress;
154 property OnArchiveSaveProgress;
155 property OnArchiveItemProgress;
156 property OnChange;
157 property OnConfirmProcessItem;
158 property OnConfirmSave;
159 property OnLoad;
160 property OnProcessItemFailure;
161 property OnRequestBlankDisk;
162 property OnRequestImage;
163 property OnRequestLastDisk;
164 property OnRequestNthDisk;
165 property OnSave;
166 property Password;
167 property StoreOptions;
168 property TempDirectory;
169 property Version;
170 property FileName; {must be after OnLoad}
171 end;
172
173 implementation
174
175 uses
176 SysUtils, AbUtils, AbTarTyp, AbGzTyp, AbBzip2Typ, AbExcept, AbZipPrc,
177 AbXzTyp, AbLzmaTyp, AbZstdTyp, DCOSUtils;
178
179 { -------------------------------------------------------------------------- }
180 constructor TAbCustomZipper.Create( AOwner : TComponent );
181 begin
182 inherited Create( AOwner );
183 CompressionMethodToUse := AbDefCompressionMethodToUse;
184 DeflationOption := AbDefDeflationOption;
185 StoreOptions := AbDefStoreOptions;
186 end;
187 { -------------------------------------------------------------------------- }
188 destructor TAbCustomZipper.Destroy;
189 begin
190 inherited Destroy;
191 end;
192 { -------------------------------------------------------------------------- }
193 procedure TAbCustomZipper.AddFiles(const FileMask : string; SearchAttr : Integer);
194 {Add files to the archive where the disk filespec matches}
195 begin
196 if (ZipArchive <> nil) then
197 ZipArchive.AddFiles(FileMask, SearchAttr)
198 else
199 raise EAbNoArchive.Create;
200 DoChange;
201 end;
202 { -------------------------------------------------------------------------- }
203 procedure TAbCustomZipper.AddFilesEx(const FileMask, ExclusionMask : string;
204 SearchAttr : Integer);
205 {Add files that match Filemask except those matching ExclusionMask}
206 begin
207 if (ZipArchive <> nil) then
208 ZipArchive.AddFilesEx(FileMask, ExclusionMask, SearchAttr)
209 else
210 raise EAbNoArchive.Create;
211 DoChange;
212 end;
213 { -------------------------------------------------------------------------- }
214 procedure TAbCustomZipper.AddFromStream(const NewName : string;
215 FromStream : TStream);
216 {Add stream directly to archive}
217 begin
218 if (ZipArchive <> nil) then begin
219 FromStream.Position := 0;
220 ZipArchive.AddFromStream(NewName, FromStream);
221 end else
222 raise EAbNoArchive.Create;
223 DoChange;
224 end;
225 { -------------------------------------------------------------------------- }
226 procedure TAbCustomZipper.DeleteFiles(const FileMask : string);
227 {delete all files from the archive that match the file mask}
228 begin
229 if (ZipArchive <> nil) then
230 ZipArchive.DeleteFiles( FileMask )
231 else
232 raise EAbNoArchive.Create;
233 DoChange;
234 end;
235 { -------------------------------------------------------------------------- }
236 procedure TAbCustomZipper.DeleteAt(Index : Integer);
237 {delete item at Index}
238 begin
239 if (ZipArchive <> nil) then
240 ZipArchive.DeleteAt( Index )
241 else
242 raise EAbNoArchive.Create;
243 DoChange;
244 end;
245 { -------------------------------------------------------------------------- }
246 procedure TAbCustomZipper.DeleteFilesEx(const FileMask, ExclusionMask : string);
247 {Delete files that match Filemask except those matching ExclusionMask}
248 begin
249 if (ZipArchive <> nil) then
250 ZipArchive.DeleteFilesEx(FileMask, ExclusionMask)
251 else
252 raise EAbNoArchive.Create;
253 DoChange;
254 end;
255 { -------------------------------------------------------------------------- }
256 procedure TAbCustomZipper.DeleteTaggedItems;
257 {delete all tagged items from the archive}
258 begin
259 if (ZipArchive <> nil) then
260 ZipArchive.DeleteTaggedItems
261 else
262 raise EAbNoArchive.Create;
263 DoChange;
264 end;
265 { -------------------------------------------------------------------------- }
266 procedure TAbCustomZipper.DoConfirmSave(Sender : TObject; var Confirm : Boolean);
267 begin
268 Confirm := True;
269 if Assigned(FOnConfirmSave) then
270 FOnConfirmSave(Self, Confirm);
271 end;
272 { -------------------------------------------------------------------------- }
273 procedure TAbCustomZipper.DoSave(Sender : TObject);
274 begin
275 if Assigned(FOnSave) then
276 FOnSave(Self);
277 end;
278 { -------------------------------------------------------------------------- }
279 procedure TAbCustomZipper.FreshenFiles(const FileMask : string);
280 {freshen all items that match the file mask}
281 begin
282 if (ZipArchive <> nil) then
283 ZipArchive.FreshenFiles( FileMask )
284 else
285 raise EAbNoArchive.Create;
286 DoChange;
287 end;
288 { -------------------------------------------------------------------------- }
289 procedure TAbCustomZipper.FreshenFilesEx(const FileMask, ExclusionMask : string);
290 {freshen all items matching FileMask except those matching ExclusionMask}
291 begin
292 if (ZipArchive <> nil) then
293 ZipArchive.FreshenFilesEx( FileMask, ExclusionMask )
294 else
295 raise EAbNoArchive.Create;
296 DoChange;
297 end;
298 { -------------------------------------------------------------------------- }
299 procedure TAbCustomZipper.FreshenTaggedItems;
300 {freshen all tagged items}
301 begin
302 if (ZipArchive <> nil) then
303 ZipArchive.FreshenTaggedItems
304 else
305 raise EAbNoArchive.Create;
306 DoChange;
307 end;
308 { -------------------------------------------------------------------------- }
309 procedure TAbCustomZipper.InitArchive;
310 begin
311 inherited InitArchive;
312 if (ZipArchive is TAbZipArchive) then begin
313 {properties}
314 ZipArchive.AutoSave := FAutoSave;
315 TAbZipArchive(ZipArchive).CompressionMethodToUse := FCompressionMethodToUse;
316 TAbZipArchive(ZipArchive).DeflationOption := FDeflationOption;
317 FArchive.DOSMode := FDOSMode;
318 ZipArchive.StoreOptions := FStoreOptions;
319 {events}
320 ZipArchive.OnArchiveSaveProgress := DoArchiveSaveProgress;
321 ZipArchive.OnConfirmSave := DoConfirmSave;
322 TAbZipArchive(ZipArchive).OnRequestBlankDisk := OnRequestBlankDisk;
323 ZipArchive.OnSave := DoSave;
324 TAbZipArchive(ZipArchive).InsertHelper := ZipProc;
325 TAbZipArchive(ZipArchive).InsertFromStreamHelper := ZipFromStreamProc;
326 end;
327 end;
328 { -------------------------------------------------------------------------- }
329 procedure TAbCustomZipper.Move(aItem : TAbArchiveItem; const NewStoredPath : string);
330 {renames the item}
331 begin
332 if (ZipArchive <> nil) then
333 ZipArchive.Move(aItem, NewStoredPath)
334 else
335 raise EAbNoArchive.Create;
336 DoChange;
337 end;
338 { -------------------------------------------------------------------------- }
339 procedure TAbCustomZipper.Replace(aItem : TAbArchiveItem);
340 {replace the item}
341 begin
342 if (ZipArchive <> nil) then
343 ZipArchive.Replace( aItem )
344 else
345 raise EAbNoArchive.Create;
346 DoChange;
347 end;
348 { -------------------------------------------------------------------------- }
349 procedure TAbCustomZipper.Save;
350 begin
351 if (ZipArchive <> nil) then begin
352 ZipArchive.Save;
353 DoChange;
354 end;
355 end;
356 { -------------------------------------------------------------------------- }
357 procedure TAbCustomZipper.SetAutoSave(Value : Boolean);
358 begin
359 FAutoSave := Value;
360 if (ZipArchive <> nil) then
361 ZipArchive.AutoSave := Value;
362 end;
363 { -------------------------------------------------------------------------- }
364 procedure TAbCustomZipper.SetCompressionMethodToUse(
365 Value : TAbZipSupportedMethod);
366 begin
367 FCompressionMethodToUse := Value;
368 if (ZipArchive is TAbZipArchive) then
369 TAbZipArchive(ZipArchive).CompressionMethodToUse := Value;
370 end;
371 { -------------------------------------------------------------------------- }
372 procedure TAbCustomZipper.SetDeflationOption(Value : TAbZipDeflationOption);
373 begin
374 FDeflationOption := Value;
375 if (ZipArchive is TAbZipArchive) then
376 TAbZipArchive(ZipArchive).DeflationOption := Value;
377 end;
378 { -------------------------------------------------------------------------- }
379 procedure TAbCustomZipper.SetDOSMode(Value : Boolean);
380 begin
381 FDOSMode := Value;
382 if (ZipArchive <> nil) then
383 ZipArchive.DOSMode := Value;
384 end;
385 { -------------------------------------------------------------------------- }
386 procedure TAbCustomZipper.SetFileName(const aFileName : string);
387 var
388 ArcType : TAbArchiveType;
389 begin
390 FFileName := aFileName;
391 if (csDesigning in ComponentState) then
392 Exit;
393
394 if Assigned(FArchive) then
395 begin
396 FArchive.Save;
397 FreeAndNil(FArchive);
398 end;
399
400 ArcType := ArchiveType;
401
402 if (FileName <> '') then
403 if mbFileExists(FileName) then begin { open it }
404
405 if not ForceType then
406 ArcType := AbDetermineArcType(FileName, atUnknown);
407
408 case ArcType of
409 atZip, atSpannedZip, atSelfExtZip : begin
410 FArchive := TAbZipArchive.Create(FileName, fmOpenRead or fmShareDenyNone);
411 InitArchive;
412 end;
413
414 atTar : begin
415 FArchive := TAbTarArchive.Create(FileName, fmOpenRead or fmShareDenyNone);
416 inherited InitArchive;
417 end;
418
419 atGZip : begin
420 FArchive := TAbGzipArchive.Create(FileName, fmOpenRead or fmShareDenyNone);
421 TAbGzipArchive(FArchive).TarAutoHandle := FTarAutoHandle;
422 TAbGzipArchive(FArchive).IsGzippedTar := False;
423 inherited InitArchive;
424 end;
425
426 atGZippedTar : begin
427 FArchive := TAbGzipArchive.Create(FileName, fmOpenRead or fmShareDenyNone);
428 TAbGzipArchive(FArchive).TarAutoHandle := FTarAutoHandle;
429 TAbGzipArchive(FArchive).IsGzippedTar := True;
430 inherited InitArchive;
431 end;
432
433 atBzip2 : begin
434 FArchive := TAbBzip2Archive.Create(FileName, fmOpenRead or fmShareDenyNone);
435 TAbBzip2Archive(FArchive).TarAutoHandle := FTarAutoHandle;
436 TAbBzip2Archive(FArchive).IsBzippedTar := False;
437 inherited InitArchive;
438 end;
439
440 atBzippedTar : begin
441 FArchive := TAbBzip2Archive.Create(FileName, fmOpenRead or fmShareDenyNone);
442 TAbBzip2Archive(FArchive).TarAutoHandle := FTarAutoHandle;
443 TAbBzip2Archive(FArchive).IsBzippedTar := True;
444 inherited InitArchive;
445 end;
446
447 atXz, atXzippedTar : begin
448 FArchive := TAbXzArchive.Create(FileName, fmOpenRead or fmShareDenyNone);
449 TAbXzArchive(FArchive).TarAutoHandle := FTarAutoHandle;
450 TAbXzArchive(FArchive).IsXzippedTar := (ArcType = atXzippedTar);
451 inherited InitArchive;
452 end;
453
454 atLzma, atLzmaTar : begin
455 FArchive := TAbLzmaArchive.Create(FileName, fmOpenRead or fmShareDenyNone);
456 TAbLzmaArchive(FArchive).TarAutoHandle := FTarAutoHandle;
457 TAbLzmaArchive(FArchive).IsLzmaTar := (ArcType = atLzmaTar);
458 inherited InitArchive;
459 end;
460
461 atZstd, atZstdTar : begin
462 FArchive := TAbZstdArchive.Create(FileName, fmOpenRead or fmShareDenyNone);
463 TAbZstdArchive(FArchive).TarAutoHandle := FTarAutoHandle;
464 TAbZstdArchive(FArchive).IsZstdTar := (ArcType = atZstdTar);
465 inherited InitArchive;
466 end;
467
468 else
469 raise EAbUnhandledType.Create;
470 end {case};
471 FArchive.Load;
472 FArchiveType := ArcType;
473
474 end else begin { file doesn't exist, so create a new one }
475 if not ForceType then
476 ArcType := AbDetermineArcType(FileName, atUnknown);
477
478 case ArcType of
479 atZip : begin
480 FArchive := TAbZipArchive.Create(FileName, fmCreate or fmShareDenyWrite);
481 InitArchive;
482 end;
483
484 atTar : begin
485 FArchive := TAbTarArchive.Create(FileName, fmCreate or fmShareDenyWrite);
486 inherited InitArchive;
487 end;
488
489 atGZip : begin
490 FArchive := TAbGzipArchive.Create(FileName, fmCreate or fmShareDenyWrite);
491 TAbGzipArchive(FArchive).TarAutoHandle := FTarAutoHandle;
492 TAbGzipArchive(FArchive).IsGzippedTar := False;
493 inherited InitArchive;
494 end;
495
496 atGZippedTar : begin
497 FArchive := TAbGzipArchive.Create(FileName, fmCreate or fmShareDenyWrite);
498 TAbGzipArchive(FArchive).TarAutoHandle := FTarAutoHandle;
499 TAbGzipArchive(FArchive).IsGzippedTar := True;
500 inherited InitArchive;
501 end;
502
503 atBzip2 : begin
504 FArchive := TAbBzip2Archive.Create(FileName, fmCreate or fmShareDenyWrite);
505 TAbBzip2Archive(FArchive).TarAutoHandle := FTarAutoHandle;
506 TAbBzip2Archive(FArchive).IsBzippedTar := False;
507 inherited InitArchive;
508 end;
509
510 atBzippedTar : begin
511 FArchive := TAbBzip2Archive.Create(FileName, fmCreate or fmShareDenyWrite);
512 TAbBzip2Archive(FArchive).TarAutoHandle := FTarAutoHandle;
513 TAbBzip2Archive(FArchive).IsBzippedTar := True;
514 inherited InitArchive;
515 end;
516
517 atXz, atXzippedTar : begin
518 FArchive := TAbXzArchive.Create(FileName, fmCreate or fmShareDenyWrite);
519 TAbXzArchive(FArchive).TarAutoHandle := FTarAutoHandle;
520 TAbXzArchive(FArchive).IsXzippedTar := (ArcType = atXzippedTar);
521 inherited InitArchive;
522 end;
523
524 atLzma, atLzmaTar : begin
525 FArchive := TAbLzmaArchive.Create(FileName, fmCreate or fmShareDenyWrite);
526 TAbLzmaArchive(FArchive).TarAutoHandle := FTarAutoHandle;
527 TAbLzmaArchive(FArchive).IsLzmaTar := (ArcType = atLzmaTar);
528 inherited InitArchive;
529 end;
530
531 atZstd, atZstdTar : begin
532 FArchive := TAbZstdArchive.Create(FileName, fmCreate or fmShareDenyWrite);
533 TAbZstdArchive(FArchive).TarAutoHandle := FTarAutoHandle;
534 TAbZstdArchive(FArchive).IsZstdTar := (ArcType = atZstdTar);
535 inherited InitArchive;
536 end;
537
538 else
539 raise EAbUnhandledType.Create;
540 end {case};
541
542 FArchiveType := ArcType;
543 end;
544 DoChange;
545 end;
546 { -------------------------------------------------------------------------- }
547 procedure TAbCustomZipper.SetStoreOptions(Value : TAbStoreOptions);
548 begin
549 FStoreOptions := Value;
550 if (ZipArchive <> nil) then
551 ZipArchive.StoreOptions := Value;
552 end;
553 { -------------------------------------------------------------------------- }
554 procedure TAbCustomZipper.SetArchiveSaveProgressMeter(const Value: IAbProgressMeter);
555 begin
556 ReferenceInterface(FArchiveSaveProgressMeter, opRemove);
557 FArchiveSaveProgressMeter := Value;
558 ReferenceInterface(FArchiveSaveProgressMeter, opInsert);
559 end;
560 { -------------------------------------------------------------------------- }
561 procedure TAbCustomZipper.SetZipfileComment(const Value : AnsiString);
562 begin
563 if (ZipArchive is TAbZipArchive) then
564 TAbZipArchive(ZipArchive).ZipfileComment := Value
565 else
566 raise EAbNoArchive.Create;
567 end;
568 { -------------------------------------------------------------------------- }
569 procedure TAbCustomZipper.ZipProc(Sender : TObject; Item : TAbArchiveItem;
570 OutStream : TStream);
571 begin
572 AbZip(TAbZipArchive(Sender), TAbZipItem(Item), OutStream);
573 end;
574 { -------------------------------------------------------------------------- }
575 procedure TAbCustomZipper.ZipFromStreamProc(Sender : TObject; Item : TAbArchiveItem;
576 OutStream, InStream : TStream);
577 begin
578 if Assigned(InStream) then
579 AbZipFromStream(TAbZipArchive(Sender), TAbZipItem(Item),
580 OutStream, InStream)
581 else
582 raise EAbZipNoInsertion.Create;
583 end;
584 { -------------------------------------------------------------------------- }
585 procedure TAbCustomZipper.DoArchiveSaveProgress(Sender : TObject;
586 Progress : Byte;
587 var Abort : Boolean);
588 begin
589 Abort := False;
590 if Assigned(FArchiveSaveProgressMeter) then
591 FArchiveSaveProgressMeter.DoProgress(Progress);
592 if Assigned(FOnArchiveSaveProgress) then
593 FOnArchiveSaveProgress(Self, Progress, Abort);
594 end;
595 { -------------------------------------------------------------------------- }
596 procedure TAbCustomZipper.Notification(Component: TComponent;
597 Operation: TOperation);
598 begin
599 inherited Notification(Component, Operation);
600 if (Operation = opRemove) then
601 if Assigned(ArchiveSaveProgressMeter) and Component.IsImplementorOf(ArchiveSaveProgressMeter) then
602 ArchiveSaveProgressMeter := nil
603 end;
604 { -------------------------------------------------------------------------- }
605 procedure TAbCustomZipper.ResetMeters;
606 begin
607 inherited ResetMeters;
608 if Assigned(FArchiveSaveProgressMeter) then
609 FArchiveSaveProgressMeter.Reset;
610 end;
611 { -------------------------------------------------------------------------- }
612
613 end.
614