-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZLibExGZ.pas
1260 lines (967 loc) · 35.6 KB
/
ZLibExGZ.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{*************************************************************************************************
* ZLibExGZ.pas *
* *
* copyright (c) 2000-2013 base2 technologies *
* copyright (c) 1995-2002 Borland Software Corporation *
* *
* revision history *
* 2012.05.23 updated for delphi xe2 *
* added overloaded GZCompressFile *
* 2011.07.21 fixed routines to validate size before calling Move *
* 2010.01.27 updated for delphi 2010 *
* 2009.04.14 added overloaded string routines for AnsiString and *
* UnicodeString *
* removed deprecated Z*G routines *
* 2009.01.28 updated for delphi 2009 String (UnicodeString) *
* 2008.05.15 added TGZCompressionStream and TGZDecompressionStream *
* 2007.11.06 changed TGZTrailer.Crc from Cardinal to Longint *
* 2007.10.01 added GZDecompressStreamSize *
* fixed GZDecompressStream position handling *
* 2007.08.15 added GZCompressFile *
* 2007.07.18 fixed GZCompressStr filename and comment processing *
* 2007.03.18 modified naming convention for gzip routines GZ* *
* deprecated previous gzip routines Z*G *
* 2007.03.15 created separate unit for gzip routines/objects *
* added ZDecompressStreamG *
* added overloaded ZCompressStrG *
* added overloaded ZCompressStreamG *
* 2007.02.24 added PWord declaration for delphi 5- *
* 2006.08.10 added ZDecompressStrG *
* 2006.06.02 added DateTimeToUnix for delphi 5- *
* 2006.03.27 added ZCompressStreamG *
* 2006.03.24 added ZCompressStrG *
* *
* acknowledgments *
* ralf wenske *
* 2006.03.24 prototyping and assisting with ZCompressStrG and *
* ZCompressStreamG *
* *
* roman krupicka *
* 2006.06.02 pointing out the DateUtils unit and the DateTimeToUnix *
* function wasn't available prior to delphi 6 * *
* *
* marcin treffler *
* 2007.02.24 pointing out the missing PWord declaration for delphi 5 *
* *
* jean-jacques esquirol *
* 2007.07.18 pointing out the "result" address issue when processing *
* filename and comment flags/content in GZCompressStr *
* 2007.11.06 pointing out the type differences with TGZTrailer.Crc *
* (Cardinal) and ZCrc32 (Longint) *
* *
* graham wideman *
* 2007.10.01 beta testing GZDecompressStreamSize and pointing out the *
* position handling issue in GZDecompressStream *
*************************************************************************************************}
unit ZLibExGZ;
interface
{$I ZLibEx.inc}
uses
ZLibEx, Windows, SysUtils, Classes {$IFDEF Version6Plus}, DateUtils {$ENDIF};
type
{** TGZHeader *********************************************************************************}
PGZHeader = ^TGZHeader;
TGZHeader = packed record
Id1 : Byte;
Id2 : Byte;
Method : Byte;
Flags : Byte;
Time : Cardinal;
ExtraFlags: Byte;
OS : Byte;
end;
{** TGZTrailer ********************************************************************************}
PGZTrailer = ^TGZTrailer;
TGZTrailer = packed record
Crc : Longint;
Size: Cardinal;
end;
{** TGZCompressionStream **********************************************************************}
TGZCompressionStream = class(TZCompressionStream)
private
FFileName: AnsiString;
FComment : AnsiString;
FDateTime: TDateTime;
FTrailer : TGZTrailer;
public
constructor Create(dest: TStream; const fileName, comment: AnsiString;
dateTime: TDateTime); overload;
constructor Create(dest: TStream); overload;
destructor Destroy; override;
function Write(const buffer; count: Longint): Longint; override;
property FileName: AnsiString read FFileName;
property Comment : AnsiString read FComment;
property DateTime: TDateTime read FDateTime;
end;
{** TGZDecompressionStream ********************************************************************}
TGZDecompressionStream = class(TZDecompressionStream)
private
FFileName: AnsiString;
FComment : AnsiString;
FDateTime: TDateTime;
FCrc : Longint;
FSize : Cardinal;
public
constructor Create(source: TStream); reintroduce;
destructor Destroy; override;
function Read(var buffer; count: Longint): Longint; override;
property FileName: AnsiString read FFileName;
property Comment : AnsiString read FComment;
property DateTime: TDateTime read FDateTime;
end;
{** string routines *****************************************************************************}
{*************************************************************************************************
* GZCompressStr *
* *
* pre-conditions *
* s = uncompressed data string *
* fileName = filename *
* comment = comment *
* dateTime = date/time *
* *
* return *
* compressed data string in gzip format *
*************************************************************************************************}
function GZCompressStr(const s: AnsiString; const fileName,
comment: AnsiString; dateTime: TDateTime): RawByteString; overload;
procedure GZCompressString(var result: RawByteString; const s: AnsiString;
const fileName, comment: AnsiString; dateTime: TDateTime); overload;
{$ifdef Version6Plus}
procedure GZCompressString(var result: RawByteString; const s: UnicodeString;
const fileName, comment: AnsiString; dateTime: TDateTime); overload;
{$endif}
function GZCompressStr(const s: AnsiString): RawByteString; overload;
procedure GZCompressString(var result: RawByteString; const s: AnsiString);
overload;
{$ifdef Version6Plus}
procedure GZCompressString(var result: RawByteString; const s: UnicodeString);
overload;
{$endif}
{*************************************************************************************************
* GZDecompressStr *
* *
* pre-conditions *
* s = compressed data string in gzip format *
* *
* post-conditions *
* fileName = filename *
* comment = comment *
* dateTime = date/time *
* *
* return *
* uncompressed data string *
*************************************************************************************************}
function GZDecompressStr(const s: RawByteString; var fileName,
comment: AnsiString; var dateTime: TDateTime): AnsiString; overload;
procedure GZDecompressString(var result: AnsiString; const s: RawByteString;
var fileName, comment: AnsiString; var dateTime: TDateTime); overload;
{$ifdef Version6Plus}
procedure GZDecompressString(var result: UnicodeString;
const s: RawByteString; var fileName, comment: AnsiString;
var dateTime: TDateTime); overload;
{$endif}
function GZDecompressStr(const s: RawByteString): AnsiString; overload;
procedure GZDecompressString(var result: AnsiString; const s: RawByteString);
overload;
{$ifdef Version6Plus}
procedure GZDecompressString(var result: UnicodeString;
const s: RawByteString); overload;
{$endif}
{** stream routines *****************************************************************************}
procedure GZCompressStream(inStream, outStream: TStream; const fileName,
comment: AnsiString; dateTime: TDateTime); overload;
procedure GZCompressStream(inStream, outStream: TStream); overload;
procedure GZDecompressStream(inStream, outStream: TStream; var fileName,
comment: AnsiString; var dateTime: TDateTime); overload;
procedure GZDecompressStream(inStream, outStream: TStream); overload;
function GZDecompressStreamSize(inStream: TStream; var fileName,
comment: AnsiString; var dateTime: TDateTime): Longint; overload;
function GZDecompressStreamSize(inStream: TStream): Longint; overload;
{** file routines *******************************************************************************}
procedure GZCompressFile(const inFileName, outFileName: String;
const fileName, comment: AnsiString); overload;
procedure GZCompressFile(const inFileName, outFileName: String;
const comment: AnsiString); overload;
procedure GZCompressFile(const inFileName, outFileName: String); overload;
procedure GZDecompressFile(const inFileName, outFolder: String;
var comment: AnsiString); overload;
procedure GZDecompressFile(const inFileName, outFolder: String); overload;
implementation
uses
ZLibExApi;
const
GZ_ZLIB_WINDOWBITS = -15;
GZ_ZLIB_MEMLEVEL = 9;
GZ_ASCII_TEXT = $01;
GZ_HEADER_CRC = $02;
GZ_EXTRA_FIELD = $04;
GZ_FILENAME = $08;
GZ_COMMENT = $10;
GZ_RESERVED = $E0;
GZ_EXTRA_DEFAULT = 0;
GZ_EXTRA_MAX = 2;
GZ_EXTRA_FASTEST = 4;
SGZInvalid = 'Invalid GZStream operation!';
{$IFNDEF Version6Plus}
type
PWord = ^Word;
{$ENDIF}
{** DateTimeToUnix ******************************************************************************}
{$IFNDEF Version6Plus}
{ Days between TDateTime basis (12/31/1899) and Unix time_t basis (1/1/1970) }
const
UnixDateDelta = 25569;
function DateTimeToUnix(const AValue: TDateTime): Cardinal;
begin
Result := Round((AValue - UnixDateDelta) * SecsPerDay);
end;
function UnixToDateTime(const AValue: Cardinal): TDateTime;
begin
Result := AValue / SecsPerDay + UnixDateDelta;
end;
{$ENDIF}
{** FileAge *************************************************************************************}
{$ifndef Version2006Plus}
function FileAge(const fileName: String; var dateTime: TDateTime): Boolean;
begin
dateTime := FileDateToDateTime(SysUtils.FileAge(fileName));
result := True;
end;
{$endif}
{** string routines *****************************************************************************}
procedure GZInitializeCompressString(var result: RawByteString;
const fileName, comment: AnsiString; dateTime: TDateTime);
var
header: PGZHeader;
begin
SetLength(result, SizeOf(TGZHeader));
header := PGZHeader(@result[1]);
FillChar(header^, SizeOf(TGZHeader), 0);
header^.Id1 := $1F;
header^.Id2 := $8B;
header^.Method := Z_DEFLATED;
if dateTime <> 0 then header^.Time := DateTimeToUnix(dateTime);
header^.ExtraFlags := GZ_EXTRA_DEFAULT;
header^.OS := 0;
// build all flags first so "result" address doesn't change
header^.Flags := 0;
if Length(fileName) > 0 then
begin
header^.Flags := header^.Flags or GZ_FILENAME;
end;
if Length(comment) > 0 then
begin
header^.Flags := header^.Flags or GZ_COMMENT;
end;
// continue with content
if Length(fileName) > 0 then
begin
result := result + fileName + #$00;
end;
if Length(comment) > 0 then
begin
result := result + comment + #$00;
end;
end;
procedure GZFinalizeCompressString(var result: RawByteString; crc: Longint;
size: Integer);
var
trailer: PGZTrailer;
index : Integer;
begin
index := Length(result);
SetLength(result, index + SizeOf(TGZTrailer));
trailer := PGZTrailer(@result[index + 1]);
FillChar(trailer^, SizeOf(TGZTrailer), 0);
trailer^.Crc := crc;
trailer^.Size := size;
end;
procedure GZInitializeDecompressString(const s: RawByteString; var fileName,
comment: AnsiString; var dateTime: TDateTime; var index: Integer);
var
header : PGZHeader;
maxIndex: Integer;
endIndex: Integer;
size : Integer;
begin
if Length(s) < SizeOf(TGZHeader) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
header := PGZHeader(@s[1]);
if (header^.Id1 <> $1F) or (header^.Id2 <> $8B)
or (header^.Method <> Z_DEFLATED)
or ((header^.Flags and GZ_RESERVED) <> 0) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
if header^.Time <> 0 then dateTime := UnixToDateTime(header^.Time)
else dateTime := 0;
maxIndex := Length(s) - SizeOf(TGZTrailer);
index := SizeOf(TGZHeader) + 1;
if (header^.Flags and GZ_EXTRA_FIELD) <> 0 then
begin
if index <= (maxIndex - 1) then
begin
size := PWord(@s[index])^;
Inc(index, 2);
if (size >= 0) and ((index + size) <= maxIndex) then Inc(index, size)
else index := maxIndex + 1;
end
else index := maxIndex + 1;
end;
if (header^.Flags and GZ_FILENAME) <> 0 then
begin
endIndex := index;
while (endIndex <= maxIndex) and (s[endIndex] <> #$00) do Inc(endIndex);
SetLength(fileName, endIndex - index);
if (endIndex - index) > 0 then
begin
Move(s[index], fileName[1], endIndex - index);
end;
index := endIndex;
if index <= maxIndex then Inc(index);
end
else fileName := '';
if (header^.Flags and GZ_COMMENT) <> 0 then
begin
endIndex := index;
while (endIndex <= maxIndex) and (s[endIndex] <> #$00) do Inc(endIndex);
SetLength(comment, endIndex - index);
if (endIndex - index) > 0 then
begin
Move(s[index], comment[1], endIndex - index);
end;
index := endIndex;
if index <= maxIndex then Inc(index);
end
else comment := '';
if (header^.Flags and GZ_HEADER_CRC) <> 0 then
begin
if index <= (maxIndex - 1) then
begin
// todo: validate header crc
Inc(index,2);
end
else index := maxIndex + 1; // force eof
end;
if index > maxIndex then
begin
raise EZDecompressionError.Create(zeDataError);
end;
end;
procedure GZFinalizeDecompressString(const s: RawByteString; crc: Longint;
size: Integer);
var
trailer: PGZTrailer;
index : Integer;
begin
index := Length(s) - SizeOf(TGZTrailer) + 1;
trailer := PGZTrailer(@s[index]);
if (trailer^.Crc <> crc)
or (trailer^.Size <> Cardinal(size)) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
end;
function GZCompressStr(const s: AnsiString; const fileName,
comment: AnsiString; dateTime: TDateTime): RawByteString;
begin
GZCompressString(result, s, fileName, comment, dateTime);
end;
procedure GZCompressString(var result: RawByteString; const s: AnsiString;
const fileName, comment: AnsiString; dateTime: TDateTime);
var
buffer: RawByteString;
crc : Longint;
size : Integer;
begin
GZInitializeCompressString(result, fileName, comment, dateTime);
ZCompressString2(buffer, s, zcDefault, GZ_ZLIB_WINDOWBITS, GZ_ZLIB_MEMLEVEL,
zsDefault);
result := result + buffer;
size := Length(s);
crc := ZCrc32(0, s[1], size);
GZFinalizeCompressString(result, crc, size);
end;
{$ifdef Version6Plus}
procedure GZCompressString(var result: RawByteString; const s: UnicodeString;
const fileName, comment: AnsiString; dateTime: TDateTime);
var
buffer: RawByteString;
crc : Longint;
size : Integer;
begin
GZInitializeCompressString(result, fileName, comment, dateTime);
ZCompressString2(buffer, s, zcDefault, GZ_ZLIB_WINDOWBITS, GZ_ZLIB_MEMLEVEL,
zsDefault);
result := result + buffer;
size := Length(s) * SizeOf(UnicodeChar);
crc := ZCrc32(0, s[1], size);
GZFinalizeCompressString(result, crc, size);
end;
{$endif}
function GZCompressStr(const s: AnsiString): RawByteString;
begin
GZCompressString(result, s);
end;
procedure GZCompressString(var result: RawByteString; const s: AnsiString);
begin
GZCompressString(result, s, '', '', 0);
end;
{$ifdef Version6Plus}
procedure GZCompressString(var result: RawByteString; const s: UnicodeString);
begin
GZCompressString(result, s, '', '', 0);
end;
{$endif}
function GZDecompressStr(const s: RawByteString; var fileName,
comment: AnsiString; var dateTime: TDateTime): AnsiString;
begin
GZDecompressString(result, s, fileName, comment, dateTime);
end;
procedure GZDecompressString(var result: AnsiString; const s: RawByteString;
var fileName, comment: AnsiString; var dateTime: TDateTime);
var
index: Integer;
crc : Longint;
size : Integer;
begin
result := '';
GZInitializeDecompressString(s, fileName, comment, dateTime, index);
size := Length(s) - SizeOf(TGZTrailer) - index + 1;
ZDecompressString2(result, Copy(s, index, size), GZ_ZLIB_WINDOWBITS);
size := Length(result);
crc := ZCrc32(0, result[1], size);
GZFinalizeDecompressString(s, crc, size);
end;
{$ifdef Version6Plus}
procedure GZDecompressString(var result: UnicodeString;
const s: RawByteString; var fileName, comment: AnsiString;
var dateTime: TDateTime);
var
index: Integer;
crc : Longint;
size : Integer;
begin
result := '';
GZInitializeDecompressString(s, fileName, comment, dateTime, index);
size := Length(s) - SizeOf(TGZTrailer) - index + 1;
ZDecompressString2(result, Copy(s, index, size), GZ_ZLIB_WINDOWBITS);
size := Length(result) * SizeOf(UnicodeChar);
crc := ZCrc32(0, result[1], size);
GZFinalizeDecompressString(s, crc, size);
end;
{$endif}
function GZDecompressStr(const s: RawByteString): AnsiString;
begin
GZDecompressString(result, s);
end;
procedure GZDecompressString(var result: AnsiString; const s: RawByteString);
var
fileName: AnsiString;
comment : AnsiString;
dateTime: TDateTime;
begin
GZDecompressString(result, s, fileName, comment, dateTime);
end;
{$ifdef Version6Plus}
procedure GZDecompressString(var result: UnicodeString;
const s: RawByteString);
var
fileName: AnsiString;
comment : AnsiString;
dateTime: TDateTime;
begin
GZDecompressString(result, s, fileName, comment, dateTime);
end;
{$endif}
{** stream routines *****************************************************************************}
procedure GZCompressStream(inStream, outStream: TStream; const fileName,
comment: AnsiString; dateTime: TDateTime);
const
bufferSize = 32768;
var
header : TGZHeader;
trailer : TGZTrailer;
buffer : Array [0..bufferSize-1] of Byte;
count : Integer;
position : TStreamPos;
nullString: AnsiString;
begin
FillChar(header,SizeOf(TGZHeader),0);
header.Id1 := $1F;
header.Id2 := $8B;
header.Method := Z_DEFLATED;
if dateTime <> 0 then header.Time := DateTimeToUnix(dateTime);
header.ExtraFlags := GZ_EXTRA_DEFAULT;
header.OS := 0;
header.Flags := 0;
if Length(fileName) > 0 then header.Flags := header.Flags or GZ_FILENAME;
if Length(comment) > 0 then header.Flags := header.Flags or GZ_COMMENT;
FillChar(trailer, SizeOf(TGZTrailer), 0);
trailer.Crc := 0;
position := inStream.Position;
while inStream.Position < inStream.Size do
begin
count := inStream.Read(buffer[0],bufferSize);
trailer.Crc := ZCrc32(trailer.Crc,buffer[0],count);
end;
inStream.Position := position;
trailer.Size := inStream.Size - inStream.Position;
outStream.Write(header, SizeOf(TGZHeader));
if Length(filename) > 0 then
begin
nullString := fileName + #$00;
outStream.Write(nullString[1], Length(nullString));
end;
if Length(comment) > 0 then
begin
nullString := comment + #$00;
outStream.Write(nullString[1], Length(nullString));
end;
ZCompressStream2(inStream, outStream, zcDefault, GZ_ZLIB_WINDOWBITS,
GZ_ZLIB_MEMLEVEL, zsDefault);
outStream.Write(trailer, SizeOf(TGZTrailer));
end;
procedure GZCompressStream(inStream, outStream: TStream);
begin
GZCompressStream(inStream, outStream, '', '', 0);
end;
procedure GZDecompressStream(inStream, outStream: TStream; var fileName,
comment: AnsiString; var dateTime: TDateTime);
const
bufferSize = 32768;
var
header : TGZHeader;
trailer : TGZTrailer;
buffer : Array [0..bufferSize-1] of Byte;
count : Integer;
position : TStreamPos;
endPosition: TStreamPos;
size : Integer;
crc : Longint;
c : AnsiChar;
begin
if inStream.Read(header,SizeOf(TGZHeader)) <> SizeOf(TGZHeader) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
if (header.Id1 <> $1F)
or (header.Id2 <> $8B)
or (header.Method <> Z_DEFLATED)
or ((header.Flags and GZ_RESERVED) <> 0) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
if header.Time <> 0 then dateTime := UnixToDateTime(header.Time)
else dateTime := 0;
if (header.Flags and GZ_EXTRA_FIELD) <> 0 then
begin
if inStream.Read(size,SizeOf(Word)) <> SizeOf(Word) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
inStream.Position := inStream.Position + size;
end;
fileName := '';
if (header.Flags and GZ_FILENAME) <> 0 then
begin
c := ' ';
while (inStream.Position < inStream.Size) and (c <> #$00) do
begin
inStream.Read(c,1);
if c <> #$00 then fileName := fileName + c;
end;
end;
comment := '';
if (header.Flags and GZ_COMMENT) <> 0 then
begin
c := ' ';
while (inStream.Position < inStream.Size) and (c <> #$00) do
begin
inStream.Read(c, 1);
if c <> #$00 then comment := comment + c;
end;
end;
if (header.Flags and GZ_HEADER_CRC) <> 0 then
begin
// todo: validate header crc
inStream.Position := inStream.Position + SizeOf(Word);
end;
if inStream.Position >= inStream.Size then
begin
raise EZDecompressionError.Create(zeDataError);
end;
position := outStream.Position;
ZDecompressStream2(inStream,outStream,GZ_ZLIB_WINDOWBITS);
endPosition := outStream.Position;
if inStream.Read(trailer,SizeOf(TGZTrailer)) <> SizeOf(TGZTrailer) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
crc := 0;
outStream.Position := position;
while outStream.Position < endPosition do
begin
size := bufferSize;
if size > (endPosition - outStream.Position) then
begin
size := endPosition - outStream.Position;
end;
count := outStream.Read(buffer[0], size);
crc := ZCrc32(crc, buffer[0], count);
end;
if (trailer.Crc <> crc)
or (trailer.Size <> Cardinal(endPosition - position)) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
end;
procedure GZDecompressStream(inStream, outStream: TStream);
var
fileName: AnsiString;
comment : AnsiString;
dateTime: TDateTime;
begin
GZDecompressStream(inStream,outStream,fileName,comment,dateTime);
end;
type
TNullStream = class(TStream)
private
FSize : Int64;
FPosition: Int64;
public
function Read(var buffer; count: Longint): Longint; override;
function Write(const buffer; count: Longint): Longint; override;
function Seek(offset: Longint; origin: Word): Longint; override;
end;
function TNullStream.Read(var buffer; count: Longint): Longint;
begin
result := 0;
if (FPosition >= 0) and (FPosition < FSize) and (count >= 0) then
begin
result := FSize - FPosition;
if result > count then result := count;
Inc(FPosition, result);
end;
end;
function TNullStream.Write(const buffer; count: Longint): Longint;
begin
result := count;
FPosition := FPosition + count;
if FPosition > FSize then FSize := FPosition;
end;
function TNullStream.Seek(offset: Longint; origin: Word): Longint;
begin
case origin of
soFromBeginning: FPosition := offset;
soFromCurrent : Inc(FPosition, offset);
soFromEnd : FPosition := FSize + offset;
end;
result := FPosition;
end;
function GZDecompressStreamSize(inStream: TStream; var fileName,
comment: AnsiString; var dateTime: TDateTime): Longint;
const
bufferSize = 32768;
var
outStream: TNullStream;
header : TGZHeader;
trailer : TGZTrailer;
position : TStreamPos;
size : Integer;
c : AnsiChar;
begin
result := 0;
position := inStream.Position;
try
if inStream.Read(header,SizeOf(TGZHeader)) <> SizeOf(TGZHeader) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
if (header.Id1 <> $1F)
or (header.Id2 <> $8B)
or (header.Method <> Z_DEFLATED)
or ((header.Flags and GZ_RESERVED) <> 0) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
if header.Time <> 0 then dateTime := UnixToDateTime(header.Time)
else dateTime := 0;
if (header.Flags and GZ_EXTRA_FIELD) <> 0 then
begin
if inStream.Read(size,SizeOf(Word)) <> SizeOf(Word) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
inStream.Position := inStream.Position + size;
end;
fileName := '';
if (header.Flags and GZ_FILENAME) <> 0 then
begin
c := ' ';
while (inStream.Position < inStream.Size) and (c <> #$00) do
begin
inStream.Read(c,1);
if c <> #$00 then fileName := fileName + c;
end;
end;
comment := '';
if (header.Flags and GZ_COMMENT) <> 0 then
begin
c := ' ';
while (inStream.Position < inStream.Size) and (c <> #$00) do
begin
inStream.Read(c,1);
if c <> #$00 then comment := comment + c;
end;
end;
if (header.Flags and GZ_HEADER_CRC) <> 0 then
begin
// todo: validate header crc
inStream.Position := inStream.Position + SizeOf(Word);
end;
if inStream.Position >= inStream.Size then
begin
raise EZDecompressionError.Create(zeDataError);
end;
outStream := TNullStream.Create;
try
ZDecompressStream2(inStream,outStream,GZ_ZLIB_WINDOWBITS);
result := outStream.Size;
finally
outStream.Free;
end;
if inStream.Read(trailer,SizeOf(TGZTrailer)) <> SizeOf(TGZTrailer) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
if trailer.Size <> Cardinal(result) then
begin
raise EZDecompressionError.Create(zeDataError);
end;
finally
inStream.Position := position;
end;
end;
function GZDecompressStreamSize(inStream: TStream): Longint;
var
fileName: AnsiString;
comment : AnsiString;
dateTime: TDateTime;
begin
result := GZDecompressStreamSize(inStream,fileName,comment,dateTime);
end;
{** file routines *******************************************************************************}
procedure GZCompressFile(const inFileName, outFileName: String;
const fileName, comment: AnsiString);
var
inStream : TFileStream;
outStream: TFileStream;
dateTime : TDateTime;
begin
inStream := TFileStream.Create(String(inFileName), fmOpenRead or fmShareDenyNone);
try