-
Notifications
You must be signed in to change notification settings - Fork 3
/
ZMZipWriter.pas
2386 lines (2246 loc) · 63 KB
/
ZMZipWriter.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
unit ZMZipWriter;
// ZMZipWriter.pas - Handles basic writing of a Zip file
(* ***************************************************************************
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-05
{$INCLUDE '.\ZipVers.inc'}
interface
uses
{$IFDEF VERDXE2up}
System.Classes, WinApi.Windows,
{$ELSE}
Classes, Windows, {$IFNDEF VERD7up}ZMCompat, {$ENDIF}
{$ENDIF}
ZipMstr, ZMZipDirectory, ZMZipReader, ZMZipBase, ZMStructs;
type
TZipWrites = (ZwDefault, ZwSingle, ZwMultiple);
type
THowToEnc = (HteOEM, HteAnsi, HteUTF8);
const
ZsbRenamed = $80000; // write to different name
ZsbVChanged = $100000; // variable length field details have changed
// zsbExtName = $200000; // filename has extended characters
// zsbExtCmnt = $400000; // filename has extended characters
ZsbDuplicate = $800000; // kept duplicate
ZsbResolved = $1000000; // duplicate resolved
ZsbIsDup = $2000000; // duplicate added to list
{ .$DEFINE DEBUG_LISTS }
type
TZMEntryWriter = class;
TZMZipWriter = class(TZMZipReader)
private
ExtEntries: TZMEntryBase;
FDuplicates: TList;
FEntryCount: Integer;
FFirst: Integer;
FNewEntries: TZMEntryWriter;
FNewEntryCount: Integer;
FNewEntryTail: TZMEntryWriter;
FNoAppend: Boolean;
FOpenRet: Integer;
FOpenRet1: Integer;
FShowAll: Boolean;
FToDoList: TList;
RefTime: string;
WBuf: array of Byte;
function CommitEntries: Int64;
function CommitPreamble: Integer;
function CommitSum(var TotalProcess: Int64; var Latest: Cardinal;
MarkLatest: Boolean): Cardinal;
function DefaultDupAction: TZMDupResolutions;
function MakeDupedName(const Name: string; TryIndex: Integer): string;
function ResolveDuplicates: Integer;
function StrToHeader(const AString: string; How: THowToEnc): TZMRawBytes;
function StrTo_UTF8(const AString: string): UTF8String;
protected
procedure AddExtEntry(Entry: TZMEntryWriter);
procedure AppendNewEntry(AnEntry: TZMEntryWriter);
function BeforeCommit: Integer; virtual;
function CalcSizes(var NoEntries: Integer; var ToProcess: Int64;
var CenSize: Cardinal): Integer;
function CommitCentral: Int64;
function CommitRec(Rec: TZMEntryWriter): Int64; virtual;
function EOCSize(Is64: Boolean): Cardinal;
function FixHeaderNames: Integer; virtual;
function FixVariableFields(Entry: TZMEntryWriter; DupCnt: Integer): Integer;
procedure HailSuccesses;
procedure MarkDirty;
function Preprocess: Integer;
procedure RemoveExtEntry(Entry: TZMEntryBase);
function ResolveDupEntry(Entry, Dup: TZMEntryWriter; var NewName: string)
: TZMDupResolutions;
function SafeHeaderName(const IntName: string): string;
function UpdateCentralEntries: Integer;
function WriteCentral: Integer;
property EntryCount: Integer read FEntryCount write FEntryCount;
property NoAppend: Boolean read FNoAppend write FNoAppend;
public
function AffixEntry(AnEntry: TZMEntryWriter; NoDup: Boolean): Boolean;
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
function Commit(MarkLatest: Boolean): Integer;
function CommitAppend(Last: Integer; MarkLatest: Boolean): Integer;
function FindDuplicates: Integer;
function PrepareWrite(Typ: TZipWrites): Boolean;
function WBuffer(Size: Integer): PByte;
property Duplicates: TList read FDuplicates;
property First: Integer read FFirst;
property OpenRet: Integer read FOpenRet write FOpenRet;
property OpenRet1: Integer read FOpenRet1 write FOpenRet1;
property ShowAll: Boolean read FShowAll write FShowAll;
property ToDoList: TList read FToDoList;
end;
TZMEntryWriter = class(TZMEntryBase)
private
FAuxInfo: TObject;
FCrypt: Boolean;
FExtraField: TZMRawBytes;
FFileComment: string;
FFileName: string;
FLink: TZMEntryBase;
FLocalData: TZMRawBytes;
FMyExtraInfo: TObject;
FStartOnDisk: Cardinal;
FTempVar: Integer;
FVariables: TZMRawBytes;
F_FileComment: TZMRawBytes;
F_FileName: TZMRawBytes;
Header: TZipCentralHeader;
_CSize: Int64;
_DiskStt: Cardinal;
_RelOfs: Int64;
_USize: Int64;
procedure SetCompressedSize(const Value: Int64);
procedure SetCompressionMethod(const Value: Word);
procedure SetCRC32(const Value: Cardinal);
procedure SetDateTime(const Value: Cardinal);
procedure SetEncrypted(const Value: Boolean);
procedure SetExtFileAttrib(const Value: Longword);
procedure SetExtraData(Tag: Word; const Data: TZMRawBytes);
procedure SetExtraField(const Value: TZMRawBytes);
procedure SetFileComment(const Value: string);
procedure SetFileName(const Value: string);
procedure SetFlag(const Value: Word);
procedure SetIntFileAttrib(const Value: Word);
procedure SetModifDateTime(const Value: Longword);
procedure SetRelOffLocalHdr(const Value: Int64);
procedure SetStartOnDisk(const Value: Cardinal);
procedure SetUncompressedSize(const Value: Int64);
procedure SetVersionMadeBy(const Value: Word);
procedure SetVersionNeeded(const Value: Word);
function StripDrive(const FName: string; NoPath: Boolean): string;
protected
procedure FixMinimumVers(IsZ64: Boolean);
function GetCompressedSize: Int64; override;
function GetCompressionMethod: Word; override;
function GetCRC32: Cardinal; override;
function GetDateTime: Cardinal; override;
function GetEncrypted: Boolean; override;
function GetExtFileAttrib: Longword; override;
function GetExtraData(Tag: Word): TZMRawBytes; override;
function GetExtraField: TZMRawBytes; override;
function GetExtraFieldLength: Word; override;
function GetFileComment: string; override;
function GetFileCommentLen: Word; override;
function GetFileName: string; override;
function GetFileNameLen: Word; override;
function GetFlag: Word; override;
function GetIntFileAttrib: Word; override;
function GetIsEncoded: TZMEncodingOpts; override;
function GetModifDateTime: Longword; override;
function GetRelOffLocalHdr: Int64; override;
function GetStartOnDisk: Cardinal; override;
function GetTitle: string; override;
function GetUncompressedSize: Int64; override;
function GetVersionMadeBy: Word; override;
function GetVersionNeeded: Word; override;
function Get_CompressedSize: Cardinal; override;
function Get_FileComment: TZMRawBytes; override;
function Get_FileName: TZMRawBytes; override;
function Get_RelOffLocalHdr: Cardinal; override;
function Get_StartOnDisk: Word; override;
function Get_UncompressedSize: Cardinal; override;
procedure PrepareLocalData;
// 1 returns bytes written, <0 _ error
function Process: Int64; virtual; abstract;
function ProcessSize: Int64; virtual; abstract;
function WriteAsLocal1(Stamp, Crc: Cardinal): Integer;
function WriteDataDesc: Integer;
// 1 Auxillary information not owned by record
property AuxInfo: TObject read FAuxInfo write FAuxInfo;
property LocalData: TZMRawBytes read FLocalData write FLocalData;
// 1 Extra info owned by record
property MyExtraInfo: TObject read FMyExtraInfo write FMyExtraInfo;
public
constructor Create(TheOwner: TZMZipWriter);
procedure AssignFrom(const ZRec: TZMEntryBase); override;
procedure BeforeDestruction; override;
function ChangeName(const NewName: string): Integer;
procedure ClearCachedName; override;
function LocalSize: Cardinal;
function ToIntForm(const ExtName: string; var IntName: string): Integer;
function Write: Integer;
property CompressedSize: Int64 read GetCompressedSize
write SetCompressedSize;
property CompressionMethod: Word read GetCompressionMethod
write SetCompressionMethod;
property CRC32: Cardinal read GetCRC32 write SetCRC32;
property DateTime: Cardinal read GetDateTime write SetDateTime;
property Encoded: TZMEncodingOpts read GetEncoded;
property Encrypted: Boolean read GetEncrypted write SetEncrypted;
property ExtFileAttrib: Longword read GetExtFileAttrib
write SetExtFileAttrib;
property ExtraData[Tag: Word]: TZMRawBytes read GetExtraData
write SetExtraData;
property ExtraField: TZMRawBytes read GetExtraField write SetExtraField;
property ExtraFieldLength: Word read GetExtraFieldLength;
property FileComment: string read GetFileComment write SetFileComment;
property FileCommentLen: Word read GetFileCommentLen;
property FileName: string read GetFileName write SetFileName;
property FileNameLen: Word read GetFileNameLen;
property Flag: Word read GetFlag write SetFlag;
property IntFileAttrib: Word read GetIntFileAttrib write SetIntFileAttrib;
property IsEncoded: TZMEncodingOpts read GetIsEncoded write SetIsEncoded;
property Link: TZMEntryBase read FLink write FLink;
property ModifDateTime: Longword read GetModifDateTime
write SetModifDateTime;
property RelOffLocalHdr: Int64 read GetRelOffLocalHdr
write SetRelOffLocalHdr;
property StartOnDisk: Cardinal read GetStartOnDisk write SetStartOnDisk;
property TempVar: Integer read FTempVar write FTempVar;
property UncompressedSize: Int64 read GetUncompressedSize
write SetUncompressedSize;
property VersionMadeBy: Word read GetVersionMadeBy write SetVersionMadeBy;
property VersionNeeded: Word read GetVersionNeeded write SetVersionNeeded;
property _CompressedSize: Cardinal read Get_CompressedSize;
property _FileComment: TZMRawBytes read F_FileComment write F_FileComment;
property _FileName: TZMRawBytes read F_FileName write F_FileName;
property _RelOffLocalHdr: Cardinal read Get_RelOffLocalHdr;
property _StartOnDisk: Word read Get_StartOnDisk;
property _UncompressedSize: Cardinal read Get_UncompressedSize;
end;
type
TZMEntryCopier = class(TZMEntryWriter)
public
procedure AssignFrom(const ARec: TZMEntryBase); override;
function Process: Int64; override;
function ProcessSize: Int64; override;
end;
type
TZMZipCopier = class(TZMZipWriter)
public
function AffixZippedFile(Rec: TZMEntryBase): TZMEntryCopier;
function AffixZippedFiles(Src: TZMZipReader; All: Boolean): Integer;
function WriteFile(InZip: TZMZipReader; All: Boolean): Int64;
end;
implementation
uses
{$IFDEF VERDXE2up}
System.SysUtils, System.TypInfo,
{$ELSE}
SysUtils, TypInfo,
{$ENDIF}
ZMBody, ZMMsg, ZMUtils, ZMUTF8, ZMMisc, ZMZipEOC, ZMHandler, ZMCRC, ZMCore;
const
__UNIT__ = 47;
const
AllSpec: string = '*.*';
AnySpec: string = '*';
const
MAX_BYTE = 255;
MaxDupAsk = 5; // maximum number of tries to manually rename duplicate
type
Txdat64 = packed record
Tag: Word;
Siz: Word;
Vals: array [0 .. 4] of Int64; // last only cardinal
end;
type
TZMEntryExt = class(TZMEntryCopier)
protected
function GetHTFileName: string; override;
public
function HTIsSame(const AnEntry: TZMEntryBase): Boolean; override;
function HTIsSameStr(StrHash: Cardinal; const Str: string)
: Boolean; override;
function Process: Int64; override;
function ProcessSize: Int64; override;
end;
function ZM_Error(Line, Error: Integer): Integer;
begin
Result := -((__UNIT__ shl ZERR_UNIT_SHIFTS) + (Line shl ZERR_LINE_SHIFTS) or
AbsErr(Error));
end;
// make safe version of external comment
function SafeComment(const Xcomment: string): string;
var
C: Char;
I: Integer;
begin
if StrHasExt(Xcomment) then
Result := StrToOEM(Xcomment)
else
Result := Xcomment;
for I := 1 to Length(Result) do
begin
C := Result[I];
if (C < ' ') or (C > #126) then
Result[I] := '_';
end;
end;
constructor TZMEntryWriter.Create(TheOwner: TZMZipWriter);
begin
inherited Create(TheOwner);
end;
procedure TZMEntryWriter.AssignFrom(const ZRec: TZMEntryBase);
var
OldFile: TZMZipDirectory;
OldHTNext: TZMEntryBase;
OldNext: TZMEntryBase;
begin
OldHTNext := HTNext;
OldNext := Next;
OldFile := MyFile;
inherited;
if ZRec <> Self then
begin
CompressedSize := ZRec.CompressedSize;
CompressionMethod := ZRec.CompressionMethod;
CRC32 := ZRec.CRC32;
ExtFileAttrib := ZRec.ExtFileAttrib;
ExtraField := ZRec.ExtraField;
FileComment := ZRec.FileComment;
FileName := ZRec.FileName;
Flag := ZRec.Flag;
_FileComment := ZRec._FileComment;
_FileName := ZRec._FileName;
IntFileAttrib := ZRec.IntFileAttrib;
ModifDateTime := ZRec.ModifDateTime;
RelOffLocalHdr := ZRec.RelOffLocalHdr;
StartOnDisk := ZRec.StartOnDisk;
UncompressedSize := ZRec.UncompressedSize;
VersionNeeded := ZRec.VersionNeeded;
VersionMadeBy := ZRec.VersionMadeBy;
HTNext := OldHTNext; // restore original values
Next := OldNext;
MyFile := OldFile;
ClearStatusBit(ZsbHashed or ZsbRenamed or ZsbVChanged);
end;
end;
procedure TZMEntryWriter.BeforeDestruction;
begin
inherited;
end;
function TZMEntryWriter.ChangeName(const NewName: string): Integer;
var
Iname: string;
begin
Result := ToIntForm(NewName, Iname);
if Result = 0 then
begin
if IsFolder(Iname) <> IsFolder(FileName) then
begin
Result := ZM_Error(413, ZE_NoChangeDir);
Exit; // dirOnly status must be same
end;
if CompareStr(Iname, FileName) <> 0 then
begin
SetStatusBit(ZsbDirty or ZsbVChanged or ZsbRenamed);
MyFile.HTRemove(Self);
FileName := Iname;
_FileName := '';
end;
end;
end;
procedure TZMEntryWriter.ClearCachedName;
begin
// does nothing
end;
procedure TZMEntryWriter.FixMinimumVers(IsZ64: Boolean);
const
OS_FAT: Word = (FS_FAT * 256);
WZIP = (FS_NTFS * 256) + 50;
var
NewNeed: Word;
begin
if ((VersionMadeBy and VerMask) <= ZIP64_VER) and
((VersionNeeded and VerMask) <= ZIP64_VER) then
begin
if IsZ64 then
VersionMadeBy := (VersionMadeBy and OSMask) or ZIP64_VER
else
if (VersionMadeBy and VerMask) = ZIP64_VER then
begin
// zip64 no longer needed
VersionMadeBy := (VersionMadeBy and OSMask) or OUR_VEM;
end;
// correct bad encodings - marked ntfs should be fat
if VersionMadeBy = WZIP then
VersionMadeBy := OS_FAT or OUR_VEM;
case CompressionMethod of
0:
NewNeed := 10; // stored
1 .. 8:
NewNeed := 20;
9:
NewNeed := 21; // enhanced deflate
10:
NewNeed := 25; // DCL
12:
NewNeed := 46; // BZip2
else
NewNeed := ZIP64_VER;
end;
if ((Flag and 32) <> 0) and (NewNeed < 27) then
NewNeed := 27;
if IsZ64 and (NewNeed < ZIP64_VER) then
NewNeed := ZIP64_VER;
// keep needed os
VersionNeeded := (VersionNeeded and OSMask) + NewNeed;
end;
end;
function TZMEntryWriter.GetCompressedSize: Int64;
begin
Result := Header.ComprSize;
if Result = MAX_UNSIGNED then
Result := _CSize;
end;
function TZMEntryWriter.GetCompressionMethod: Word;
begin
Result := Header.ComprMethod;
end;
function TZMEntryWriter.GetCRC32: Cardinal;
begin
Result := Header.CRC32;
end;
function TZMEntryWriter.GetDateTime: Cardinal;
begin
Result := Header.ModifDateTime;
end;
function TZMEntryWriter.GetEncrypted: Boolean;
begin
Result := (Flag and FLAG_CRYPT_BIT) <> 0;
if Result <> FCrypt then
Encrypted := FCrypt;
end;
function TZMEntryWriter.GetExtFileAttrib: Longword;
begin
Result := Header.ExtFileAtt;
end;
// returns the 'data' without the tag
function TZMEntryWriter.GetExtraData(Tag: Word): TZMRawBytes;
var
I: Integer;
Sz: Integer;
X: TZMRawBytes;
begin
Result := '';
X := ExtraField;
if XData(X, Tag, I, Sz) then
Result := Copy(X, I + 4, Sz - 4);
end;
function TZMEntryWriter.GetExtraField: TZMRawBytes;
begin
Result := FExtraField;
end;
function TZMEntryWriter.GetExtraFieldLength: Word;
begin
Result := Length(FExtraField);
end;
function TZMEntryWriter.GetFileComment: string;
begin
Result := FFileComment;
end;
function TZMEntryWriter.GetFileCommentLen: Word;
begin
Result := Length(F_FileComment);
end;
function TZMEntryWriter.GetFileName: string;
begin
Result := FFileName;
end;
function TZMEntryWriter.GetFileNameLen: Word;
begin
Result := Length(_FileName);
end;
function TZMEntryWriter.GetFlag: Word;
begin
Result := Header.Flag;
end;
function TZMEntryWriter.GetIntFileAttrib: Word;
begin
Result := Header.IntFileAtt;
end;
function TZMEntryWriter.GetIsEncoded: TZMEncodingOpts;
begin
Result := inherited GetIsEncoded;
end;
function TZMEntryWriter.GetModifDateTime: Longword;
begin
Result := Header.ModifDateTime;
end;
function TZMEntryWriter.GetRelOffLocalHdr: Int64;
begin
Result := Header.RelOffLocalHdr;
if Result = MAX_UNSIGNED then
Result := _RelOfs;
end;
function TZMEntryWriter.GetStartOnDisk: Cardinal;
begin
Result := Header.DiskStart;
if Result = MAX_WORD then
Result := _DiskStt;
end;
function TZMEntryWriter.GetTitle: string;
begin
Result := FileName;
if Status[ZsbRenamed] and (Link <> nil) then
Result := Link.Title + ' :: ' + Result;
end;
function TZMEntryWriter.GetUncompressedSize: Int64;
begin
Result := Header.UncomprSize;
if Result = MAX_UNSIGNED then
Result := _USize;
end;
function TZMEntryWriter.GetVersionMadeBy: Word;
begin
Result := Header.VersionMadeBy;
end;
function TZMEntryWriter.GetVersionNeeded: Word;
begin
Result := Header.VersionNeeded;
end;
function TZMEntryWriter.Get_CompressedSize: Cardinal;
begin
Result := Header.ComprSize;
end;
function TZMEntryWriter.Get_FileComment: TZMRawBytes;
begin
Result := F_FileComment;
end;
function TZMEntryWriter.Get_FileName: TZMRawBytes;
begin
Result := F_FileName;
end;
function TZMEntryWriter.Get_RelOffLocalHdr: Cardinal;
begin
Result := Header.RelOffLocalHdr;
end;
function TZMEntryWriter.Get_StartOnDisk: Word;
begin
Result := Header.DiskStart;
end;
function TZMEntryWriter.Get_UncompressedSize: Cardinal;
begin
Result := Header.UncomprSize;
end;
// also calculate required version and create extra data
function TZMEntryWriter.LocalSize: Cardinal;
begin
Result := Sizeof(TZipLocalHeader);
PrepareLocalData; // form local extra data
Inc(Result, FileNameLen + Length(LocalData));
end;
procedure TZMEntryWriter.PrepareLocalData;
var
Xd: Txdat64;
Need64: Boolean;
begin
LocalData := ''; // empty
ClearStatusBit(ZsbLocal64);
// check for Zip64
Need64 := (UncompressedSize >= MAX_UNSIGNED) or
(CompressedSize >= MAX_UNSIGNED);
FixMinimumVers(Need64);
if Need64 then
begin
SetStatusBit(ZsbLocal64);
Xd.Tag := Zip64_data_tag;
Xd.Siz := 16;
Xd.Vals[0] := UncompressedSize;
Xd.Vals[1] := CompressedSize;
SetLength(FLocalData, 20);
Move(Xd.Tag, PAnsiChar(LocalData)^, 20);
end;
// remove unwanted 'old' tags
if ExtraFieldLength > 0 then
LocalData := LocalData + XDataRemove(ExtraField,
[Zip64_data_tag, NTFS_data_tag, UCmnt_Data_Tag]);
SetStatusBit(ZsbLocalDone);
end;
procedure TZMEntryWriter.SetCompressedSize(const Value: Int64);
begin
if Value >= MAX_UNSIGNED then
begin
_CSize := Value;
Header.ComprSize := MAX_UNSIGNED;
end
else
begin
Header.ComprSize := Value;
_CSize := 0;
end;
end;
procedure TZMEntryWriter.SetCompressionMethod(const Value: Word);
begin
Header.ComprMethod := Value;
end;
procedure TZMEntryWriter.SetCRC32(const Value: Cardinal);
begin
Header.CRC32 := Value;
end;
procedure TZMEntryWriter.SetDateTime(const Value: Cardinal);
begin
Header.ModifDateTime := Value;
end;
procedure TZMEntryWriter.SetEncrypted(const Value: Boolean);
begin
FCrypt := Value;
if Value then
Flag := Flag or FLAG_CRYPT_BIT
else
Flag := Flag and (not FLAG_CRYPT_BIT);
end;
procedure TZMEntryWriter.SetExtFileAttrib(const Value: Longword);
begin
Header.ExtFileAtt := Value;
end;
// assumes data contains the data with no header
procedure TZMEntryWriter.SetExtraData(Tag: Word; const Data: TZMRawBytes);
var
After: Integer;
AfterLen: Integer;
Nidx: Integer;
Ix: Integer;
NewXData: TZMRawBytes;
DataSize: Word;
Sz: Integer;
V: Integer;
X: TZMRawBytes;
begin
X := ExtraField;
XData(X, Tag, Ix, Sz); // find existing Tag
V := Length(X) - Sz; // size after old tag removed
if Length(Data) > 0 then
V := V + Length(Data) + 4;
if V > MAX_WORD then // new length too big?
Exit; // maybe give error
DataSize := Length(Data);
SetLength(NewXData, V);
Nidx := 1; // next Index into newXData
if (DataSize > 0) then
begin
// prefix required tag
NewXData[1] := AnsiChar(Tag and MAX_BYTE);
NewXData[2] := AnsiChar(Tag shr 8);
NewXData[3] := AnsiChar(DataSize and MAX_BYTE);
NewXData[4] := AnsiChar(DataSize shr 8);
// add the data
Move(Data[1], NewXData[5], DataSize);
Inc(Nidx, DataSize + 4);
end;
if Ix >= 1 then
begin
// had existing data
if Ix > 1 then
begin
// append data from before existing tag
Move(X[1], NewXData[Nidx], Ix - 1);
Inc(Nidx, Ix);
end;
After := Ix + Sz; // Index after replaced tag
if After < Length(X) then
begin
// append data from after existing
AfterLen := Length(X) + 1 - After;
Move(X[After], NewXData[Nidx], AfterLen);
end;
end
else
begin
// did not exist
if Length(X) > 0 then
Move(X[1], NewXData[Nidx], Length(X)); // append old extra data
end;
ExtraField := NewXData;
end;
procedure TZMEntryWriter.SetExtraField(const Value: TZMRawBytes);
begin
FExtraField := Value;
FVariables := ''; // will rebuild later
end;
procedure TZMEntryWriter.SetFileComment(const Value: string);
begin
FFileComment := Value;
FVariables := ''; // will rebuild later
Status[ZsbExtCmnt] := StrHasExt(Value);
SetStatusBit(ZsbVChanged);
end;
procedure TZMEntryWriter.SetFileName(const Value: string);
begin
FFileName := Value;
FVariables := ''; // will rebuild later
Status[ZsbExtName] := StrHasExt(Value);
SetStatusBit(ZsbVChanged);
ClearStatusBit(ZsbHashed);
end;
procedure TZMEntryWriter.SetFlag(const Value: Word);
begin
Header.Flag := Value;
end;
procedure TZMEntryWriter.SetIntFileAttrib(const Value: Word);
begin
Header.IntFileAtt := Value;
end;
procedure TZMEntryWriter.SetModifDateTime(const Value: Longword);
begin
Header.ModifDateTime := Value;
end;
procedure TZMEntryWriter.SetRelOffLocalHdr(const Value: Int64);
begin
if Value >= MAX_UNSIGNED then
begin
_RelOfs := Value;
Header.RelOffLocalHdr := MAX_UNSIGNED;
end
else
begin
Header.RelOffLocalHdr := Value;
_RelOfs := 0;
end;
end;
procedure TZMEntryWriter.SetStartOnDisk(const Value: Cardinal);
begin
FStartOnDisk := Value;
if Value >= MAX_WORD then
begin
_DiskStt := Value;
Header.DiskStart := MAX_WORD;
end
else
begin
Header.DiskStart := Value;
_DiskStt := 0;
end;
end;
procedure TZMEntryWriter.SetUncompressedSize(const Value: Int64);
begin
if Value >= MAX_UNSIGNED then
begin
_USize := Value;
Header.UncomprSize := MAX_UNSIGNED;
end
else
begin
Header.UncomprSize := Value;
_USize := 0;
end;
end;
procedure TZMEntryWriter.SetVersionMadeBy(const Value: Word);
begin
Header.VersionMadeBy := Value;
end;
procedure TZMEntryWriter.SetVersionNeeded(const Value: Word);
begin
Header.VersionNeeded := Value;
end;
// converts to internal delimiter
function TZMEntryWriter.StripDrive(const FName: string;
NoPath: Boolean): string;
var
Nam: Integer;
Posn: Integer;
begin
Result := SetSlash(FName, PsdExternal);
// Remove drive: or //host/share
Posn := 0;
if Length(Result) > 1 then
begin
if Result[2] = ':' then
begin
Posn := 2;
if (Length(Result) > 2) and (Result[3] = PathDelim) then
Posn := 3;
end
else
if (Result[1] = PathDelim) and (Result[2] = PathDelim) then
begin
Posn := 3;
while (Posn < Length(Result)) and (Result[Posn] <> PathDelim) do
Inc(Posn);
Inc(Posn);
while (Posn < Length(Result)) and (Result[Posn] <> PathDelim) do
Inc(Posn);
if Posn >= Length(Result) then
begin
// error - invalid host/share
Body.Inform('Invalid filespec: ' + Result, {_LINE_}909, __UNIT__);
Result := '';
Exit;
end;
end;
end;
Inc(Posn);
// remove leading ./
if ((Posn + 1) < Length(Result)) and (Result[Posn] = '.') and
(Result[Posn + 1] = PathDelim) then
Posn := Posn + 2;
// remove path if not wanted
if NoPath then
begin
Nam := LastPos(Result, PathDelim);
if Nam > Posn then
Posn := Nam + 1;
end;
Result := Copy(Result, Posn, MAX_PATH);
end;
function TZMEntryWriter.ToIntForm(const ExtName: string;
var IntName: string): Integer;
var
Temp: string;
begin
Result := 0;
IntName := StripDrive(ExtName, False);
// truncate if too long
if Length(IntName) > MAX_PATH then
begin
Temp := IntName;
SetLength(IntName, MAX_PATH);
Body.Inform('Truncated ' + Temp + ' to ' + IntName, {_LINE_}942, __UNIT__);
end;
if IsInvalidIntName(IntName) then
Result := Body.PrepareErrMsg(ZE_BadFileName, [IntName], {_LINE_}945,
__UNIT__);
end;
// write the central entry on it's MyFile
// return bytes written (< 0 = -Error)
function TZMEntryWriter.Write: Integer;
var
Ch: PZipCentralHeader;
L: Integer;
Need64: Boolean;
Ni: TZMRawBytes;
P: PByte;
Pb: PByte;
R: Integer;
Siz: Word;
Vals: array [0 .. 4] of Int64;
Wf: TZMZipWriter;
X: TZMRawBytes;
begin
Wf := MyFile as TZMZipWriter;
// Diag('Write central');
Result := -1;
if not Wf.IsOpen then
Exit;
Pb := Wf.WBuffer(Sizeof(TZipCentralHeader));
Ch := PZipCentralHeader(Pb);
Ni := _FileName;
Ch^.HeaderSig := CentralFileHeaderSig;
Ch^.VersionMadeBy := VersionMadeBy;
Ch^.VersionNeeded := VersionNeeded;
// assumes local was written - may be updated
Ch^.Flag := Flag;
Ch^.ComprMethod := CompressionMethod;
Ch^.ModifDateTime := ModifDateTime;
Ch^.CRC32 := CRC32;
Ch^.FileNameLen := Length(Ni);
Ch^.FileComLen := Length(_FileComment);
Ch^.IntFileAtt := IntFileAttrib;
Ch^.ExtFileAtt := ExtFileAttrib;
Siz := 0;
if (UncompressedSize >= MAX_UNSIGNED) then
begin
Vals[0] := UncompressedSize;
Siz := 8;
Ch^.UncomprSize := MAX_UNSIGNED;
end
else
Ch^.UncomprSize := Cardinal(UncompressedSize);
if (CompressedSize >= MAX_UNSIGNED) then
begin
Vals[Siz div 8] := CompressedSize;
Inc(Siz, 8);
Ch^.ComprSize := MAX_UNSIGNED;
end
else
Ch^.ComprSize := Cardinal(CompressedSize);
if (RelOffLocalHdr >= MAX_UNSIGNED) then
begin
Vals[Siz div 8] := RelOffLocalHdr;
Inc(Siz, 8);
Ch^.RelOffLocalHdr := MAX_UNSIGNED;
end
else
Ch^.RelOffLocalHdr := Cardinal(RelOffLocalHdr);