source: trunk/Packages/bgrabitmap/unzipperext.pas

Last change on this file was 2, checked in by chronos, 5 years ago
File size: 3.3 KB
Line 
1unit UnzipperExt;
2
3{$mode objfpc}{$H+}
4
5interface
6
7uses
8 Classes, SysUtils, zipper;
9
10type
11
12 { TUnzipperStreamUtf8 }
13
14 TUnzipperStreamUtf8 = class(TUnZipper)
15 private
16 FCustomOutputStream: TStream;
17 FCustomInputStream: TStream;
18 procedure SetInputStream(AValue: TStream);
19 protected
20 Procedure CustomOpenInput(Sender: TObject; var AStream: TStream);
21 procedure CustomCloseInput(Sender: TObject; var AStream: TStream);
22 procedure CustomCreateOutput(Sender : TObject; var AStream : TStream; {%H-}AItem : TFullZipFileEntry);
23 procedure CustomCloseOutput(Sender : TObject; var AStream : TStream; {%H-}AItem : TFullZipFileEntry);
24 public
25 function UnzipFileToStream(AFilename: string; AStream: TStream; ACaseSensitive: boolean= true): boolean;
26 function UnzipFileToString(AFilename:string): string;
27 constructor Create;
28 property InputStream: TStream read FCustomInputStream write SetInputStream;
29 end;
30
31implementation
32
33uses BGRAUTF8;
34
35{ TUnzipperStreamUtf8 }
36
37procedure TUnzipperStreamUtf8.SetInputStream(AValue: TStream);
38begin
39 if FCustomInputStream=AValue then Exit;
40 FCustomInputStream:=AValue;
41end;
42
43procedure TUnzipperStreamUtf8.CustomOpenInput(Sender: TObject; var AStream: TStream);
44begin
45 if Assigned(FCustomInputStream) then
46 AStream := FCustomInputStream
47 else
48 AStream := TFileStreamUTF8.Create(FileName, fmOpenRead or fmShareDenyWrite);
49end;
50
51procedure TUnzipperStreamUtf8.CustomCloseInput(Sender: TObject; var AStream: TStream);
52begin
53 if AStream = FCustomInputStream then
54 AStream := nil
55 else
56 FreeAndNil(AStream);
57end;
58
59procedure TUnzipperStreamUtf8.CustomCreateOutput(Sender: TObject;
60 var AStream: TStream; AItem: TFullZipFileEntry);
61begin
62 AStream := FCustomOutputStream;
63end;
64
65procedure TUnzipperStreamUtf8.CustomCloseOutput(Sender: TObject;
66 var AStream: TStream; AItem: TFullZipFileEntry);
67begin
68 AStream := nil;
69end;
70
71function TUnzipperStreamUtf8.UnzipFileToStream(AFilename: string; AStream: TStream;
72 ACaseSensitive: boolean): boolean;
73var
74 i: integer;
75 entryName: string;
76begin
77 OpenInput;
78 AFilename := StringReplace(AFilename,'/','\',[rfReplaceAll]);
79 Try
80 ReadZipDirectory;
81 for i := 0 to Entries.count-1 do
82 begin
83 entryName := Entries.FullEntries[i].ArchiveFileName;
84 entryName:= StringReplace(entryName,'/','\',[rfReplaceAll]);
85 if (entryName = AFilename) or
86 (not ACaseSensitive and (CompareText(entryName,AFilename)=0)) then
87 begin
88 OnCreateStream := @CustomCreateOutput;
89 OnDoneStream := @CustomCloseOutput;
90 FCustomOutputStream := AStream;
91 UnZipOneFile(Entries.FullEntries[i]);
92 OnCreateStream := nil;
93 OnDoneStream := nil;
94 FCustomOutputStream := nil;
95 result := true;
96 exit;
97 end;
98 end;
99 Finally
100 CloseInput;
101 end;
102 result := false;
103end;
104
105function TUnzipperStreamUtf8.UnzipFileToString(AFilename: string): string;
106var mem: TMemoryStream;
107begin
108 mem := TMemoryStream.Create;
109 try
110 UnzipFileToStream(AFilename,mem);
111 setlength(result,mem.Size);
112 mem.Position:= 0;
113 mem.Read(result[1], length(result));
114 finally
115 mem.Free;
116 end;
117end;
118
119constructor TUnzipperStreamUtf8.Create;
120begin
121 inherited Create;
122 OnOpenInputStream := @CustomOpenInput;
123 OnCloseInputStream:= @CustomCloseInput;
124end;
125
126
127end.
128
Note: See TracBrowser for help on using the repository browser.