-
Notifications
You must be signed in to change notification settings - Fork 1
/
unitfilezip.pas
240 lines (205 loc) · 6.88 KB
/
unitfilezip.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
unit UnitFileZip;
// Copyright 2022-2024 Zoran Vučenović
// SPDX-License-Identifier: Apache-2.0
{$mode ObjFPC}{$H+}
{$i zxinc.inc}
interface
uses
Classes, SysUtils, Types, UnitFormChooseString, Zipper;
type
ENoSpectrumFilesInZip = class(Exception);
TFileUnzipper = class(TObject)
strict private
InStream: TStream;
OutStream: TStream;
procedure DoOpenInputStream(Sender: TObject; var AStream: TStream);
procedure DoCloseInputStream(Sender: TObject; var AStream: TStream);
procedure DoCreateOutZipStream(Sender : TObject; var AStream : TStream; {%H-}AItem : TFullZipFileEntry);
procedure DoDoneOutZipStream(Sender : TObject; var AStream : TStream; {%H-}AItem : TFullZipFileEntry);
class function GetFileFromZipStream(const AZipStream: TStream; const Extensions: array of String;
out AStream: TStream; out FileNameInZip: String): Boolean; static;
public
class function GetFileFromZipFile(const ZipFileName: String; const Extensions: array of String;
out AStream: TStream; out FileNameInZip: String): Boolean; static;
end;
implementation
{ TFileUnzipper }
procedure TFileUnzipper.DoOpenInputStream(Sender: TObject; var AStream: TStream
);
begin
AStream := InStream;
if Assigned(InStream) then
InStream.Position := 0;
end;
procedure TFileUnzipper.DoCloseInputStream(Sender: TObject; var AStream: TStream
);
begin
AStream := nil;
end;
procedure TFileUnzipper.DoCreateOutZipStream(Sender: TObject;
var AStream: TStream; AItem: TFullZipFileEntry);
begin
OutStream := TMemoryStream.Create;
OutStream.Position := 0;
AStream := OutStream;
end;
procedure TFileUnzipper.DoDoneOutZipStream(Sender: TObject;
var AStream: TStream; AItem: TFullZipFileEntry);
begin
if Assigned(AStream) then
AStream.Position := 0;
end;
class function TFileUnzipper.GetFileFromZipStream(const AZipStream: TStream;
const Extensions: array of String; out AStream: TStream; out
FileNameInZip: String): Boolean;
const
CaptionText: AnsiString = 'Choose a file';
MessageText: AnsiString =
'The chosen zip file contains %d file entries which might be Spectrum files'
+ '. Please choose one:'
;
var
UnZ: TUnZipper;
I, II, LE, N, L, LL, LLL: Integer;
Entry: TFullZipFileEntry;
Extension: AnsiString;
SL: TStringList;
FiUnz: TFileUnzipper;
ExtFound: Boolean;
S, S2: AnsiString;
SA: TStringDynArray;
begin
Result := False;
AStream := nil;
FileNameInZip := '';
if Assigned(AZipStream) then begin
LE := Length(Extensions);
if LE > 0 then begin
FiUnz := TFileUnzipper.Create;
try
UnZ := TUnZipper.Create;
try
UnZ.FileName := '';
FiUnz.InStream := AZipStream;
UnZ.OnOpenInputStream := @(FiUnz.DoOpenInputStream);
UnZ.OnCloseInputStream := @(FiUnz.DoCloseInputStream);
UnZ.Examine;
LL := UnZ.Entries.Count;
if LL <= 144 then
L := LL
else
L := 144;
SetLength(SA{%H-}, L);
L := 0;
I := 0;
while I < LL do begin
Entry := UnZ.Entries.FullEntries[I];
if not (Entry.IsDirectory or Entry.IsLink) then begin
S2 := Entry.ArchiveFileName;
Extension := ExtractFileExt(S2);
ExtFound := False;
II := 0;
while (not ExtFound) and (II < LE) do begin
S := Trim(Extensions[II]);
if Length(S) > 0 then begin
if not S.StartsWith(ExtensionSeparator, True) then
S := ExtensionSeparator + S;
ExtFound := AnsiCompareText(Extension, S) = 0;
end;
Inc(II);
end;
if ExtFound then begin
if L >= Length(SA) then begin
LLL := (L * 7) div 5;
if LLL > LL then
LLL := LL;
SetLength(SA, LLL);
end;
SA[L] := S2;
Inc(L);
end;
end;
Inc(I);
end;
SetLength(SA, L);
if L <= 0 then
raise ENoSpectrumFilesInZip.Create('No Spectrum files in zip.');
if UnitFormChooseString.TFormChooseString.ShowFormChooseString(
SA, CaptionText, Format(MessageText, [L]), N)
then begin
if N < 0 then
Result := True // user cancelled
else begin
FileNameInZip := SA[N];
SetLength(SA, 0);
SL := TStringList.Create;
try
SL.Append(FileNameInZip);
FiUnz.OutStream := nil;
try
UnZ.OnCreateStream := @(FiUnz.DoCreateOutZipStream);
UnZ.OnDoneStream := @(FiUnz.DoDoneOutZipStream);
UnZ.UnZipFiles(SL);
if Assigned(FiUnz.OutStream) then begin
if FiUnz.OutStream.Size > 0 then begin
Extension := ExtractFileExt(FileNameInZip);
if AnsiCompareText(Extension, ExtensionSeparator + 'zip') = 0 then begin
if GetFileFromZipStream(FiUnz.OutStream, Extensions, AStream, S) then begin
FileNameInZip := IncludeTrailingPathDelimiter(FileNameInZip) + S;
Result := True;
end else
AStream := nil;
end else begin
AStream := FiUnz.OutStream;
FiUnz.OutStream := nil;
Result := True;
end;
end;
end;
finally
FiUnz.OutStream.Free;
end;
finally
SL.Free;
end;
end;
end;
finally
UnZ.Free;
end;
finally
FiUnz.Free;
end;
end;
end;
end;
class function TFileUnzipper.GetFileFromZipFile(const ZipFileName: String;
const Extensions: array of String; out AStream: TStream; out
FileNameInZip: String): Boolean;
var
FileStream: TStream;
ZipStream: TMemoryStream;
begin
Result := False;
AStream := nil;
FileStream := TFileStream.Create(ZipFileName, fmOpenRead or fmShareDenyWrite);
try
// Create auxiliary in-memory stream, instead of passing the file stream
// directly, so that we can release file locks immediately.
ZipStream := TMemoryStream.Create;
try
ZipStream.Size := FileStream.Size;
FileStream.Position := 0;
ZipStream.Position := 0;
if FileStream.Read(ZipStream.Memory^, ZipStream.Size) = ZipStream.Size then begin
FreeAndNil(FileStream); // release file locks immediately
Result := GetFileFromZipStream(ZipStream, Extensions, AStream, FileNameInZip);
end;
finally
ZipStream.Free;
end;
finally
FileStream.Free;
end;
end;
end.