1 {$INCLUDE ../shared/a_modes.inc}
5 // Implementation restrictions:
6 // - File must start with LFH or EOCD signature
7 // - EOCD must be located strictly at the end of file
8 // - Multi-disk ZIP files are not supported
9 // - Expect UTF-8 or CP1251 encoded names
10 // - ZIP64 not supported
11 // - Encryption not supported
12 // - Zero-length file names not supported
13 // - CDR holds most actual data about file, LFH mostly ignored
14 // - Attributes, comments and extra data are ignored and not saved
15 // - Store and Deflate compression supported
19 uses Classes
, WADEDITOR
;
30 stream
: TMemoryStream
;
36 list
: array of TResource
;
39 PResource
= ^TResource
;
42 TZIPEditor
= class sealed(WADEDITOR
.TWADEditor
)
44 FSection
: array of TSection
;
49 function FindSectionIDRAW(name
: AnsiString; caseSensitive
: Boolean): Integer;
50 function FindSectionRAW(name
: AnsiString; caseSensitive
: Boolean): PSection
;
51 function InsertSectionRAW(name
: AnsiString; mtime
: UInt32
): PSection
;
53 function FindSectionID(name
: AnsiString): Integer;
54 function FindSection(name
: AnsiString): PSection
;
55 function InsertSection(name
: AnsiString; mtime
: UInt32
): PSection
;
57 function InsertFileInfo(const section
, name
: AnsiString; pos
, csize
, usize
, comp
, crc
, mtime
: UInt32
): PResource
;
58 function Preload(p
: PResource
): Boolean;
59 function GetSourceStream(p
: PResource
): TStream
;
61 procedure ReadLFH(s
: TStream
; fname
: AnsiString; xcsize
, xusize
, xcomp
, xcrc
, xtime
: UInt32
);
62 procedure ReadCDR(s
: TStream
; cdrid
: Integer);
63 function FindEOCD(s
: TStream
): Boolean;
64 procedure ReadEOCD(s
: TStream
);
66 procedure WriteLFH(s
: TStream
; comp
, mtime
, crc
, csize
, usize
: UInt32
; const afname
: AnsiString);
67 procedure WriteCDR(s
: TStream
; comp
, mtime
, crc
, csize
, usize
, eattr
, offset
: UInt32
; const afname
: AnsiString; cdrid
: Integer);
68 procedure SaveToStream(s
: TStream
);
72 destructor Destroy(); override;
73 procedure FreeWAD(); override;
74 function ReadFile2(FileName
: string): Boolean; override;
75 function ReadMemory(Data
: Pointer; Len
: LongWord): Boolean; override;
76 procedure CreateImage(); override;
77 function AddResource(Data
: Pointer; Len
: LongWord; Name
, Section
: String): Boolean; override; overload
;
78 function AddResource(FileName
, Name
, Section
: String): Boolean; override; overload
;
79 function AddAlias(Res
, Alias
: String): Boolean; override;
80 procedure AddSection(Name
: String); override;
81 procedure RemoveResource(Section
, Resource
: String); override;
82 procedure SaveTo(FileName
: String); override;
83 function HaveResource(Section
, Resource
: String): Boolean; override;
84 function HaveSection(Section
: string): Boolean; override;
85 function GetResource(Section
, Resource
: String; var pData
: Pointer; var Len
: Integer): Boolean; override;
86 function GetSectionList(): SArray
; override;
87 function GetResourcesList(Section
: String): SArray
; override;
89 function GetLastError
: Integer; override;
90 function GetLastErrorStr
: String; override;
91 function GetResourcesCount
: Word; override;
92 function GetVersion
: Byte; override;
97 uses SysUtils
, StrUtils
, DateUtils
, Math
, utils
, zstream
, crc
, e_log
;
100 ZIP_SIGN_CDR
= 'PK'#1#2;
101 ZIP_SIGN_LFH
= 'PK'#3#4;
102 ZIP_SIGN_EOCD
= 'PK'#5#6;
107 ZIP_COMP_REDUCE1
= 2;
108 ZIP_COMP_REDUCE2
= 3;
109 ZIP_COMP_REDUCE3
= 4;
110 ZIP_COMP_REDUCE4
= 5;
111 ZIP_COMP_IMPLODE
= 6;
112 ZIP_COMP_TOKENIZED
= 7;
113 ZIP_COMP_DEFLATE
= 8;
114 ZIP_COMP_DEFLATE64
= 9;
115 ZIP_COMP_TERSE1
= 10;
119 ZIP_COMP_TERSE2
= 18;
126 ZIP_COMP_WAVPACK
= 97;
131 ZIP_SYSTEM
= 0; // DOS / FAT
132 ZIP_MAXVERSION
= 63; // Max supported version
135 ZIP_ENCRYPTION_MASK
= (1 << 0) or (1 << 6) or (1 << 13);
136 ZIP_UTF8_MASK
= (1 << 11);
138 function IsASCII(const s
: AnsiString): Boolean;
141 for i
:= 1 to Length(s
) do
152 function IsUTF8(const s
: AnsiString): Boolean;
153 var i
, j
, len
: Integer;
156 i
:= 1; len
:= Length(s
);
161 $80..$BF: exit
; // invalid encoding
165 otherwise exit
; // invalid encoding
170 if i
> len
then exit
; // invlid length
173 else exit
; // invalid encoding
182 function DosToStr(dostime
: UInt32
): AnsiString;
185 DateTimeToString(Result
, 'yyyy/mm/dd hh:nn:ss', DosDateTimeToDateTime(dostime
));
186 except on e
: EConvertError
do
187 Result
:= 'INVALID ($' + IntToHex(dostime
, 8) + ')';
191 procedure ToSectionFile(fname
: AnsiString; out section
, name
: AnsiString); inline;
194 i
:= LastDelimiter('/', fname
);
195 section
:= Copy(fname
, 1, i
- 1);
196 name
:= Copy(fname
, i
+ 1)
199 function GetFileName(const Section
, Name
: AnsiString): AnsiString; inline;
204 Result
:= Section
+ '/' + Name
;
207 function PrepString(const s
: AnsiString; caseSensitive
, extSensitive
: Boolean): AnsiString; inline;
211 if caseSensitive
= False then
213 Result
:= UpperCase(Result
);
215 if extSensitive
= False then
217 i
:= Pos('.', Result
); // fix dotfiles
219 SetLength(Result
, i
- 1);
223 function FindResourceIDRAW(p
: PSection
; name
: AnsiString; caseSensitive
, extSensitive
: Boolean): Integer;
224 var i
: Integer; pname
: AnsiString;
228 pname
:= PrepString(name
, caseSensitive
, extSensitive
);
229 for i
:= 0 to High(p
.list
) do
231 if PrepString(p
.list
[i
].name
, caseSensitive
, extSensitive
) = pname
then
241 function FindResourceID(p
: PSection
; name
: AnsiString): Integer;
244 i
:= FindResourceIDRAW(p
, name
, True, True); // CaSeNaMe.Ext
247 i
:= FindResourceIDRAW(p
, name
, False, True); // CASENAME.EXT
250 i
:= FindResourceIDRAW(p
, name
, True, False); // CaSeNaMe
253 i
:= FindResourceIDRAW(p
, name
, False, False); // CASENAME
260 function FindResource(p
: PSection
; name
: AnsiString): PResource
;
263 i
:= FindResourceID(p
, name
);
272 function TZIPEditor
.FindSectionIDRAW(name
: AnsiString; caseSensitive
: Boolean): Integer;
273 var i
: Integer; pname
: AnsiString;
275 if FSection
<> nil then
277 pname
:= PrepString(name
, caseSensitive
, True);
278 for i
:= 0 to High(FSection
) do
280 if PrepString(FSection
[i
].name
, caseSensitive
, True) = pname
then
290 function TZIPEditor
.FindSectionRAW(name
: AnsiString; caseSensitive
: Boolean): PSection
;
293 i
:= FindSectionIDRAW(name
, caseSensitive
);
295 Result
:= @FSection
[i
]
300 function TZIPEditor
.InsertSectionRAW(name
: AnsiString; mtime
: UInt32
): PSection
;
303 if FSection
= nil then i
:= 0 else i
:= Length(FSection
);
304 SetLength(FSection
, i
+ 1);
305 FSection
[i
] := Default(TSection
);
306 FSection
[i
].name
:= name
;
307 FSection
[i
].mtime
:= mtime
;
308 Result
:= @FSection
[i
];
313 function TZIPEditor
.FindSectionID(name
: AnsiString): Integer;
314 var fixName
: AnsiString;
316 fixName
:= StringReplace(name
, '\', '/', [rfReplaceAll
], TStringReplaceAlgorithm
.sraManySmall
);
317 Result
:= FindSectionIDRAW(fixName
, True); // CaSeNaMe
319 Result
:= FindSectionIDRAW(fixName
, False); // CASENAME
322 function TZIPEditor
.FindSection(name
: AnsiString): PSection
;
323 var fixName
: AnsiString;
325 fixName
:= StringReplace(name
, '\', '/', [rfReplaceAll
], TStringReplaceAlgorithm
.sraManySmall
);
326 Result
:= FindSectionRAW(fixName
, True); // CaSeNaMe
328 Result
:= FindSectionRAW(fixName
, False); // CASENAME
331 function TZIPEditor
.InsertSection(name
: AnsiString; mtime
: UInt32
): PSection
;
333 Result
:= FindSection(name
);
335 Result
:= InsertSectionRAW(name
, mtime
);
340 function TZIPEditor
.InsertFileInfo(const section
, name
: AnsiString; pos
, csize
, usize
, comp
, crc
, mtime
: UInt32
): PResource
;
341 var p
: PSection
; i
: Integer;
343 p
:= FindSectionRAW(section
, True);
345 p
:= InsertSectionRAW(section
, mtime
);
346 if p
.list
= nil then i
:= 0 else i
:= Length(p
.list
);
347 SetLength(p
.list
, i
+ 1);
348 p
.list
[i
] := Default(TResource
);
349 p
.list
[i
].name
:= name
;
350 p
.list
[i
].pos
:= pos
;
351 p
.list
[i
].csize
:= csize
;
352 p
.list
[i
].usize
:= usize
;
353 p
.list
[i
].comp
:= comp
;
354 p
.list
[i
].chksum
:= crc
;
355 p
.list
[i
].mtime
:= mtime
;
356 p
.list
[i
].stream
:= nil;
357 Result
:= @p
.list
[i
];
362 function TZIPEditor
.AddAlias(Res
, Alias
: String): Boolean;
364 // Hard-links not supported in ZIP
365 // However, they never created by editor
369 function TZIPEditor
.AddResource(Data
: Pointer; Len
: LongWord; Name
, Section
: String): Boolean;
370 const compress
: Boolean = True;
371 const level
: TCompressionLevel
= TCompressionLevel
.clMax
;
372 var s
: TMemoryStream
; cs
: TCompressionStream
; p
: PResource
;
373 var comp
, crc
: UInt32
;
378 s
:= TMemoryStream
.Create();
380 if compress
and (Len
> 0) then
382 cs
:= TCompressionStream
.Create(level
, s
, True);
384 cs
.WriteBuffer(PByte(Data
)[0], Len
);
386 comp
:= ZIP_COMP_DEFLATE
;
391 if (Len
= 0) or (compress
= False) or (s
.Size
>= Len
) then
393 s
.Seek(0, TSeekOrigin
.soBeginning
);
395 s
.WriteBuffer(PByte(Data
)[0], Len
);
396 comp
:= ZIP_COMP_STORE
;
397 Assert(s
.Size
= Len
);
399 crc
:= crc32(0, nil, 0);
400 crc
:= crc32(crc
, data
, len
);
401 p
:= InsertFileInfo(Section
, Name
, $ffffffff, s
.Size
, Len
, comp
, crc
, DateTimeToDosDateTime(Now()));
411 function TZIPEditor
.AddResource(FileName
, Name
, Section
: String): Boolean;
412 var s
: TFileStream
; ptr
: PByte;
415 FLastError
:= DFWAD_ERROR_READWAD
;
417 s
:= TFileStream
.Create(FileName
, fmOpenRead
or fmShareDenyWrite
);
421 s
.ReadBuffer(ptr
[0], s
.Size
);
422 Result
:= AddResource(ptr
, s
.Size
, Name
, Section
);
423 if Result
= True then FLastError
:= DFWAD_NOERROR
;
433 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
434 e_WriteLog('DFZIP: AddResource: failed to open file ' + FileName
, MSG_NOTIFY
);
435 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
440 constructor TZIPEditor
.Create();
444 FLastError
:= DFWAD_NOERROR
;
449 destructor TZIPEditor
.Destroy();
455 procedure TZIPEditor
.FreeWAD();
458 if FSection
<> nil then
460 for i
:= 0 to High(FSection
) do
462 if FSection
[i
].list
<> nil then
464 for j
:= 0 to High(FSection
[i
].list
) do
466 if FSection
[i
].list
[j
].stream
<> nil then
468 FreeAndNil(FSection
[i
].list
[j
].stream
);
471 SetLength(FSection
[i
].list
, 0);
474 SetLength(FSection
, 0);
476 if FStream
<> nil then
480 FLastError
:= DFWAD_NOERROR
;
484 function TZIPEditor
.Preload(p
: PResource
): Boolean;
485 var s
: TMemoryStream
;
490 Result
:= p
.stream
<> nil;
491 if (p
.stream
= nil) and (FStream
<> nil) then
493 s
:= TMemoryStream
.Create();
497 FStream
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
498 s
.CopyFrom(FStream
, p
.csize
);
500 Assert(s
.Size
= p
.csize
); // wtf, random size if copied zero bytes!
510 procedure TZIPEditor
.CreateImage();
513 if FStream
= nil then
515 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
516 e_WriteLog('DFZIP: CreateImage: File not assigned', MSG_NOTIFY
);
517 FLastError
:= DFWAD_ERROR_WADNOTLOADED
;
519 else if FStream
is TMemoryStream
then
521 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
522 e_WriteLog('DFZIP: CreateImage: Memory stream', MSG_NOTIFY
);
523 FLastError
:= DFWAD_NOERROR
;
527 if FSection
<> nil then
529 for i
:= 0 to High(FSection
) do
531 if FSection
[i
].list
<> nil then
533 for j
:= 0 to High(FSection
[i
].list
) do
535 if Preload(@FSection
[i
].list
[j
]) = False then
537 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
538 e_WriteLog('DFZIP: CreateImage: failed to preload resource [' + FSection
[i
].name
+ '][' + FSection
[i
].list
[j
].name
+ ']', MSG_WARNING
);
539 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
547 FLastError
:= DFWAD_NOERROR
;
551 procedure TZIPEditor
.AddSection(Name
: String);
553 if InsertSection(Name
, DateTimeToDosDateTime(Now())) = nil then
554 raise Exception
.Create('DFZIP: AddSection[' + Name
+ ']: failed to insert');
557 function TZIPEditor
.HaveResource(Section
, Resource
: String): Boolean;
559 Result
:= FindResource(FindSection(Section
), Resource
) <> nil;
562 function TZIPEditor
.HaveSection(Section
: String): Boolean;
564 Result
:= FindSection(Section
) <> nil;
567 function TZIPEditor
.GetSourceStream(p
: PResource
): TStream
;
571 if p
.stream
<> nil then
574 src
.Seek(0, TSeekOrigin
.soBeginning
);
576 else if FStream
<> nil then
579 src
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
584 function TZIPEditor
.GetResource(Section
, Resource
: String; var pData
: Pointer; var Len
: Integer): Boolean;
585 var p
: PResource
; ptr
: PByte; src
: TStream
; tmp
: TDecompressionStream
; crc
: UInt32
;
587 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
591 p
:= FindResource(FindSection(Section
), Resource
);
594 src
:= GetSourceStream(p
);
600 Assert(p
.csize
= p
.usize
);
601 GetMem(ptr
, p
.usize
);
604 src
.ReadBuffer(ptr
[0], p
.usize
);
610 except on e
: EReadError
do
611 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
612 e_WriteLog('DFZIP: Failed to read STOREd data, reason: ' + e
.Message, MSG_WARNING
);
617 tmp
:= TDecompressionStream
.Create(src
, True);
619 GetMem(ptr
, p
.usize
);
621 tmp
.ReadBuffer(ptr
[0], p
.usize
);
631 on e
: EStreamError
do
633 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
634 e_WriteLog('DFZIP: Failed to decompress DEFLATEd data, reason: ' + e
.Message, MSG_WARNING
);
639 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
640 e_WriteLog('DFZIP: Unsupported compression method: ' + IntToStr(p
.comp
), MSG_WARNING
);
645 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
646 e_WriteLog('DFZIP: No available source for file data', MSG_WARNING
);
647 FLastError
:= DFWAD_ERROR_WADNOTLOADED
;
649 if Result
= True then
651 crc
:= crc32(0, nil, 0);
652 crc
:= crc32(crc
, ptr
, p
.usize
);
653 Result
:= crc
= p
.chksum
;
654 if Result
= True then
658 FLastError
:= DFWAD_NOERROR
;
662 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
663 e_WriteLog('DFZIP: File integrity check failed: expected CRC32 $' + IntToHex(p
.chksum
, 8) + ', calculated CRC32 $' + IntToHex(crc
, 8), MSG_WARNING
);
670 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
671 e_WriteLog('DFZIP: Resource not found', MSG_NOTIFY
);
672 FLastError
:= DFWAD_ERROR_RESOURCENOTFOUND
;
676 function TZIPEditor
.GetResourcesList(Section
: String): SArray
;
677 var p
: PSection
; i
: Integer;
680 p
:= FindSection(Section
);
681 if (p
<> nil) and (p
.list
<> nil) then
683 SetLength(Result
, Length(p
.list
));
684 for i
:= 0 to High(p
.list
) do
686 Result
[i
] := p
.list
[i
].name
;
691 function TZIPEditor
.GetSectionList(): SArray
;
695 if FSection
<> nil then
697 SetLength(Result
, Length(FSection
));
698 for i
:= 0 to High(FSection
) do
700 Result
[i
] := FSection
[i
].name
;
705 procedure TZIPEditor
.ReadLFH(s
: TStream
; fname
: AnsiString; xcsize
, xusize
, xcomp
, xcrc
, xtime
: UInt32
);
706 var sig
: packed array [0..3] of Char;
707 var va
, vb
, flags
, comp
: UInt16
;
708 var mtime
, crc
, csize
, usize
: UInt32
;
709 var fnlen
, extlen
: UInt16
;
710 var mypos
, datapos
: UInt64;
711 var section
, name
: AnsiString;
715 if mypos
+ 30 <= s
.Size
then
717 s
.ReadBuffer(sig
[0], 4);
718 if sig
= ZIP_SIGN_LFH
then
720 va
:= s
.ReadByte(); // Min Version
721 vb
:= s
.ReadByte(); // Min System
722 flags
:= LEtoN(s
.ReadWord());
723 comp
:= LEtoN(s
.ReadWord());
724 mtime
:= LEtoN(s
.ReadDWord());
725 crc
:= LEtoN(s
.ReadDWord());
726 csize
:= LEtoN(s
.ReadDWord());
727 usize
:= LEtoN(s
.ReadDWord());
728 fnlen
:= LEtoN(s
.ReadWord());
729 extlen
:= LEtoN(s
.ReadWord());
730 datapos
:= s
.Position
+ fnlen
+ extlen
;
731 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
733 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(va
), MSG_NOTIFY
);
734 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(vb
), MSG_NOTIFY
);
735 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
736 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
737 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Modification Time : ' + DosToStr(mtime
), MSG_NOTIFY
);
738 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
739 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
740 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
741 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
742 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(extlen
), MSG_NOTIFY
);
743 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': <DATA OFFSET> : $' + IntToHex(datapos
, 8), MSG_NOTIFY
);
745 if (va
>= 10) and (va
<= ZIP_MAXVERSION
) then
747 if datapos
+ xcsize
<= s
.Size
then
749 ToSectionFile(fname
, section
, name
);
752 p
:= FindSectionRAW(section
, True);
754 p
:= InsertSectionRAW(section
, xtime
);
758 p
:= InsertFileInfo(section
, name
, datapos
, xcsize
, xusize
, xcomp
, xcrc
, xtime
);
761 raise Exception
.Create('Failed to register resource [' + fname
+ ']');
764 raise Exception
.Create('Invalid LFH size (corrupted file?)');
768 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
769 raise Exception
.Create('Unsupported CDR version ' + IntToStr(va
) + ', not in range [10..' + IntToStr(ZIP_MAXVERSION
) + ']');
773 raise Exception
.Create('Invalid LFH signature $' +IntToHex(Ord(sig
[0]), 2) + ' $' +IntToHex(Ord(sig
[1]), 2) + ' $' +IntToHex(Ord(sig
[2]), 2) + ' $' +IntToHex(Ord(sig
[3]), 2) + ' (corrupted file?)');
776 raise Exception
.Create('Invalid LFH size (corrupted file?)');
779 procedure TZIPEditor
.ReadCDR(s
: TStream
; cdrid
: Integer);
780 var sig
: packed array [0..3] of Char;
781 var vva
, vvb
, va
, vb
, flags
, comp
: UInt16
;
782 var mtime
, crc
, csize
, usize
: UInt32
;
783 var fnlen
, extlen
, comlen
, disk
, iattr
: UInt16
;
784 var eattr
, offset
: UInt32
;
785 var mypos
, next
: UInt64;
787 var aname
: AnsiString;
788 var cvtbug
, utf8
: Boolean;
791 s
.ReadBuffer(sig
[0], 4);
792 if sig
= ZIP_SIGN_CDR
then
794 // Valid Central Directory Signature
795 vva
:= s
.ReadByte(); // Writer Version
796 vvb
:= s
.ReadByte(); // Writer System
797 va
:= s
.ReadByte(); // Min Version
798 vb
:= s
.ReadByte(); // Min System
799 flags
:= LEtoN(s
.ReadWord());
800 comp
:= LEtoN(s
.ReadWord());
801 mtime
:= LEtoN(s
.ReadDWord());
802 crc
:= LEtoN(s
.ReadDWord());
803 csize
:= LEtoN(s
.ReadDWord());
804 usize
:= LEtoN(s
.ReadDWord());
805 fnlen
:= LEtoN(s
.ReadWord());
806 extlen
:= LEtoN(s
.ReadWord());
807 comlen
:= LEtoN(s
.ReadWord());
808 disk
:= LEtoN(s
.ReadWord());
809 iattr
:= LEtoN(s
.ReadWord());
810 eattr
:= LEtoN(s
.ReadDWord());
811 offset
:= LEtoN(s
.ReadDWord());
812 next
:= s
.Position
+ fnlen
+ extlen
+ comlen
;
814 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
816 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer Version : ' + IntToStr(vva
), MSG_NOTIFY
);
817 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer System : ' + IntToStr(vvb
), MSG_NOTIFY
);
818 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(va
), MSG_NOTIFY
);
819 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(vb
), MSG_NOTIFY
);
820 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
821 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
822 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Modification Time : ' + DosToStr(mtime
), MSG_NOTIFY
);
823 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
824 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
825 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
826 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
827 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(extlen
), MSG_NOTIFY
);
828 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(comlen
), MSG_NOTIFY
);
829 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Disk : ' + IntToStr(disk
), MSG_NOTIFY
);
830 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Internal Attrib : $' + IntToHex(iattr
, 4), MSG_NOTIFY
);
831 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': External Attrib : $' + IntToHex(eattr
, 8), MSG_NOTIFY
);
832 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': LFH Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
835 if (vva
= $10) and (vvb
= $0A) and (va
= $10) and (vb
= $00) and (flags
= (1 << 10)) and (mtime
= 0) and (iattr
= 0) and (eattr
= 0) then
837 // HACK: Editor and wadcvt for long time sets incorrent flag for UTF-8
838 flags
:= ZIP_UTF8_MASK
;
841 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
842 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': WADCVT BUG : ' + BoolToStr(cvtbug
, True), MSG_NOTIFY
);
843 if (va
>= 10) and (va
<= ZIP_MAXVERSION
) then
845 if (flags
and ZIP_ENCRYPTION_MASK
) = 0 then
847 if (csize
<> $ffffffff) and (usize
<> $ffffffff) and (disk
<> $ffff) and (offset
<> $ffffffff) then
851 if (next
<= s
.Size
) and (fnlen
> 0) then
855 if csize
<> usize
then
856 raise Exception
.Create('Compressed size ' + IntToStr(csize
) + ' != Descompressed size ' + IntToStr(usize
) + 'for STORE method (corrupted file?)');
880 raise Exception
.Create('Encrypted archives not supported');
882 raise Exception
.Create('Unknown compression method ' + IntToStr(comp
));
884 GetMem(name
, UInt32(fnlen
) + 1);
886 s
.ReadBuffer(name
[0], fnlen
);
890 if (flags
and ZIP_UTF8_MASK
= 0) and (IsUTF8(name
) = False) then
892 aname
:= win2utf(aname
);
895 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
897 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': UTF-8 Comatible : ' + BoolToStr(utf8
, True), MSG_NOTIFY
);
898 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name : "' + aname
+ '"', MSG_NOTIFY
);
900 s
.Seek(offset
, TSeekOrigin
.soBeginning
);
901 ReadLFH(s
, aname
, csize
, usize
, comp
, crc
, mtime
);
903 s
.Seek(next
, TSeekOrigin
.soBeginning
);
908 raise Exception
.Create('Empty files names not supported');
911 raise Exception
.Create('Splitted archives not supported');
915 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
916 raise Exception
.Create('ZIP64 not supported');
921 FLastError
:= DFWAD_ERROR_READWAD
;
922 raise Exception
.Create('Encrypted archives not supported');
927 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
928 raise Exception
.Create('Unsupported CDR version ' + IntToStr(va
) + ', not in range [10..' + IntToStr(ZIP_MAXVERSION
) + ']');
932 raise Exception
.Create('Invalid CDR signature $' + IntToHex(Ord(sig
[0]), 2) + ' $' +IntToHex(Ord(sig
[1]), 2) + ' $' +IntToHex(Ord(sig
[2]), 2) + ' $' +IntToHex(Ord(sig
[3]), 2) + ' (corrupted file?)');
935 function TZIPEditor
.FindEOCD(s
: TStream
): Boolean;
936 const maxedir
= 20; // end of central directory entry
937 const maxecdir
= maxedir
+ 65536; // + comment
938 var sig
: packed array [0..3] of Char; off
, lim
: Int64;
941 if s
.Size
>= maxedir
then
943 if s
.Size
< maxecdir
then lim
:= s
.Size
else lim
:= maxecdir
;
944 lim
:= lim
- maxedir
;
946 while (off
<= lim
) and (Result
= False) do
948 s
.Seek(s
.Size
- off
, TSeekOrigin
.soBeginning
);
949 s
.ReadBuffer(sig
[0], 4);
950 Result
:= sig
= ZIP_SIGN_EOCD
;
956 procedure TZIPEditor
.ReadEOCD(s
: TStream
);
957 var sig
: packed array [0..3] of Char;
958 var idisk
, ndisk
, nrec
, total
, comlen
: UInt16
;
959 var csize
, cpos
, i
: UInt32
;
962 FLastError
:= DFWAD_ERROR_FILENOTWAD
;
964 s
.ReadBuffer(sig
[0], 4);
965 if (sig
= ZIP_SIGN_LFH
) or (sig
= ZIP_SIGN_EOCD
) then
969 // End of Central Directory found
970 FLastError
:= DFWAD_ERROR_READWAD
;
971 mypos
:= s
.Position
- 4;
972 idisk
:= LEtoN(s
.ReadWord());
973 ndisk
:= LEtoN(s
.ReadWord());
974 nrec
:= LEtoN(s
.ReadWord());
975 total
:= LEtoN(s
.ReadWord());
976 csize
:= LEtoN(s
.ReadDWord());
977 cpos
:= LEtoN(s
.ReadDWord());
978 comlen
:= LEtoN(s
.ReadWord());
979 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
981 e_WriteLog('==============================================', MSG_NOTIFY
);
982 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID : ' + IntToStr(idisk
), MSG_NOTIFY
);
983 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID with CD : ' + IntToStr(ndisk
), MSG_NOTIFY
);
984 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Available CDR''s : ' + IntToStr(nrec
), MSG_NOTIFY
);
985 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Total CDR''s : ' + IntToStr(total
), MSG_NOTIFY
);
986 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Length : ' + IntToStr(csize
), MSG_NOTIFY
);
987 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Offset : $' + IntToHex(cpos
, 8), MSG_NOTIFY
);
988 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(comlen
), MSG_NOTIFY
);
990 if (idisk
<> $ffff) and (ndisk
<> $ffff) and (nrec
<> $ffff) and (total
<> $ffff) and (csize
<> $ffffffff) and (cpos
<> $ffffffff) then
992 if s
.Position
+ comlen
= s
.Size
then
994 if (idisk
= 0) and (ndisk
= 0) and (nrec
= total
) then
996 if (nrec
* 46 <= csize
) and (UInt64(cpos
) + csize
<= s
.Size
) then
1001 s
.Seek(cpos
, TSeekOrigin
.soBeginning
);
1004 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1005 e_WriteLog('==============================================', MSG_NOTIFY
);
1009 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1010 e_WriteLog('==============================================', MSG_NOTIFY
);
1014 raise Exception
.Create('Central Directory too big (corrupted file?)');
1017 raise Exception
.Create('Splitted archives not supported');
1020 raise Exception
.Create('EOCD too big (corrupted file?)');
1023 raise Exception
.Create('ZIP64 not supported');
1026 raise Exception
.Create('EOCD not found (corrupted file?)');
1029 raise Exception
.Create('Not DFZIP formated file');
1032 function TZIPEditor
.ReadFile2(FileName
: String): Boolean;
1039 s
:= TFileStream
.Create(FileName
, fmOpenRead
or fmShareDenyWrite
);
1043 FLastError
:= DFWAD_NOERROR
;
1052 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1053 e_WriteLog('ZIP: Failed to read ZIP from file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
1058 on e
: EFOpenError
do
1060 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1061 e_WriteLog('DFZIP: Failed to open file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
1062 if FileExists(FileName
) then
1063 FLastError
:= DFWAD_ERROR_CANTOPENWAD
1065 FLastError
:= DFWAD_ERROR_WADNOTFOUND
;
1070 function TZIPEditor
.ReadMemory(Data
: Pointer; Len
: LongWord): Boolean;
1071 var s
: TMemoryStream
;
1076 s
:= TMemoryStream
.Create
;
1079 s
.WriteBuffer(PByte(Data
)[0], Len
);
1080 s
.Seek(0, soBeginning
);
1083 FLastError
:= DFWAD_NOERROR
;
1092 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1093 e_WriteLog('DFZIP: Failed to read ZIP from memory, reason: ' + e
.Message, MSG_WARNING
);
1099 procedure TZIPEditor
.RemoveResource(Section
, Resource
: String);
1100 var p
: PSection
; i
: Integer;
1102 p
:= FindSection(Section
);
1103 i
:= FindResourceID(p
, Resource
);
1106 if p
.list
[i
].stream
<> nil then
1107 FreeAndNil(p
.list
[i
].stream
);
1108 for i
:= i
+ 1 to High(p
.list
) do
1110 p
.list
[i
- 1] := p
.list
[i
];
1112 SetLength(p
.list
, High(p
.list
));
1116 function GetZIPVersion(const afname
: AnsiString; flags
, comp
: UInt16
): UInt8
;
1119 version
:= 10; // Base version
1121 ZIP_COMP_STORE
: version
:= 10;
1122 ZIP_COMP_SHRUNK
: version
:= 10;
1123 ZIP_COMP_REDUCE1
: version
:= 10;
1124 ZIP_COMP_REDUCE2
: version
:= 10;
1125 ZIP_COMP_REDUCE3
: version
:= 10;
1126 ZIP_COMP_REDUCE4
: version
:= 10;
1127 ZIP_COMP_IMPLODE
: version
:= 10;
1128 ZIP_COMP_TOKENIZED
: version
:= 20;
1129 ZIP_COMP_DEFLATE
: version
:= 20;
1130 ZIP_COMP_DEFLATE64
: version
:= 21;
1131 ZIP_COMP_TERSE1
: version
:= 25; // PKWARE DCL Implode
1132 ZIP_COMP_BZIP2
: version
:= 46;
1133 ZIP_COMP_LZMA
: version
:= 63;
1134 ZIP_COMP_CMPSC
: version
:= 63;
1135 ZIP_COMP_TERSE2
: version
:= 63;
1136 ZIP_COMP_LZ77
: version
:= 63;
1137 ZIP_COMP_ZSTD1
: version
:= 63;
1138 ZIP_COMP_ZSTD2
: version
:= 63;
1139 ZIP_COMP_MP3
: version
:= 63;
1140 ZIP_COMP_XZ
: version
:= 63;
1141 ZIP_COMP_JPEG
: version
:= 63;
1142 ZIP_COMP_WAVPACK
: version
:= 63;
1143 ZIP_COMP_PPMD
: version
:= 63;
1144 ZIP_COMP_AE
: version
:= 63;
1146 if afname
[Length(afname
)] = '/' then
1147 version
:= Max(20, version
); // Folder
1148 if flags
and ZIP_UTF8_MASK
<> 0 then
1149 version
:= Max(63, version
); // UTF-8 name
1153 procedure TZIPEditor
.WriteLFH(s
: TStream
; comp
, mtime
, crc
, csize
, usize
: UInt32
; const afname
: AnsiString);
1154 var fname
: PChar; version
: UInt8
; fnlen
, flags
: UInt16
; mypos
: UInt64;
1156 mypos
:= s
.Position
;
1157 fname
:= PChar(afname
);
1158 fnlen
:= Length(fname
);
1160 if IsASCII(afname
) = False then
1161 flags
:= flags
or ZIP_UTF8_MASK
;
1162 version
:= GetZIPVersion(afname
, flags
, comp
);
1163 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1165 e_WriteLog('==============================================', MSG_NOTIFY
);
1166 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(version
), MSG_NOTIFY
);
1167 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1168 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
1169 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
1170 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Modification Time : ' + DosToStr(mtime
), MSG_NOTIFY
);
1171 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
1172 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
1173 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
1174 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
1175 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(0), MSG_NOTIFY
);
1176 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name : "' + fname
+ '"', MSG_NOTIFY
);
1178 s
.WriteBuffer(ZIP_SIGN_LFH
, 4); // LFH Signature
1179 s
.WriteByte(version
); // Min version
1180 s
.WriteByte(ZIP_SYSTEM
); // System
1181 WriteInt(s
, UInt16(flags
)); // Flags
1182 WriteInt(s
, UInt16(comp
)); // Compression method
1183 WriteInt(s
, UInt32(mtime
)); // Modification time/date
1184 WriteInt(s
, UInt32(crc
)); // CRC-32
1185 WriteInt(s
, UInt32(csize
)); // Compressed size
1186 WriteInt(s
, UInt32(usize
)); // Decompressed size
1187 WriteInt(s
, UInt16(fnlen
)); // Name field length
1188 WriteInt(s
, UInt16(0)); // Extra field length
1189 s
.WriteBuffer(fname
[0], fnlen
); // File Name
1192 procedure TZIPEditor
.WriteCDR(s
: TStream
; comp
, mtime
, crc
, csize
, usize
, eattr
, offset
: UInt32
; const afname
: AnsiString; cdrid
: Integer);
1193 var fname
: PChar; version
: UInt8
; fnlen
, flags
: UInt16
; mypos
: UInt64;
1195 mypos
:= s
.Position
;
1196 fname
:= PChar(afname
);
1197 fnlen
:= Length(fname
);
1199 if IsASCII(afname
) = False then
1200 flags
:= flags
or ZIP_UTF8_MASK
;
1201 version
:= GetZIPVersion(afname
, flags
, comp
);
1202 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1204 e_WriteLog('==============================================', MSG_NOTIFY
);
1205 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer Version : ' + IntToStr(ZIP_MAXVERSION
), MSG_NOTIFY
);
1206 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1207 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(version
), MSG_NOTIFY
);
1208 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1209 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
1210 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
1211 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Modification Time : ' + DosToStr(mtime
), MSG_NOTIFY
);
1212 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
1213 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
1214 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
1215 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
1216 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(0), MSG_NOTIFY
);
1217 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(0), MSG_NOTIFY
);
1218 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Disk : ' + IntToStr(0), MSG_NOTIFY
);
1219 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Internal Attrib : $' + IntToHex(0, 4), MSG_NOTIFY
);
1220 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': External Attrib : $' + IntToHex(eattr
, 8), MSG_NOTIFY
);
1221 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': LFH Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
1222 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name : "' + fname
+ '"', MSG_NOTIFY
);
1224 s
.WriteBuffer(ZIP_SIGN_CDR
, 4); // CDR Signature
1225 s
.WriteByte(ZIP_MAXVERSION
); // Used version
1226 s
.WriteByte(ZIP_SYSTEM
); // Used system
1227 s
.WriteByte(version
); // Min version
1228 s
.WriteByte(ZIP_SYSTEM
); // Min system
1229 WriteInt(s
, UInt16(flags
)); // Flags
1230 WriteInt(s
, UInt16(comp
)); // Compression method
1231 WriteInt(s
, UInt32(mtime
)); // Modification time/date
1232 WriteInt(s
, UInt32(crc
)); // CRC-32
1233 WriteInt(s
, UInt32(csize
)); // Compressed size
1234 WriteInt(s
, UInt32(usize
)); // Decompressed size
1235 WriteInt(s
, UInt16(fnlen
)); // Name field length
1236 WriteInt(s
, UInt16(0)); // Extra field length
1237 WriteInt(s
, UInt16(0)); // Comment field length
1238 WriteInt(s
, UInt16(0)); // Disk
1239 WriteInt(s
, UInt16(0)); // Internal attributes
1240 WriteInt(s
, UInt32(eattr
)); // External attributes
1241 WriteInt(s
, UInt32(offset
)); // LFH offset
1242 s
.WriteBuffer(fname
[0], fnlen
); // File Name
1245 procedure TZIPEditor
.SaveToStream(s
: TStream
);
1247 var start
, offset
, loffset
, size
, zcrc
, count
: UInt32
;
1249 var afname
: AnsiString;
1252 // Write LFH headers and data
1253 start
:= s
.Position
;
1254 zcrc
:= crc32(0, nil, 0);
1255 if FSection
<> nil then
1257 for i
:= 0 to High(FSection
) do
1259 if FSection
[i
].list
<> nil then
1261 for j
:= 0 to High(FSection
[i
].list
) do
1263 p
:= @FSection
[i
].list
[j
];
1264 afname
:= GetFileName(FSection
[i
].name
, p
.name
);
1265 WriteLFH(s
, p
.comp
, p
.mtime
, p
.chksum
, p
.csize
, p
.usize
, afname
);
1266 if p
.stream
<> nil then
1268 Assert(p
.stream
.Size
= p
.csize
);
1269 p
.stream
.SaveToStream(s
);
1271 else if FStream
<> nil then
1273 FStream
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
1274 s
.CopyFrom(FStream
, p
.csize
);
1278 raise Exception
.Create('No data source available (somethig very wrong)');
1284 afname
:= GetFileName(FSection
[i
].name
, '');
1285 WriteLFH(s
, ZIP_COMP_STORE
, FSection
[i
].mtime
, zcrc
, 0, 0, afname
);
1289 // Write CDR headers
1292 offset
:= s
.Position
- start
;
1293 if FSection
<> nil then
1295 for i
:= 0 to High(FSection
) do
1297 if FSection
[i
].list
<> nil then
1299 for j
:= 0 to High(FSection
[i
].list
) do
1301 p
:= @FSection
[i
].list
[j
];
1302 afname
:= GetFileName(FSection
[i
].name
, p
.name
);
1303 WriteCDR(s
, p
.comp
, p
.mtime
, p
.chksum
, p
.csize
, p
.usize
, $00, loffset
, afname
, i
);
1304 loffset
:= loffset
+ 30 + Length(afname
) + p
.csize
;
1310 afname
:= GetFileName(FSection
[i
].name
, '');
1311 WriteCDR(s
, ZIP_COMP_STORE
, FSection
[i
].mtime
, zcrc
, 0, 0, $10, loffset
, afname
, i
);
1312 loffset
:= loffset
+ 30 + Length(afname
) + 0;
1317 Assert(loffset
= offset
);
1318 Assert(count
< $ffff);
1319 size
:= s
.Position
- start
- offset
;
1320 // Write EOCD header
1321 mypos
:= s
.Position
;
1322 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1324 e_WriteLog('==============================================', MSG_NOTIFY
);
1325 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID : ' + IntToStr(0), MSG_NOTIFY
);
1326 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID with CD : ' + IntToStr(0), MSG_NOTIFY
);
1327 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Available CDR''s : ' + IntToStr(count
), MSG_NOTIFY
);
1328 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Total CDR''s : ' + IntToStr(count
), MSG_NOTIFY
);
1329 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Length : ' + IntToStr(size
), MSG_NOTIFY
);
1330 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
1331 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(0), MSG_NOTIFY
);
1332 e_WriteLog('==============================================', MSG_NOTIFY
);
1334 s
.WriteBuffer(ZIP_SIGN_EOCD
, 4); // EOCD Signature
1335 WriteInt(s
, UInt16(0)); // Disk
1336 WriteInt(s
, UInt16(0)); // Num of Disks
1337 WriteInt(s
, UInt16(count
)); // Num of CDRs
1338 WriteInt(s
, UInt16(count
)); // Total CDR entries
1339 WriteInt(s
, UInt32(size
)); // Central Directory size
1340 WriteInt(s
, UInt32(offset
)); // Central Directory offset
1341 WriteInt(s
, UInt16(0)); // Comment field length
1344 procedure TZIPEditor
.SaveTo(FileName
: String);
1348 s
:= TFileStream
.Create(FileName
, fmCreate
);
1357 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1358 e_WriteLog('ZIP: Failed to create file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
1364 function TZIPEditor
.GetLastError
: Integer;
1366 Result
:= FLastError
;
1369 function TZIPEditor
.GetLastErrorStr
: String;
1372 DFWAD_NOERROR
: Result
:= '';
1373 DFWAD_ERROR_WADNOTFOUND
: Result
:= 'DFZIP file not found';
1374 DFWAD_ERROR_CANTOPENWAD
: Result
:= 'Can''t open DFZIP file';
1375 DFWAD_ERROR_RESOURCENOTFOUND
: Result
:= 'Resource not found';
1376 DFWAD_ERROR_FILENOTWAD
: Result
:= 'File is not DFZIP';
1377 DFWAD_ERROR_WADNOTLOADED
: Result
:= 'DFZIP file is not loaded';
1378 DFWAD_ERROR_READRESOURCE
: Result
:= 'Read resource error';
1379 DFWAD_ERROR_READWAD
: Result
:= 'Read DFZIP error';
1380 otherwise Result
:= IntToStr(FLastError
);
1384 function TZIPEditor
.GetResourcesCount
: Word;
1388 if FSection
<> nil then
1390 Result
:= Result
+ Length(FSection
);
1391 for i
:= 0 to High(FSection
) do
1392 if FSection
[i
].list
<> nil then
1393 Result
:= Result
+ Length(FSection
[i
].list
);
1397 function TZIPEditor
.GetVersion
: Byte;
1403 gWADEditorFactory
.RegisterEditor('DFZIP', TZIPEditor
);