-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZMFStream.pas
384 lines (340 loc) · 11.5 KB
/
ZMFStream.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
unit ZMFStream;
// ZMFStream.pas - basic in/out for zip files
(* ***************************************************************************
TZipMaster VCL originally by Chris Vleghert, Eric W. Engler.
Present Maintainers and Authors Roger Aelbrecht and Russell Peters.
Copyright (C) 1997-2002 Chris Vleghert and Eric W. Engler
Copyright (C) 1992-2008 Eric W. Engler
Copyright (C) 2009, 2010, 2011, 2012, 2013 Russell Peters and Roger Aelbrecht
Copyright (C) 2014 Russell Peters and Roger Aelbrecht
All rights reserved.
For the purposes of Copyright and this license "DelphiZip" is the current
authors, maintainers and developers of its code:
Russell Peters and Roger Aelbrecht.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* DelphiZip reserves the names "DelphiZip", "ZipMaster", "ZipBuilder",
"DelZip" and derivatives of those names for the use in or about this
code and neither those names nor the names of its authors or
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL DELPHIZIP, IT'S AUTHORS OR CONTRIBUTERS BE
LIABLE FOR ANYDIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
contact: problems AT delphizip DOT org
updates: http://www.delphizip.org
*************************************************************************** *)
// modified 2013-12-16
{$I '.\ZipVers.inc'}
interface
uses
{$IFDEF VERDXE2up}
System.Classes, WinApi.Windows, System.SysUtils,
{$ELSE}
Classes, Windows, SysUtils,
{$ENDIF}
ZMBody;
type
TZMFileStream = class(TStream)
private
FBody: TZMBody;
protected
function GetIsOpen: Boolean; virtual; abstract;
function GetLastWritten: Cardinal; virtual; abstract;
function GetPosition: Int64; virtual; abstract;
procedure SetPosition(const Value: Int64); virtual; abstract;
function GetRealFileName: string; virtual; abstract;
function GetIsTemp: Boolean; virtual; abstract;
procedure SetIsTemp(const Value: Boolean); virtual; abstract;
function GetOpenMode: Integer; virtual; abstract;
procedure SetOpenMode(const Value: integer); virtual; abstract;
public
constructor Create(MyOwner: TZMBody);
function FileDate: Cardinal; virtual; abstract;
procedure File_Close; virtual; abstract;
procedure File_Create(const TheName: string); virtual; abstract;
function File_GetDate: Cardinal; virtual; abstract;
procedure File_Open(const FileName: string; Mode: Word); virtual; abstract;
function GetFileInformation(var FileInfo
: _BY_HANDLE_FILE_INFORMATION): Boolean; virtual; abstract;
function IsEndOfFile: Boolean; virtual; abstract;
function LastWriteTime(var Last_write: TFileTime): Boolean; virtual; abstract;
{$IFNDEF VERD7up}
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload;
virtual; abstract;
{$ENDIF}
function SetEndOfFile: Boolean; virtual; abstract;
property Body: TZMBody read FBody;
property LastWritten: Cardinal read GetLastWritten;
property Position: Int64 read GetPosition write SetPosition;
property RealFileName: string read GetRealFileName;
property IsOpen: Boolean read GetIsOpen;
property IsTemp: Boolean read GetIsTemp write SetIsTemp;
property OpenMode: Integer read GetOpenMode write SetOpenMode;
end;
type
TZMSingleFileStream = class(TZMFileStream)
private
FHandle: Integer;
FIsTemp: Boolean;
FOpenMode: Integer;
FRealFileName: string;
protected
function GetIsOpen: Boolean; override;
function GetLastWritten: Cardinal; override;
function GetPosition: Int64; override;
procedure SetPosition(const Value: Int64); override;
function GetRealFileName: string; override;
function GetIsTemp: Boolean; override;
procedure SetIsTemp(const Value: Boolean); override;
function GetOpenMode: Integer; override;
procedure SetOpenMode(const Value: integer); override;
property Handle: Integer read FHandle;
public
constructor Create(MyOwner: TZMBody; const FileName: string; Mode: Word);
overload;
constructor Create(MyOwner: TZMBody); overload;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
function FileDate: Cardinal; override;
procedure File_Close; override;
procedure File_Create(const TheName: string); override;
function File_GetDate: Cardinal; override;
procedure File_Open(const FileName: string; Mode: Word); override;
function GetFileInformation(var FileInfo
: _BY_HANDLE_FILE_INFORMATION): Boolean; override;
function IsEndOfFile: Boolean; override;
function LastWriteTime(var Last_write: TFileTime): Boolean; override;
function Read(var Buffer; Len: Integer): Integer; override;
function Seek(Offset: Longint; Origin: Word): Longint; overload; override;
function Seek(const Offset: Int64; Origin: TSeekOrigin): Int64; overload; override;
function SetEndOfFile: Boolean; override;
function Write(const Buffer; Len: Integer): Integer; override;
end;
implementation
uses
ZMCore, ZMMsg, ZMWinFuncs, ZMUtils;
const
__UNIT__ = 18;
function ZM_Error(Line, Error: Integer): Integer;
begin
Result := -((__UNIT__ shl ZERR_UNIT_SHIFTS) + (Line shl ZERR_LINE_SHIFTS) or
AbsErr(Error));
end;
{ TZMSingleFileStream }
constructor TZMSingleFileStream.Create(MyOwner: TZMBody; const FileName: string;
Mode: Word);
begin
inherited Create(MyOwner);
FOpenMode := Mode;
FRealFileName := FileName;
end;
constructor TZMSingleFileStream.Create(MyOwner: TZMBody);
begin
inherited;
FOpenMode := -1;
FRealFileName := '';
end;
procedure TZMSingleFileStream.AfterConstruction;
begin
inherited;
FHandle := -1;
if OpenMode <> -1 then
begin
if OpenMode = FmCreate then
File_Create(FRealFileName)
else
File_Open(FRealFileName, OpenMode);
end;
end;
procedure TZMSingleFileStream.BeforeDestruction;
begin
File_Close;
if IsTemp and _Z_FileExists(RealFileName) then
begin
Body.TraceFmt('Deleting %s', [RealFileName], {_LINE_}201, __UNIT__);
File_Delete(RealFileName);
end;
inherited;
end;
function TZMSingleFileStream.FileDate: Cardinal;
begin
Result := Cardinal(-1);
if FHandle <> -1 then
Result := FileGetDate(Handle);
end;
procedure TZMSingleFileStream.File_Close;
var
Th: Integer;
begin
if FHandle <> -1 then
begin
Th := FHandle;
FHandle := -1;
FileClose(Th);
Body.TraceFmt('Closed %s', [RealFileName], {_LINE_}223, __UNIT__);
end;
end;
procedure TZMSingleFileStream.File_Create(const TheName: string);
begin
if Handle <> -1 then
File_Close;
FRealFileName := TheName;
if TheName <> '' then
begin
Body.TraceFmt('File_Create : %s', [TheName], {_LINE_}234, __UNIT__);
FHandle := _Z_FileCreate(TheName);
if FHandle = -1 then
Body.InformSysFmt('FileCreate Failed: %s', [TheName], {_LINE_}237,
__UNIT__);
FOpenMode := {$IFDEF VERDXE2up}System.{$ENDIF}SysUtils.FmOpenReadWrite;
end;
end;
function TZMSingleFileStream.File_GetDate: Cardinal;
begin
Result := FileGetDate(Handle);
end;
procedure TZMSingleFileStream.File_Open(const FileName: string; Mode: Word);
begin
if Handle <> -1 then
File_Close;
FRealFileName := FileName;
FHandle := _Z_FileOpen(RealFileName, Mode);
if Handle < 0 then
Body.InformSysFmt('FileOpen Failed: %s', [RealFileName],
{_LINE_}256, __UNIT__)
else
Body.TraceFmt('Opened %s', [RealFileName], {_LINE_}258, __UNIT__);
FOpenMode := Mode;
end;
function TZMSingleFileStream.GetFileInformation(var FileInfo
: _BY_HANDLE_FILE_INFORMATION): Boolean;
begin
Result := False;
if Handle <> -1 then
Result := GetFileInformationByHandle(Handle, FileInfo);
if not Result then
ZeroMemory(@FileInfo, Sizeof(_BY_HANDLE_FILE_INFORMATION));
end;
function TZMSingleFileStream.GetIsOpen: Boolean;
begin
Result := (Handle <> -1);
end;
function TZMSingleFileStream.GetIsTemp: Boolean;
begin
Result := FIsTemp;
end;
function TZMSingleFileStream.GetLastWritten: Cardinal;
var
Ft: TFileTime;
begin
Result := 0;
if (Handle <> -1) and LastWriteTime(Ft) then
Result := FileTimeToLocalDOSTime(Ft);
end;
function TZMSingleFileStream.GetOpenMode: Integer;
begin
Result := FOpenMode;
end;
function TZMSingleFileStream.GetPosition: Int64;
begin
if Handle <> -1 then
Result := FileSeek(FHandle, 0, SoFromCurrent)
else
Result := -1;
end;
function TZMSingleFileStream.GetRealFileName: string;
begin
Result := FRealFileName;
end;
// return true if end of segment
function TZMSingleFileStream.IsEndOfFile: Boolean;
var
Cposn: Int64;
begin
Result := True;
if Handle <> -1 then
begin
Cposn := Position;
Result := Cposn = FileSeek(Handle, 0, SoFromEnd);
if not Result then
Position := Cposn;
end;
end;
function TZMSingleFileStream.LastWriteTime(var Last_write: TFileTime): Boolean;
var
BHFInfo: TByHandleFileInformation;
begin
Result := False;
Last_write.DwLowDateTime := 0;
Last_write.DwHighDateTime := 0;
if Handle <> -1 then
begin
Result := GetFileInformationByHandle(Handle, BHFInfo);
if Result then
Last_write := BHFInfo.FtLastWriteTime;
end;
end;
function TZMSingleFileStream.Read(var Buffer; Len: Integer): Integer;
begin
Assert(Len >= 0, 'TZMSingleFileStream read len < 0');
Result := -1;
if Handle <> -1 then
Result := FileRead(Handle, Buffer, Len);
end;
function TZMSingleFileStream.Seek(const Offset: Int64; Origin: TSeekOrigin): Int64;
begin
Result := -1;
if Handle <> -1 then
Result := FileSeek(Handle, Offset, WORD(Origin));
end;
function TZMSingleFileStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
Result := Seek(Int64(Offset), TSeekOrigin(Origin));
end;
function TZMSingleFileStream.SetEndOfFile: Boolean;
begin
Result := False;
if Handle <> -1 then
Result := {$IFDEF VERDXE2up}WinApi.{$ENDIF}Windows.SetEndOfFile(Handle);
end;
procedure TZMSingleFileStream.SetIsTemp(const Value: Boolean);
begin
FIsTemp := Value;
end;
procedure TZMSingleFileStream.SetOpenMode(const Value: integer);
begin
FOpenMode := Value;
end;
procedure TZMSingleFileStream.SetPosition(const Value: Int64);
begin
if Handle <> -1 then
Seek(Value, TSeekOrigin(SoFromBeginning));
end;
function TZMSingleFileStream.Write(const Buffer; Len: Integer): Integer;
begin
Result := -1;
Assert(Len >= 0, 'TZMSingleFileStream write Len < 0');
if Handle <> -1 then
Result := FileWrite(Handle, Buffer, Len);
end;
{ TZMFileStream }
constructor TZMFileStream.Create(MyOwner: TZMBody);
begin
inherited Create;
FBody := MyOwner;
end;
end.