Debian Codegraph @

plugins/wcx/zip/src/fparchive/abbrowse.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: AbBrowse.pas *}
28 {*********************************************************}
29 {* ABBREVIA: Base Browser Component *}
30 {*********************************************************}
31
32 unit AbBrowse;
33
34 {$I AbDefine.inc}
35
36 interface
37
38 uses
39 Classes,
40 AbBase,
41 AbUtils,
42 AbArcTyp;
43
44 type
45 IAbProgressMeter = interface
46 ['{4B766704-FD20-40BF-BA40-2EC2DD77B178}']
47 procedure DoProgress(Progress : Byte);
48 procedure Reset;
49 end;
50
51 TAbBaseBrowser = class(TAbBaseComponent)
52 public
53 FArchive : TAbArchive;
54 protected {private}
55 FSpanningThreshold : Longint;
56 FItemProgressMeter : IAbProgressMeter;
57 FArchiveProgressMeter : IAbProgressMeter;
58 FBaseDirectory : string;
59 FFileName : string;
60 FLogFile : string;
61 FLogging : Boolean;
62 FOnArchiveProgress : TAbArchiveProgressEvent;
63 FOnArchiveItemProgress : TAbArchiveItemProgressEvent;
64 FOnChange : TNotifyEvent;
65 FOnConfirmProcessItem : TAbArchiveItemConfirmEvent;
66 FOnLoad : TAbArchiveEvent;
67 FOnProcessItemFailure : TAbArchiveItemFailureEvent;
68 FOnRequestImage : TAbRequestImageEvent;
69 FTempDirectory : string;
70
71 { detected compression type }
72 FArchiveType : TAbArchiveType;
73 FForceType : Boolean;
74
75 protected {private methods}
76 function GetCount : Integer;
77 function GetItem(Value : Longint) : TAbArchiveItem;
78 function GetSpanned : Boolean;
79 function GetStatus : TAbArchiveStatus;
80 procedure ResetMeters; virtual;
81 procedure SetArchiveProgressMeter(const Value: IAbProgressMeter);
82 procedure SetCompressionType(const Value: TAbArchiveType);
83 procedure SetBaseDirectory(const Value : string);
84 procedure SetItemProgressMeter(const Value: IAbProgressMeter);
85 procedure SetSpanningThreshold(Value : Longint);
86 procedure SetLogFile(const Value : string);
87 procedure SetLogging(Value : Boolean);
88 procedure SetTempDirectory(const Value : string);
89
90 procedure Loaded; override;
91 procedure Notification(Component: TComponent;
92 Operation: TOperation); override;
93
94 protected {virtual methods}
95 procedure DoArchiveItemProgress(Sender : TObject;
96 Item : TAbArchiveItem;
97 Progress : Byte;
98 var Abort : Boolean); virtual;
99 procedure DoArchiveProgress(Sender : TObject;
100 Progress : Byte;
101 var Abort : Boolean); virtual;
102 procedure DoChange; virtual;
103 procedure DoConfirmProcessItem(Sender : TObject;
104 Item : TAbArchiveItem;
105 ProcessType : TAbProcessType;
106 var Confirm : Boolean); virtual;
107 procedure DoLoad(Sender : TObject); virtual;
108 procedure DoProcessItemFailure(Sender : TObject;
109 Item : TAbArchiveItem;
110 ProcessType : TAbProcessType;
111 ErrorClass : TAbErrorClass;
112 ErrorCode : Integer); virtual;
113 procedure SetOnRequestImage(Value : TAbRequestImageEvent); virtual;
114 procedure InitArchive; virtual;
115
116 {This method must be defined in descendent classes}
117 procedure SetFileName(const aFileName : string); virtual; abstract;
118
119
120 protected {properties}
121 property Archive : TAbArchive
122 read FArchive;
123 property ArchiveProgressMeter : IAbProgressMeter
124 read FArchiveProgressMeter
125 write SetArchiveProgressMeter;
126 property BaseDirectory : string
127 read FBaseDirectory
128 write SetBaseDirectory;
129 property FileName : string
130 read FFileName
131 write SetFileName;
132 property SpanningThreshold : Longint
133 read FSpanningThreshold
134 write SetSpanningThreshold
135 default 0;
136 property ItemProgressMeter : IAbProgressMeter
137 read FItemProgressMeter
138 write SetItemProgressMeter;
139 property LogFile : string
140 read FLogFile
141 write SetLogFile;
142 property Logging : Boolean
143 read FLogging
144 write SetLogging
145 default False;
146 property Spanned : Boolean
147 read GetSpanned;
148 property TempDirectory : string
149 read FTempDirectory
150 write SetTempDirectory;
151
152
153
154 protected {events}
155 property OnArchiveProgress : TAbArchiveProgressEvent
156 read FOnArchiveProgress
157 write FOnArchiveProgress;
158 property OnArchiveItemProgress : TAbArchiveItemProgressEvent
159 read FOnArchiveItemProgress
160 write FOnArchiveItemProgress;
161 property OnConfirmProcessItem : TAbArchiveItemConfirmEvent
162 read FOnConfirmProcessItem
163 write FOnConfirmProcessItem;
164 property OnProcessItemFailure : TAbArchiveItemFailureEvent
165 read FOnProcessItemFailure
166 write FOnProcessItemFailure;
167 property OnRequestImage : TAbRequestImageEvent
168 read FOnRequestImage
169 write SetOnRequestImage;
170
171 public {methods}
172 constructor Create(AOwner : TComponent); override;
173 destructor Destroy; override;
174
175 procedure ClearTags;
176 {Clear all tags from the archive}
177 function FindItem(aItem : TAbArchiveItem) : Integer;
178 function FindFile(const aFileName : string) : Integer;
179 procedure TagItems(const FileMask : string);
180 {tag all items that match the mask}
181 procedure UnTagItems(const FileMask : string);
182 {clear tags for all items that match the mask}
183 procedure CloseArchive;
184 {closes the archive by setting FileName to ''}
185 procedure OpenArchive(const aFileName : string);
186 {opens the archive}
187
188 public {properties}
189 property Count : Integer
190 read GetCount;
191 property Items[Index : Integer] : TAbArchiveItem
192 read GetItem; default;
193 property Status : TAbArchiveStatus
194 read GetStatus;
195
196 property ArchiveType : TAbArchiveType
197 read FArchiveType
198 write SetCompressionType
199 default atUnknown;
200
201 property ForceType : Boolean
202 read FForceType
203 write FForceType
204 default False;
205
206 public {events}
207 property OnChange : TNotifyEvent
208 read FOnChange
209 write FOnChange;
210 property OnLoad : TAbArchiveEvent
211 read FOnLoad
212 write FOnLoad;
213 end;
214
215 function AbDetermineArcType(const FN : string; AssertType : TAbArchiveType) : TAbArchiveType; overload;
216 function AbDetermineArcType(aStream: TStream) : TAbArchiveType; overload;
217
218 implementation
219
220 uses
221 SysUtils,
222 AbExcept,
223 {$IF DEFINED(ExtractCabSupport)}
224 AbCabTyp,
225 {$ENDIF}
226 AbZipTyp,
227 AbTarTyp,
228 AbGzTyp,
229 AbBzip2Typ,
230 AbLzmaTyp,
231 AbXzTyp,
232 AbZstdTyp,
233 DCOSUtils,
234 DCClassesUtf8;
235
236 { TAbBaseBrowser implementation ======================================= }
237
238 { -------------------------------------------------------------------------- }
239 constructor TAbBaseBrowser.Create(AOwner : TComponent);
240 begin
241 inherited Create(AOwner);
242 FLogFile := '';
243 FLogging := False;
244 FSpanningThreshold := 0;
245
246 FArchiveType := atUnknown;
247 FForceType := False;
248 end;
249 { -------------------------------------------------------------------------- }
250 destructor TAbBaseBrowser.Destroy;
251 begin
252 FArchive.Free;
253 FArchive := nil;
254 inherited Destroy;
255 end;
256 { -------------------------------------------------------------------------- }
257 procedure TAbBaseBrowser.ClearTags;
258 {Clear all tags from the archive}
259 begin
260 if Assigned(FArchive) then
261 FArchive.ClearTags
262 else
263 raise EAbNoArchive.Create;
264 end;
265 { -------------------------------------------------------------------------- }
266 procedure TAbBaseBrowser.CloseArchive;
267 {closes the archive by setting FileName to ''}
268 begin
269 if FFileName <> '' then
270 FileName := '';
271 end;
272 { -------------------------------------------------------------------------- }
273 procedure TAbBaseBrowser.DoArchiveItemProgress(Sender : TObject;
274 Item : TAbArchiveItem;
275 Progress : Byte;
276 var Abort : Boolean);
277 begin
278 Abort := False;
279 if Assigned(FItemProgressMeter) then
280 FItemProgressMeter.DoProgress(Progress);
281 if Assigned(FOnArchiveItemProgress) then
282 FOnArchiveItemProgress(Self, Item, Progress, Abort);
283 end;
284 { -------------------------------------------------------------------------- }
285 procedure TAbBaseBrowser.DoArchiveProgress(Sender : TObject;
286 Progress : Byte;
287 var Abort : Boolean);
288 begin
289 Abort := False;
290 if Assigned(FArchiveProgressMeter) then
291 FArchiveProgressMeter.DoProgress(Progress);
292 if Assigned(FOnArchiveProgress) then
293 FOnArchiveProgress(Self, Progress, Abort);
294 end;
295 { -------------------------------------------------------------------------- }
296 procedure TAbBaseBrowser.DoChange;
297 begin
298 if Assigned(FOnChange) then begin
299 FOnChange(Self);
300 end;
301 end;
302 { -------------------------------------------------------------------------- }
303 procedure TAbBaseBrowser.DoConfirmProcessItem(Sender : TObject;
304 Item : TAbArchiveItem;
305 ProcessType : TAbProcessType;
306 var Confirm : Boolean);
307 begin
308 Confirm := True;
309 if Assigned(FItemProgressMeter) then
310 FItemProgressMeter.Reset;
311 if Assigned(FOnConfirmProcessItem) then
312 FOnConfirmProcessItem(Self, Item, ProcessType, Confirm);
313 end;
314 { -------------------------------------------------------------------------- }
315 procedure TAbBaseBrowser.DoLoad(Sender : TObject);
316 begin
317 if Assigned(FOnLoad) then
318 FOnLoad(Self);
319 end;
320 { -------------------------------------------------------------------------- }
321 procedure TAbBaseBrowser.DoProcessItemFailure(Sender : TObject;
322 Item : TAbArchiveItem;
323 ProcessType : TAbProcessType;
324 ErrorClass : TAbErrorClass;
325 ErrorCode : Integer);
326 begin
327 if Assigned(FOnProcessItemFailure) then
328 FOnProcessItemFailure(Self, Item, ProcessType, ErrorClass, ErrorCode);
329 end;
330 { -------------------------------------------------------------------------- }
331 function TAbBaseBrowser.FindItem(aItem : TAbArchiveItem) : Integer;
332 begin
333 if Assigned(FArchive) then
334 Result := FArchive.FindItem(aItem)
335 else
336 Result := -1;
337 end;
338 { -------------------------------------------------------------------------- }
339 function TAbBaseBrowser.FindFile(const aFileName : string) : Integer;
340 begin
341 if Assigned(FArchive) then
342 Result := FArchive.FindFile(aFileName)
343 else
344 Result := -1;
345 end;
346 { -------------------------------------------------------------------------- }
347 function TAbBaseBrowser.GetSpanned : Boolean;
348 begin
349 if Assigned(FArchive) then
350 Result := FArchive.Spanned
351 else
352 Result := False;
353 end;
354 { -------------------------------------------------------------------------- }
355 function TAbBaseBrowser.GetStatus : TAbArchiveStatus;
356 begin
357 if Assigned(FArchive) then
358 Result := FArchive.Status
359 else
360 Result := asInvalid;
361 end;
362 { -------------------------------------------------------------------------- }
363 function TAbBaseBrowser.GetCount : Integer;
364 begin
365 if Assigned(FArchive) then
366 Result := FArchive.Count
367 else
368 Result := 0;
369 end;
370 { -------------------------------------------------------------------------- }
371 function TAbBaseBrowser.GetItem(Value : Longint) : TAbArchiveItem;
372 begin
373 if Assigned(FArchive) then
374 Result := FArchive.ItemList[Value]
375 else
376 raise EAbNoArchive.Create;
377 end;
378 { -------------------------------------------------------------------------- }
379 procedure TAbBaseBrowser.InitArchive;
380 begin
381 ResetMeters;
382 if Assigned(FArchive) then begin
383 {properties}
384 FArchive.SpanningThreshold := FSpanningThreshold;
385 FArchive.LogFile := FLogFile;
386 FArchive.Logging := FLogging;
387 FArchive.TempDirectory := FTempDirectory;
388 SetBaseDirectory(FBaseDirectory);
389 {events}
390 FArchive.OnArchiveProgress := DoArchiveProgress;
391 FArchive.OnArchiveItemProgress := DoArchiveItemProgress;
392 FArchive.OnConfirmProcessItem := DoConfirmProcessItem;
393 FArchive.OnLoad := DoLoad;
394 FArchive.OnProcessItemFailure := DoProcessItemFailure;
395 FArchive.OnRequestImage := FOnRequestImage;
396 end;
397 end;
398 { -------------------------------------------------------------------------- }
399 procedure TAbBaseBrowser.Loaded;
400 begin
401 inherited Loaded;
402 DoChange;
403 end;
404 { -------------------------------------------------------------------------- }
405 procedure TAbBaseBrowser.Notification(Component: TComponent;
406 Operation: TOperation);
407 begin
408 inherited Notification(Component, Operation);
409 if (Operation = opRemove) then begin
410 if Assigned(ItemProgressMeter) and Component.IsImplementorOf(ItemProgressMeter) then
411 ItemProgressMeter := nil;
412 if Assigned(ArchiveProgressMeter) and Component.IsImplementorOf(ArchiveProgressMeter) then
413 ArchiveProgressMeter := nil;
414 end;
415 end;
416 { -------------------------------------------------------------------------- }
417 procedure TAbBaseBrowser.OpenArchive(const aFileName : string);
418 {opens the archive}
419 begin
420 FileName := AFileName;
421 end;
422 { -------------------------------------------------------------------------- }
423 procedure TAbBaseBrowser.ResetMeters;
424 begin
425 if Assigned(FArchiveProgressMeter) then
426 FArchiveProgressMeter.Reset;
427 if Assigned(FItemProgressMeter) then
428 FItemProgressMeter.Reset;
429 end;
430 { -------------------------------------------------------------------------- }
431 procedure TAbBaseBrowser.SetBaseDirectory(const Value : string);
432 begin
433 if Assigned(FArchive) then begin
434 FArchive.BaseDirectory := Value;
435 FBaseDirectory := FArchive.BaseDirectory;
436 end else
437 FBaseDirectory := Value;
438 end;
439 { -------------------------------------------------------------------------- }
440 procedure TAbBaseBrowser.SetSpanningThreshold(Value : Longint);
441 begin
442 FSpanningThreshold := Value;
443 if Assigned(FArchive) then
444 FArchive.SpanningThreshold := Value;
445 end;
446 { -------------------------------------------------------------------------- }
447 procedure TAbBaseBrowser.SetLogFile(const Value : string);
448 begin
449 FLogFile := Value;
450 if (csDesigning in ComponentState) then
451 Exit;
452 if Assigned(FArchive) then
453 FArchive.LogFile := Value;
454 SetLogging(Value <> '');
455 end;
456 { -------------------------------------------------------------------------- }
457 procedure TAbBaseBrowser.SetLogging(Value : Boolean);
458 begin
459 FLogging := Value;
460 if (csDesigning in ComponentState) then
461 Exit;
462 if Assigned(FArchive) then
463 FArchive.Logging:= Value;
464 end;
465 { -------------------------------------------------------------------------- }
466 procedure TAbBaseBrowser.SetOnRequestImage(Value : TAbRequestImageEvent);
467 begin
468 FOnRequestImage := Value;
469 if Assigned(FArchive) then
470 FArchive.OnRequestImage := Value;
471 end;
472 { -------------------------------------------------------------------------- }
473 procedure TAbBaseBrowser.SetTempDirectory(const Value : string);
474 begin
475 FTempDirectory := Value;
476 if Assigned(FArchive) then
477 FArchive.TempDirectory := Value;
478 end;
479 { -------------------------------------------------------------------------- }
480 procedure TAbBaseBrowser.TagItems(const FileMask : string);
481 {tag all items that match the mask}
482 begin
483 if Assigned(FArchive) then
484 FArchive.TagItems(FileMask)
485 else
486 raise EAbNoArchive.Create;
487 end;
488 { -------------------------------------------------------------------------- }
489 procedure TAbBaseBrowser.UnTagItems(const FileMask : string);
490 {clear tags for all items that match the mask}
491 begin
492 if Assigned(FArchive) then
493 FArchive.UnTagItems(FileMask)
494 else
495 raise EAbNoArchive.Create;
496 end;
497 { -------------------------------------------------------------------------- }
498 procedure TAbBaseBrowser.SetCompressionType(const Value: TAbArchiveType);
499 begin
500 if not Assigned(FArchive) or (Status <> asInvalid) then
501 FArchiveType := Value
502 else
503 raise EAbArchiveBusy.Create;
504 end;
505 { -------------------------------------------------------------------------- }
506 procedure TAbBaseBrowser.SetArchiveProgressMeter(const Value: IAbProgressMeter);
507 begin
508 ReferenceInterface(FArchiveProgressMeter, opRemove);
509 FArchiveProgressMeter := Value;
510 ReferenceInterface(FArchiveProgressMeter, opInsert);
511 end;
512 { -------------------------------------------------------------------------- }
513 procedure TAbBaseBrowser.SetItemProgressMeter(const Value: IAbProgressMeter);
514 begin
515 ReferenceInterface(FItemProgressMeter, opRemove);
516 FItemProgressMeter := Value;
517 ReferenceInterface(FItemProgressMeter, opInsert);
518 end;
519 { -------------------------------------------------------------------------- }
520 function AbDetermineArcType(const FN : string; AssertType : TAbArchiveType) : TAbArchiveType;
521 var
522 Ext : string;
523 FS : TStream;
524 begin
525 Result := AssertType;
526 if Result = atUnknown then begin
527 { Guess archive type based on it's extension }
528 Ext := UpperCase(ExtractFileExt(FN));
529 if (Ext = '.ZIP') or (Ext = '.JAR') or (Ext = '.ZIPX') then
530 Result := atZip
531 else if (Ext = '.EXE') then
532 Result := atSelfExtZip
533 else if (Ext = '.TAR') then
534 Result := atTar
535 else if (Ext = '.GZ') then
536 Result := atGzip
537 else if (Ext = '.TGZ') then
538 Result := atGzippedTar
539 else if (Ext = '.CAB') then
540 Result := atCab
541 else if (Ext = '.BZ2') then
542 Result := atBzip2
543 else if (Ext = '.TBZ') then
544 Result := atBzippedTar
545 else if (Ext = '.XZ') then
546 Result := atXz
547 else if (Ext = '.TXZ') then
548 Result := atXzippedTar
549 else if (Ext = '.LZMA') then
550 Result := atLzma
551 else if (Ext = '.TLZ') then
552 Result := atLzmaTar
553 else if (Ext = '.ZST') then
554 Result := atZstd
555 else if (Ext = '.TZST') then
556 Result := atZstdTar;
557 end;
558 {$IF NOT DEFINED(ExtractCabSupport)}
559 if Result = atCab then
560 Result := atUnknown;
561 {$ENDIF}
562 if mbFileExists(FN) and (AbFileGetSize(FN) > 0) then begin
563 { If the file doesn't exist (or is empty) presume to make one, otherwise
564 guess or verify the contents }
565 try
566 FS := TFileStreamEx.Create(FN, fmOpenRead or fmShareDenyNone);
567 try
568 if Result <> atUnknown then begin
569 case Result of
570 atZip : begin
571 Result := VerifyZip(FS);
572 end;
573 atSelfExtZip : begin
574 Result := VerifySelfExtracting(FS);
575 end;
576 atTar : begin
577 Result := VerifyTar(FS);
578 end;
579 atGzip, atGzippedTar: begin
580 Result := VerifyGzip(FS);
581 end;
582 {$IF DEFINED(ExtractCabSupport)}
583 atCab : begin
584 Result := VerifyCab(FS);
585 end;
586 {$ENDIF}
587 atBzip2, atBzippedTar: begin
588 Result := VerifyBzip2(FS);
589 end;
590 atXz, atXzippedTar: begin
591 Result := VerifyXz(FS);
592 end;
593 atLzma, atLzmaTar: begin
594 Result := VerifyLzma(FS);
595 end;
596 atZstd, atZStdTar: begin
597 Result := VerifyZstd(FS);
598 end;
599 end;
600 end;
601 if Result = atUnknown then
602 Result := AbDetermineArcType(FS)
603 finally
604 FS.Free;
605 end;
606 except
607 // Skip
608 end;
609 end;
610 end;
611 { -------------------------------------------------------------------------- }
612 function AbDetermineArcType(aStream: TStream): TAbArchiveType;
613 begin
614 { VerifyZip returns true for self-extracting zips too, so test those first }
615 Result := VerifySelfExtracting(aStream);
616 { VerifyZip returns true for example when ZIP file is stored in a TAR archive, so test it first }
617 if Result = atUnknown then
618 Result := VerifyTar(aStream);
619 if Result = atUnknown then
620 Result := VerifyZip(aStream);
621 if Result = atUnknown then
622 Result := VerifyGzip(aStream);
623 if Result = atUnknown then
624 Result := VerifyBzip2(aStream);
625 {$IF DEFINED(ExtractCabSupport)}
626 if Result = atUnknown then
627 Result := VerifyCab(aStream);
628 {$ENDIF}
629 if Result = atUnknown then
630 Result := VerifyXz(aStream);
631 if Result = atUnknown then
632 Result := VerifyZstd(aStream);
633 end;
634 { -------------------------------------------------------------------------- }
635
636
637 end.