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 // - UTF-8 not supported yet, expected WIN1251 encoding
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
;
29 stream
: TMemoryStream
;
34 list
: array of TResource
;
37 PResource
= ^TResource
;
40 TZIPEditor
= class sealed(WADEDITOR
.TWADEditor
)
42 FSection
: array of TSection
;
47 function FindSectionIDRAW(name
: AnsiString
; caseSensitive
: Boolean): Integer;
48 function FindSectionRAW(name
: AnsiString
; caseSensitive
: Boolean): PSection
;
49 function InsertSectionRAW(name
: AnsiString
): PSection
;
51 function FindSectionID(name
: AnsiString
): Integer;
52 function FindSection(name
: AnsiString
): PSection
;
53 function InsertSection(name
: AnsiString
): PSection
;
55 function InsertFileInfo(const section
, name
: AnsiString
; pos
, csize
, usize
, comp
, crc
: UInt32
): PResource
;
56 function Preload(p
: PResource
): Boolean;
57 function GetSourceStream(p
: PResource
): TStream
;
59 procedure ReadLFH(s
: TStream
; fname
: AnsiString
; xcsize
, xusize
, xcomp
, xcrc
: UInt32
);
60 procedure ReadCDR(s
: TStream
; cdrid
: Integer);
61 function FindEOCD(s
: TStream
): Boolean;
62 procedure ReadEOCD(s
: TStream
);
64 procedure WriteLFH(s
: TStream
; comp
, crc
, csize
, usize
: UInt32
; const afname
: AnsiString
);
65 procedure WriteCDR(s
: TStream
; comp
, crc
, csize
, usize
, eattr
, offset
: UInt32
; const afname
: AnsiString
; cdrid
: Integer);
66 procedure SaveToStream(s
: TStream
);
70 destructor Destroy(); override;
71 procedure FreeWAD(); override;
72 function ReadFile2(FileName
: string): Boolean; override;
73 function ReadMemory(Data
: Pointer; Len
: LongWord
): Boolean; override;
74 procedure CreateImage(); override;
75 function AddResource(Data
: Pointer; Len
: LongWord
; Name
, Section
: String): Boolean; override; overload
;
76 function AddResource(FileName
, Name
, Section
: String): Boolean; override; overload
;
77 function AddAlias(Res
, Alias
: String): Boolean; override;
78 procedure AddSection(Name
: String); override;
79 procedure RemoveResource(Section
, Resource
: String); override;
80 procedure SaveTo(FileName
: String); override;
81 function HaveResource(Section
, Resource
: String): Boolean; override;
82 function HaveSection(Section
: string): Boolean; override;
83 function GetResource(Section
, Resource
: String; var pData
: Pointer; var Len
: Integer): Boolean; override;
84 function GetSectionList(): SArray
; override;
85 function GetResourcesList(Section
: String): SArray
; override;
87 function GetLastError
: Integer; override;
88 function GetLastErrorStr
: String; override;
89 function GetResourcesCount
: Word; override;
90 function GetVersion
: Byte; override;
95 uses SysUtils
, StrUtils
, Math
, utils
, zstream
, crc
, e_log
;
98 ZIP_SIGN_CDR
= 'PK'#1#2;
99 ZIP_SIGN_LFH
= 'PK'#3#4;
100 ZIP_SIGN_EOCD
= 'PK'#5#6;
105 ZIP_COMP_REDUCE1
= 2;
106 ZIP_COMP_REDUCE2
= 3;
107 ZIP_COMP_REDUCE3
= 4;
108 ZIP_COMP_REDUCE4
= 5;
109 ZIP_COMP_IMPLODE
= 6;
110 ZIP_COMP_TOKENIZED
= 7;
111 ZIP_COMP_DEFLATE
= 8;
112 ZIP_COMP_DEFLATE64
= 9;
113 ZIP_COMP_TERSE1
= 10;
117 ZIP_COMP_TERSE2
= 18;
124 ZIP_COMP_WAVPACK
= 97;
129 ZIP_SYSTEM
= 0; // DOS / FAT
130 ZIP_MAXVERSION
= 63; // Max supported version
133 ZIP_ENCRYPTION_MASK
= (1 << 0) or (1 << 6) or (1 << 13);
134 ZIP_UTF8_MASK
= (1 << 11);
136 procedure ToSectionFile(fname
: AnsiString
; out section
, name
: AnsiString
); inline;
139 i
:= LastDelimiter('/', fname
);
140 section
:= Copy(fname
, 1, i
- 1);
141 name
:= Copy(fname
, i
+ 1)
144 function GetFileName(const Section
, Name
: AnsiString
): AnsiString
; inline;
149 Result
:= Section
+ '/' + Name
;
152 function PrepString(const s
: AnsiString
; caseSensitive
, extSensitive
: Boolean): AnsiString
; inline;
156 if caseSensitive
= False then
158 Result
:= UpperCase(Result
);
160 if extSensitive
= False then
162 i
:= Pos('.', Result
); // fix dotfiles
164 SetLength(Result
, i
- 1);
168 function FindResourceIDRAW(p
: PSection
; name
: AnsiString
; caseSensitive
, extSensitive
: Boolean): Integer;
169 var i
: Integer; pname
: AnsiString
;
173 pname
:= PrepString(name
, caseSensitive
, extSensitive
);
174 for i
:= 0 to High(p
.list
) do
176 if PrepString(p
.list
[i
].name
, caseSensitive
, extSensitive
) = pname
then
186 function FindResourceID(p
: PSection
; name
: AnsiString
): Integer;
189 i
:= FindResourceIDRAW(p
, name
, True, True); // CaSeNaMe.Ext
192 i
:= FindResourceIDRAW(p
, name
, False, True); // CASENAME.EXT
195 i
:= FindResourceIDRAW(p
, name
, True, False); // CaSeNaMe
198 i
:= FindResourceIDRAW(p
, name
, False, False); // CASENAME
205 function FindResource(p
: PSection
; name
: AnsiString
): PResource
;
208 i
:= FindResourceID(p
, name
);
217 function TZIPEditor
.FindSectionIDRAW(name
: AnsiString
; caseSensitive
: Boolean): Integer;
218 var i
: Integer; pname
: AnsiString
;
220 if FSection
<> nil then
222 pname
:= PrepString(name
, caseSensitive
, True);
223 for i
:= 0 to High(FSection
) do
225 if PrepString(FSection
[i
].name
, caseSensitive
, True) = pname
then
235 function TZIPEditor
.FindSectionRAW(name
: AnsiString
; caseSensitive
: Boolean): PSection
;
238 i
:= FindSectionIDRAW(name
, caseSensitive
);
240 Result
:= @FSection
[i
]
245 function TZIPEditor
.InsertSectionRAW(name
: AnsiString
): PSection
;
248 if FSection
= nil then i
:= 0 else i
:= Length(FSection
);
249 SetLength(FSection
, i
+ 1);
250 FSection
[i
] := Default(TSection
);
251 FSection
[i
].name
:= name
;
252 Result
:= @FSection
[i
];
257 function TZIPEditor
.FindSectionID(name
: AnsiString
): Integer;
258 var fixName
: AnsiString
;
260 fixName
:= StringReplace(name
, '\', '/', [rfReplaceAll
], TStringReplaceAlgorithm
.sraManySmall
);
261 Result
:= FindSectionIDRAW(fixName
, True); // CaSeNaMe
263 Result
:= FindSectionIDRAW(fixName
, False); // CASENAME
266 function TZIPEditor
.FindSection(name
: AnsiString
): PSection
;
267 var fixName
: AnsiString
;
269 fixName
:= StringReplace(name
, '\', '/', [rfReplaceAll
], TStringReplaceAlgorithm
.sraManySmall
);
270 Result
:= FindSectionRAW(fixName
, True); // CaSeNaMe
272 Result
:= FindSectionRAW(fixName
, False); // CASENAME
275 function TZIPEditor
.InsertSection(name
: AnsiString
): PSection
;
277 Result
:= FindSection(name
);
279 Result
:= InsertSectionRAW(name
);
284 function TZIPEditor
.InsertFileInfo(const section
, name
: AnsiString
; pos
, csize
, usize
, comp
, crc
: UInt32
): PResource
;
285 var p
: PSection
; i
: Integer;
287 p
:= FindSectionRAW(section
, True);
289 p
:= InsertSectionRAW(section
);
290 if p
.list
= nil then i
:= 0 else i
:= Length(p
.list
);
291 SetLength(p
.list
, i
+ 1);
292 p
.list
[i
] := Default(TResource
);
293 p
.list
[i
].name
:= name
;
294 p
.list
[i
].pos
:= pos
;
295 p
.list
[i
].csize
:= csize
;
296 p
.list
[i
].usize
:= usize
;
297 p
.list
[i
].comp
:= comp
;
298 p
.list
[i
].chksum
:= crc
;
299 p
.list
[i
].stream
:= nil;
300 Result
:= @p
.list
[i
];
305 function TZIPEditor
.AddAlias(Res
, Alias
: String): Boolean;
307 // Hard-links not supported in ZIP
308 // However, they never created by editor
312 function TZIPEditor
.AddResource(Data
: Pointer; Len
: LongWord
; Name
, Section
: String): Boolean;
313 const compress
: Boolean = True;
314 const level
: TCompressionLevel
= TCompressionLevel
.clMax
;
315 var s
: TMemoryStream
; cs
: TCompressionStream
; p
: PResource
;
316 var comp
, crc
: UInt32
;
321 s
:= TMemoryStream
.Create();
323 if compress
and (Len
> 0) then
325 cs
:= TCompressionStream
.Create(level
, s
, True);
327 cs
.WriteBuffer(PByte(Data
)[0], Len
);
329 comp
:= ZIP_COMP_DEFLATE
;
334 if (Len
= 0) or (compress
= False) or (s
.Size
>= Len
) then
336 s
.Seek(0, TSeekOrigin
.soBeginning
);
338 s
.WriteBuffer(PByte(Data
)[0], Len
);
339 comp
:= ZIP_COMP_STORE
;
340 Assert(s
.Size
= Len
);
342 crc
:= crc32(0, nil, 0);
343 crc
:= crc32(crc
, data
, len
);
344 p
:= InsertFileInfo(Section
, Name
, $ffffffff, s
.Size
, Len
, comp
, crc
);
354 function TZIPEditor
.AddResource(FileName
, Name
, Section
: String): Boolean;
355 var s
: TFileStream
; ptr
: PByte
;
358 FLastError
:= DFWAD_ERROR_READWAD
;
360 s
:= TFileStream
.Create(FileName
, fmOpenRead
or fmShareDenyWrite
);
364 s
.ReadBuffer(ptr
[0], s
.Size
);
365 Result
:= AddResource(ptr
, s
.Size
, Name
, Section
);
366 if Result
= True then FLastError
:= DFWAD_NOERROR
;
376 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
377 e_WriteLog('DFZIP: AddResource: failed to open file ' + FileName
, MSG_NOTIFY
);
378 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
383 constructor TZIPEditor
.Create();
387 FLastError
:= DFWAD_NOERROR
;
392 destructor TZIPEditor
.Destroy();
398 procedure TZIPEditor
.FreeWAD();
401 if FSection
<> nil then
403 for i
:= 0 to High(FSection
) do
405 if FSection
[i
].list
<> nil then
407 for j
:= 0 to High(FSection
[i
].list
) do
409 if FSection
[i
].list
[j
].stream
<> nil then
411 FreeAndNil(FSection
[i
].list
[j
].stream
);
414 SetLength(FSection
[i
].list
, 0);
417 SetLength(FSection
, 0);
419 if FStream
<> nil then
423 FLastError
:= DFWAD_NOERROR
;
427 function TZIPEditor
.Preload(p
: PResource
): Boolean;
428 var s
: TMemoryStream
;
433 Result
:= p
.stream
<> nil;
434 if (p
.stream
= nil) and (FStream
<> nil) then
436 s
:= TMemoryStream
.Create();
440 FStream
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
441 s
.CopyFrom(FStream
, p
.csize
);
443 Assert(s
.Size
= p
.csize
); // wtf, random size if copied zero bytes!
453 procedure TZIPEditor
.CreateImage();
456 if FStream
= nil then
458 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
459 e_WriteLog('DFZIP: CreateImage: File not assigned', MSG_NOTIFY
);
460 FLastError
:= DFWAD_ERROR_WADNOTLOADED
;
462 else if FStream
is TMemoryStream
then
464 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
465 e_WriteLog('DFZIP: CreateImage: Memory stream', MSG_NOTIFY
);
466 FLastError
:= DFWAD_NOERROR
;
470 if FSection
<> nil then
472 for i
:= 0 to High(FSection
) do
474 if FSection
[i
].list
<> nil then
476 for j
:= 0 to High(FSection
[i
].list
) do
478 if Preload(@FSection
[i
].list
[j
]) = False then
480 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
481 e_WriteLog('DFZIP: CreateImage: failed to preload resource [' + FSection
[i
].name
+ '][' + FSection
[i
].list
[j
].name
+ ']', MSG_WARNING
);
482 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
490 FLastError
:= DFWAD_NOERROR
;
494 procedure TZIPEditor
.AddSection(Name
: String);
496 if InsertSection(Name
) = nil then
497 raise Exception
.Create('DFZIP: AddSection[' + Name
+ ']: failed to insert');
500 function TZIPEditor
.HaveResource(Section
, Resource
: String): Boolean;
502 Result
:= FindResource(FindSection(Section
), Resource
) <> nil;
505 function TZIPEditor
.HaveSection(Section
: String): Boolean;
507 Result
:= FindSection(Section
) <> nil;
510 function TZIPEditor
.GetSourceStream(p
: PResource
): TStream
;
514 if p
.stream
<> nil then
517 src
.Seek(0, TSeekOrigin
.soBeginning
);
519 else if FStream
<> nil then
522 src
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
527 function TZIPEditor
.GetResource(Section
, Resource
: String; var pData
: Pointer; var Len
: Integer): Boolean;
528 var p
: PResource
; ptr
: PByte
; src
: TStream
; tmp
: TDecompressionStream
; crc
: UInt32
;
530 FLastError
:= DFWAD_ERROR_CANTOPENWAD
;
534 p
:= FindResource(FindSection(Section
), Resource
);
537 src
:= GetSourceStream(p
);
543 Assert(p
.csize
= p
.usize
);
544 GetMem(ptr
, p
.usize
);
547 src
.ReadBuffer(ptr
[0], p
.usize
);
553 except on e
: EReadError
do
554 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
555 e_WriteLog('DFZIP: Failed to read STOREd data, reason: ' + e
.Message, MSG_WARNING
);
560 tmp
:= TDecompressionStream
.Create(src
, True);
562 GetMem(ptr
, p
.usize
);
564 tmp
.ReadBuffer(ptr
[0], p
.usize
);
574 on e
: EStreamError
do
576 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
577 e_WriteLog('DFZIP: Failed to decompress DEFLATEd data, reason: ' + e
.Message, MSG_WARNING
);
582 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
583 e_WriteLog('DFZIP: Unsupported compression method: ' + IntToStr(p
.comp
), MSG_WARNING
);
588 if gWADEditorLogLevel
>= DFWAD_LOG_WARN
then
589 e_WriteLog('DFZIP: No available source for file data', MSG_WARNING
);
590 FLastError
:= DFWAD_ERROR_WADNOTLOADED
;
592 if Result
= True then
594 crc
:= crc32(0, nil, 0);
595 crc
:= crc32(crc
, ptr
, p
.usize
);
596 Result
:= crc
= p
.chksum
;
597 if Result
= True then
601 FLastError
:= DFWAD_NOERROR
;
605 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
606 e_WriteLog('DFZIP: File integrity check failed: expected CRC32 $' + IntToHex(p
.chksum
, 8) + ', calculated CRC32 $' + IntToHex(crc
, 8), MSG_WARNING
);
613 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
614 e_WriteLog('DFZIP: Resource not found', MSG_NOTIFY
);
615 FLastError
:= DFWAD_ERROR_RESOURCENOTFOUND
;
619 function TZIPEditor
.GetResourcesList(Section
: String): SArray
;
620 var p
: PSection
; i
: Integer;
623 p
:= FindSection(Section
);
624 if (p
<> nil) and (p
.list
<> nil) then
626 SetLength(Result
, Length(p
.list
));
627 for i
:= 0 to High(p
.list
) do
629 Result
[i
] := p
.list
[i
].name
;
634 function TZIPEditor
.GetSectionList(): SArray
;
638 if FSection
<> nil then
640 SetLength(Result
, Length(FSection
));
641 for i
:= 0 to High(FSection
) do
643 Result
[i
] := FSection
[i
].name
;
648 procedure TZIPEditor
.ReadLFH(s
: TStream
; fname
: AnsiString
; xcsize
, xusize
, xcomp
, xcrc
: UInt32
);
649 var sig
: packed array [0..3] of Char;
650 var va
, vb
, flags
, comp
: UInt16
;
651 var mtime
, crc
, csize
, usize
: UInt32
;
652 var fnlen
, extlen
: UInt16
;
653 var mypos
, datapos
: UInt64
;
654 var section
, name
: AnsiString
;
658 if mypos
+ 30 <= s
.Size
then
660 s
.ReadBuffer(sig
[0], 4);
661 if sig
= ZIP_SIGN_LFH
then
663 va
:= s
.ReadByte(); // Min Version
664 vb
:= s
.ReadByte(); // Min System
665 flags
:= LEtoN(s
.ReadWord());
666 comp
:= LEtoN(s
.ReadWord());
667 mtime
:= LEtoN(s
.ReadDWord());
668 crc
:= LEtoN(s
.ReadDWord());
669 csize
:= LEtoN(s
.ReadDWord());
670 usize
:= LEtoN(s
.ReadDWord());
671 fnlen
:= LEtoN(s
.ReadWord());
672 extlen
:= LEtoN(s
.ReadWord());
673 datapos
:= s
.Position
+ fnlen
+ extlen
;
674 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
676 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(va
), MSG_NOTIFY
);
677 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(vb
), MSG_NOTIFY
);
678 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
679 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
680 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Modification Time : $' + IntToHex(mtime
, 8), MSG_NOTIFY
);
681 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
682 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
683 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
684 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
685 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(extlen
), MSG_NOTIFY
);
686 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': <DATA OFFSET> : $' + IntToHex(datapos
, 8), MSG_NOTIFY
);
688 if (va
>= 10) and (va
<= ZIP_MAXVERSION
) then
690 if datapos
+ xcsize
<= s
.Size
then
692 ToSectionFile(fname
, section
, name
);
695 p
:= FindSectionRAW(section
, True);
697 p
:= InsertSectionRAW(section
)
701 p
:= InsertFileInfo(section
, name
, datapos
, xcsize
, xusize
, xcomp
, xcrc
);
704 raise Exception
.Create('Failed to register resource [' + fname
+ ']');
707 raise Exception
.Create('Invalid LFH size (corrupted file?)');
711 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
712 raise Exception
.Create('Unsupported CDR version ' + IntToStr(va
) + ', not in range [10..' + IntToStr(ZIP_MAXVERSION
) + ']');
716 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?)');
719 raise Exception
.Create('Invalid LFH size (corrupted file?)');
722 procedure TZIPEditor
.ReadCDR(s
: TStream
; cdrid
: Integer);
723 var sig
: packed array [0..3] of Char;
724 var vva
, vvb
, va
, vb
, flags
, comp
: UInt16
;
725 var mtime
, crc
, csize
, usize
: UInt32
;
726 var fnlen
, extlen
, comlen
, disk
, iattr
: UInt16
;
727 var eattr
, offset
: UInt32
;
728 var mypos
, next
: UInt64
;
730 var aname
: AnsiString
;
733 s
.ReadBuffer(sig
[0], 4);
734 if sig
= ZIP_SIGN_CDR
then
736 // Valid Central Directory Signature
737 vva
:= s
.ReadByte(); // Writer Version
738 vvb
:= s
.ReadByte(); // Writer System
739 va
:= s
.ReadByte(); // Min Version
740 vb
:= s
.ReadByte(); // Min System
741 flags
:= LEtoN(s
.ReadWord());
742 comp
:= LEtoN(s
.ReadWord());
743 mtime
:= LEtoN(s
.ReadDWord());
744 crc
:= LEtoN(s
.ReadDWord());
745 csize
:= LEtoN(s
.ReadDWord());
746 usize
:= LEtoN(s
.ReadDWord());
747 fnlen
:= LEtoN(s
.ReadWord());
748 extlen
:= LEtoN(s
.ReadWord());
749 comlen
:= LEtoN(s
.ReadWord());
750 disk
:= LEtoN(s
.ReadWord());
751 iattr
:= LEtoN(s
.ReadWord());
752 eattr
:= LEtoN(s
.ReadDWord());
753 offset
:= LEtoN(s
.ReadDWord());
754 next
:= s
.Position
+ fnlen
+ extlen
+ comlen
;
756 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
758 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer Version : ' + IntToStr(vva
), MSG_NOTIFY
);
759 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer System : ' + IntToStr(vvb
), MSG_NOTIFY
);
760 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(va
), MSG_NOTIFY
);
761 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(vb
), MSG_NOTIFY
);
762 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
763 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
764 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Modification Time : $' + IntToHex(mtime
, 8), MSG_NOTIFY
);
765 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
766 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
767 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
768 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
769 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(extlen
), MSG_NOTIFY
);
770 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(comlen
), MSG_NOTIFY
);
771 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Disk : ' + IntToStr(disk
), MSG_NOTIFY
);
772 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Internal Attrib : $' + IntToHex(iattr
, 4), MSG_NOTIFY
);
773 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': External Attrib : $' + IntToHex(eattr
, 8), MSG_NOTIFY
);
774 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': LFH Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
776 if (va
>= 10) and (va
<= ZIP_MAXVERSION
) then
778 if (flags
and ZIP_ENCRYPTION_MASK
) = 0 then
780 if (csize
<> $ffffffff) and (usize
<> $ffffffff) and (disk
<> $ffff) and (offset
<> $ffffffff) then
784 if (next
<= s
.Size
) and (fnlen
> 0) then
788 if csize
<> usize
then
789 raise Exception
.Create('Compressed size ' + IntToStr(csize
) + ' != Descompressed size ' + IntToStr(usize
) + 'for STORE method (corrupted file?)');
813 raise Exception
.Create('Encrypted archives not supported');
815 raise Exception
.Create('Unknown compression method ' + IntToStr(comp
));
817 GetMem(name
, UInt32(fnlen
) + 1);
819 s
.ReadBuffer(name
[0], fnlen
);
821 // TODO: when packer version < 63 detect utf8 by hands?
822 if flags
and ZIP_UTF8_MASK
= 0 then
823 aname
:= win2utf(name
)
826 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
827 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name : "' + aname
+ '"', MSG_NOTIFY
);
828 s
.Seek(offset
, TSeekOrigin
.soBeginning
);
829 ReadLFH(s
, aname
, csize
, usize
, comp
, crc
);
831 s
.Seek(next
, TSeekOrigin
.soBeginning
);
836 raise Exception
.Create('Empty files names not supported');
839 raise Exception
.Create('Splitted archives not supported');
843 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
844 raise Exception
.Create('ZIP64 not supported');
849 FLastError
:= DFWAD_ERROR_READWAD
;
850 raise Exception
.Create('Encrypted archives not supported');
855 FLastError
:= DFWAD_ERROR_WRONGVERSION
;
856 raise Exception
.Create('Unsupported CDR version ' + IntToStr(va
) + ', not in range [10..' + IntToStr(ZIP_MAXVERSION
) + ']');
860 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?)');
863 function TZIPEditor
.FindEOCD(s
: TStream
): Boolean;
864 const maxedir
= 20; // end of central directory entry
865 const maxecdir
= maxedir
+ 65536; // + comment
866 var sig
: packed array [0..3] of Char; off
, lim
: Int64;
869 if s
.Size
>= maxedir
then
871 if s
.Size
< maxecdir
then lim
:= s
.Size
else lim
:= maxecdir
;
872 lim
:= lim
- maxedir
;
874 while (off
<= lim
) and (Result
= False) do
876 s
.Seek(s
.Size
- off
, TSeekOrigin
.soBeginning
);
877 s
.ReadBuffer(sig
[0], 4);
878 Result
:= sig
= ZIP_SIGN_EOCD
;
884 procedure TZIPEditor
.ReadEOCD(s
: TStream
);
885 var sig
: packed array [0..3] of Char;
886 var idisk
, ndisk
, nrec
, total
, comlen
: UInt16
;
887 var csize
, cpos
, i
: UInt32
;
890 FLastError
:= DFWAD_ERROR_FILENOTWAD
;
892 s
.ReadBuffer(sig
[0], 4);
893 if (sig
= ZIP_SIGN_LFH
) or (sig
= ZIP_SIGN_EOCD
) then
897 // End of Central Directory found
898 FLastError
:= DFWAD_ERROR_READWAD
;
899 mypos
:= s
.Position
- 4;
900 idisk
:= LEtoN(s
.ReadWord());
901 ndisk
:= LEtoN(s
.ReadWord());
902 nrec
:= LEtoN(s
.ReadWord());
903 total
:= LEtoN(s
.ReadWord());
904 csize
:= LEtoN(s
.ReadDWord());
905 cpos
:= LEtoN(s
.ReadDWord());
906 comlen
:= LEtoN(s
.ReadWord());
907 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
909 e_WriteLog('==============================================', MSG_NOTIFY
);
910 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID : ' + IntToStr(idisk
), MSG_NOTIFY
);
911 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID with CD : ' + IntToStr(ndisk
), MSG_NOTIFY
);
912 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Available CDR''s : ' + IntToStr(nrec
), MSG_NOTIFY
);
913 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Total CDR''s : ' + IntToStr(total
), MSG_NOTIFY
);
914 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Length : ' + IntToStr(csize
), MSG_NOTIFY
);
915 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Offset : $' + IntToHex(cpos
, 8), MSG_NOTIFY
);
916 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(comlen
), MSG_NOTIFY
);
918 if (idisk
<> $ffff) and (ndisk
<> $ffff) and (nrec
<> $ffff) and (total
<> $ffff) and (csize
<> $ffffffff) and (cpos
<> $ffffffff) then
920 if s
.Position
+ comlen
= s
.Size
then
922 if (idisk
= 0) and (ndisk
= 0) and (nrec
= total
) then
924 if (nrec
* 46 <= csize
) and (UInt64(cpos
) + csize
<= s
.Size
) then
929 s
.Seek(cpos
, TSeekOrigin
.soBeginning
);
932 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
933 e_WriteLog('==============================================', MSG_NOTIFY
);
937 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
938 e_WriteLog('==============================================', MSG_NOTIFY
);
942 raise Exception
.Create('Central Directory too big (corrupted file?)');
945 raise Exception
.Create('Splitted archives not supported');
948 raise Exception
.Create('EOCD too big (corrupted file?)');
951 raise Exception
.Create('ZIP64 not supported');
954 raise Exception
.Create('EOCD not found (corrupted file?)');
957 raise Exception
.Create('Not DFZIP formated file');
960 function TZIPEditor
.ReadFile2(FileName
: String): Boolean;
967 s
:= TFileStream
.Create(FileName
, fmOpenRead
or fmShareDenyWrite
);
971 FLastError
:= DFWAD_NOERROR
;
980 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
981 e_WriteLog('ZIP: Failed to read ZIP from file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
988 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
989 e_WriteLog('DFZIP: Failed to open file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
990 if FileExists(FileName
) then
991 FLastError
:= DFWAD_ERROR_CANTOPENWAD
993 FLastError
:= DFWAD_ERROR_WADNOTFOUND
;
998 function TZIPEditor
.ReadMemory(Data
: Pointer; Len
: LongWord
): Boolean;
999 var s
: TMemoryStream
;
1004 s
:= TMemoryStream
.Create
;
1007 s
.WriteBuffer(PByte(Data
)[0], Len
);
1008 s
.Seek(0, soBeginning
);
1011 FLastError
:= DFWAD_NOERROR
;
1020 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1021 e_WriteLog('DFZIP: Failed to read ZIP from memory, reason: ' + e
.Message, MSG_WARNING
);
1027 procedure TZIPEditor
.RemoveResource(Section
, Resource
: String);
1028 var p
: PSection
; i
: Integer;
1030 p
:= FindSection(Section
);
1031 i
:= FindResourceID(p
, Resource
);
1034 if p
.list
[i
].stream
<> nil then
1035 FreeAndNil(p
.list
[i
].stream
);
1036 for i
:= i
+ 1 to High(p
.list
) do
1038 p
.list
[i
- 1] := p
.list
[i
];
1040 SetLength(p
.list
, High(p
.list
));
1044 function IsASCII(const s
: AnsiString
): Boolean;
1047 for i
:= 1 to Length(s
) do
1049 if s
[i
] >= #
$80 then
1058 function GetZIPVersion(const afname
: AnsiString
; flags
, comp
: UInt16
): UInt8
;
1061 version
:= 10; // Base version
1063 ZIP_COMP_STORE
: version
:= 10;
1064 ZIP_COMP_SHRUNK
: version
:= 10;
1065 ZIP_COMP_REDUCE1
: version
:= 10;
1066 ZIP_COMP_REDUCE2
: version
:= 10;
1067 ZIP_COMP_REDUCE3
: version
:= 10;
1068 ZIP_COMP_REDUCE4
: version
:= 10;
1069 ZIP_COMP_IMPLODE
: version
:= 10;
1070 ZIP_COMP_TOKENIZED
: version
:= 20;
1071 ZIP_COMP_DEFLATE
: version
:= 20;
1072 ZIP_COMP_DEFLATE64
: version
:= 21;
1073 ZIP_COMP_TERSE1
: version
:= 25; // PKWARE DCL Implode
1074 ZIP_COMP_BZIP2
: version
:= 46;
1075 ZIP_COMP_LZMA
: version
:= 63;
1076 ZIP_COMP_CMPSC
: version
:= 63;
1077 ZIP_COMP_TERSE2
: version
:= 63;
1078 ZIP_COMP_LZ77
: version
:= 63;
1079 ZIP_COMP_ZSTD1
: version
:= 63;
1080 ZIP_COMP_ZSTD2
: version
:= 63;
1081 ZIP_COMP_MP3
: version
:= 63;
1082 ZIP_COMP_XZ
: version
:= 63;
1083 ZIP_COMP_JPEG
: version
:= 63;
1084 ZIP_COMP_WAVPACK
: version
:= 63;
1085 ZIP_COMP_PPMD
: version
:= 63;
1086 ZIP_COMP_AE
: version
:= 63;
1088 if afname
[Length(afname
)] = '/' then
1089 version
:= Max(20, version
); // Folder
1090 if flags
and ZIP_UTF8_MASK
<> 0 then
1091 version
:= Max(63, version
); // UTF-8 name
1095 procedure TZIPEditor
.WriteLFH(s
: TStream
; comp
, crc
, csize
, usize
: UInt32
; const afname
: AnsiString
);
1096 var fname
: PChar
; version
: UInt8
; fnlen
, flags
: UInt16
; mypos
: UInt64
;
1098 mypos
:= s
.Position
;
1099 fname
:= PChar(afname
);
1100 fnlen
:= Length(fname
);
1102 if IsASCII(afname
) = False then
1103 flags
:= flags
or ZIP_UTF8_MASK
;
1104 version
:= GetZIPVersion(afname
, flags
, comp
);
1105 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1107 e_WriteLog('==============================================', MSG_NOTIFY
);
1108 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(version
), MSG_NOTIFY
);
1109 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1110 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
1111 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
1112 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Modification Time : $' + IntToHex(0, 8), MSG_NOTIFY
);
1113 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
1114 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
1115 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
1116 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
1117 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(0), MSG_NOTIFY
);
1118 e_WriteLog('LFH @' + IntToHex(mypos
, 8) + ': Name : "' + fname
+ '"', MSG_NOTIFY
);
1120 s
.WriteBuffer(ZIP_SIGN_LFH
, 4); // LFH Signature
1121 s
.WriteByte(version
); // Min version
1122 s
.WriteByte(ZIP_SYSTEM
); // System
1123 WriteInt(s
, UInt16(flags
)); // Flags
1124 WriteInt(s
, UInt16(comp
)); // Compression method
1125 WriteInt(s
, UInt32(0)); // Modification time/date
1126 WriteInt(s
, UInt32(crc
)); // CRC-32
1127 WriteInt(s
, UInt32(csize
)); // Compressed size
1128 WriteInt(s
, UInt32(usize
)); // Decompressed size
1129 WriteInt(s
, UInt16(fnlen
)); // Name field length
1130 WriteInt(s
, UInt16(0)); // Extra field length
1131 s
.WriteBuffer(fname
[0], fnlen
); // File Name
1134 procedure TZIPEditor
.WriteCDR(s
: TStream
; comp
, crc
, csize
, usize
, eattr
, offset
: UInt32
; const afname
: AnsiString
; cdrid
: Integer);
1135 var fname
: PChar
; version
: UInt8
; fnlen
, flags
: UInt16
; mypos
: UInt64
;
1137 mypos
:= s
.Position
;
1138 fname
:= PChar(afname
);
1139 fnlen
:= Length(fname
);
1141 if IsASCII(afname
) = False then
1142 flags
:= flags
or ZIP_UTF8_MASK
;
1143 version
:= GetZIPVersion(afname
, flags
, comp
);
1144 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1146 e_WriteLog('==============================================', MSG_NOTIFY
);
1147 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer Version : ' + IntToStr(ZIP_MAXVERSION
), MSG_NOTIFY
);
1148 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Writer System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1149 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min Version : ' + IntToStr(version
), MSG_NOTIFY
);
1150 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Min System : ' + IntToStr(ZIP_SYSTEM
), MSG_NOTIFY
);
1151 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Flags : $' + IntToHex(flags
, 4), MSG_NOTIFY
);
1152 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compression : ' + IntToStr(comp
), MSG_NOTIFY
);
1153 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Modification Time : $' + IntToHex(0, 8), MSG_NOTIFY
);
1154 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': CRC-32 : $' + IntToHex(crc
, 8), MSG_NOTIFY
);
1155 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Compressed size : ' + IntToStr(csize
), MSG_NOTIFY
);
1156 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Decompressed size : ' + IntToStr(usize
), MSG_NOTIFY
);
1157 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name Length : ' + IntToStr(fnlen
), MSG_NOTIFY
);
1158 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Extension Length : ' + IntToStr(0), MSG_NOTIFY
);
1159 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(0), MSG_NOTIFY
);
1160 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Disk : ' + IntToStr(0), MSG_NOTIFY
);
1161 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Internal Attrib : $' + IntToHex(0, 4), MSG_NOTIFY
);
1162 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': External Attrib : $' + IntToHex(eattr
, 8), MSG_NOTIFY
);
1163 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': LFH Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
1164 e_WriteLog('CDR#' + IntToStr(cdrid
) + ' @' + IntToHex(mypos
, 8) + ': Name : "' + fname
+ '"', MSG_NOTIFY
);
1166 s
.WriteBuffer(ZIP_SIGN_CDR
, 4); // CDR Signature
1167 s
.WriteByte(ZIP_MAXVERSION
); // Used version
1168 s
.WriteByte(ZIP_SYSTEM
); // Used system
1169 s
.WriteByte(version
); // Min version
1170 s
.WriteByte(ZIP_SYSTEM
); // Min system
1171 WriteInt(s
, UInt16(flags
)); // Flags
1172 WriteInt(s
, UInt16(comp
)); // Compression method
1173 WriteInt(s
, UInt32(0)); // Modification time/date
1174 WriteInt(s
, UInt32(crc
)); // CRC-32
1175 WriteInt(s
, UInt32(csize
)); // Compressed size
1176 WriteInt(s
, UInt32(usize
)); // Decompressed size
1177 WriteInt(s
, UInt16(fnlen
)); // Name field length
1178 WriteInt(s
, UInt16(0)); // Extra field length
1179 WriteInt(s
, UInt16(0)); // Comment field length
1180 WriteInt(s
, UInt16(0)); // Disk
1181 WriteInt(s
, UInt16(0)); // Internal attributes
1182 WriteInt(s
, UInt32(eattr
)); // External attributes
1183 WriteInt(s
, UInt32(offset
)); // LFH offset
1184 s
.WriteBuffer(fname
[0], fnlen
); // File Name
1187 procedure TZIPEditor
.SaveToStream(s
: TStream
);
1189 var start
, offset
, loffset
, size
, zcrc
, count
: UInt32
;
1191 var afname
: AnsiString
;
1194 // Write LFH headers and data
1195 start
:= s
.Position
;
1196 zcrc
:= crc32(0, nil, 0);
1197 if FSection
<> nil then
1199 for i
:= 0 to High(FSection
) do
1201 if FSection
[i
].list
<> nil then
1203 for j
:= 0 to High(FSection
[i
].list
) do
1205 p
:= @FSection
[i
].list
[j
];
1206 afname
:= GetFileName(FSection
[i
].name
, p
.name
);
1207 WriteLFH(s
, p
.comp
, p
.chksum
, p
.csize
, p
.usize
, afname
);
1208 if p
.stream
<> nil then
1210 Assert(p
.stream
.Size
= p
.csize
);
1211 p
.stream
.SaveToStream(s
);
1213 else if FStream
<> nil then
1215 FStream
.Seek(p
.pos
, TSeekOrigin
.soBeginning
);
1216 s
.CopyFrom(FStream
, p
.csize
);
1220 raise Exception
.Create('No data source available (somethig very wrong)');
1226 afname
:= GetFileName(FSection
[i
].name
, '');
1227 WriteLFH(s
, ZIP_COMP_STORE
, zcrc
, 0, 0, afname
);
1231 // Write CDR headers
1234 offset
:= s
.Position
- start
;
1235 if FSection
<> nil then
1237 for i
:= 0 to High(FSection
) do
1239 if FSection
[i
].list
<> nil then
1241 for j
:= 0 to High(FSection
[i
].list
) do
1243 p
:= @FSection
[i
].list
[j
];
1244 afname
:= GetFileName(FSection
[i
].name
, p
.name
);
1245 WriteCDR(s
, p
.comp
, p
.chksum
, p
.csize
, p
.usize
, $00, loffset
, afname
, i
);
1246 loffset
:= loffset
+ 30 + Length(afname
) + p
.csize
;
1252 afname
:= GetFileName(FSection
[i
].name
, '');
1253 WriteCDR(s
, ZIP_COMP_STORE
, zcrc
, 0, 0, $10, loffset
, afname
, i
);
1254 loffset
:= loffset
+ 30 + Length(afname
) + 0;
1259 Assert(loffset
= offset
);
1260 Assert(count
< $ffff);
1261 size
:= s
.Position
- start
- offset
;
1262 // Write EOCD header
1263 mypos
:= s
.Position
;
1264 if gWADEditorLogLevel
>= DFWAD_LOG_DEBUG
then
1266 e_WriteLog('==============================================', MSG_NOTIFY
);
1267 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID : ' + IntToStr(0), MSG_NOTIFY
);
1268 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Disk ID with CD : ' + IntToStr(0), MSG_NOTIFY
);
1269 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Available CDR''s : ' + IntToStr(count
), MSG_NOTIFY
);
1270 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Total CDR''s : ' + IntToStr(count
), MSG_NOTIFY
);
1271 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Length : ' + IntToStr(size
), MSG_NOTIFY
);
1272 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': CD Offset : $' + IntToHex(offset
, 8), MSG_NOTIFY
);
1273 e_WriteLog('EOCD @' + IntToHex(mypos
, 8) + ': Comment Length : ' + IntToStr(0), MSG_NOTIFY
);
1274 e_WriteLog('==============================================', MSG_NOTIFY
);
1276 s
.WriteBuffer(ZIP_SIGN_EOCD
, 4); // EOCD Signature
1277 WriteInt(s
, UInt16(0)); // Disk
1278 WriteInt(s
, UInt16(0)); // Num of Disks
1279 WriteInt(s
, UInt16(count
)); // Num of CDRs
1280 WriteInt(s
, UInt16(count
)); // Total CDR entries
1281 WriteInt(s
, UInt32(size
)); // Central Directory size
1282 WriteInt(s
, UInt32(offset
)); // Central Directory offset
1283 WriteInt(s
, UInt16(0)); // Comment field length
1286 procedure TZIPEditor
.SaveTo(FileName
: String);
1290 s
:= TFileStream
.Create(FileName
, fmCreate
);
1299 if gWADEditorLogLevel
>= DFWAD_LOG_INFO
then
1300 e_WriteLog('ZIP: Failed to create file ' + FileName
+ ', reason: ' + e
.Message, MSG_WARNING
);
1306 function TZIPEditor
.GetLastError
: Integer;
1308 Result
:= FLastError
;
1311 function TZIPEditor
.GetLastErrorStr
: String;
1314 DFWAD_NOERROR
: Result
:= '';
1315 DFWAD_ERROR_WADNOTFOUND
: Result
:= 'DFZIP file not found';
1316 DFWAD_ERROR_CANTOPENWAD
: Result
:= 'Can''t open DFZIP file';
1317 DFWAD_ERROR_RESOURCENOTFOUND
: Result
:= 'Resource not found';
1318 DFWAD_ERROR_FILENOTWAD
: Result
:= 'File is not DFZIP';
1319 DFWAD_ERROR_WADNOTLOADED
: Result
:= 'DFZIP file is not loaded';
1320 DFWAD_ERROR_READRESOURCE
: Result
:= 'Read resource error';
1321 DFWAD_ERROR_READWAD
: Result
:= 'Read DFZIP error';
1322 otherwise Result
:= IntToStr(FLastError
);
1326 function TZIPEditor
.GetResourcesCount
: Word;
1330 if FSection
<> nil then
1332 Result
:= Result
+ Length(FSection
);
1333 for i
:= 0 to High(FSection
) do
1334 if FSection
[i
].list
<> nil then
1335 Result
:= Result
+ Length(FSection
[i
].list
);
1339 function TZIPEditor
.GetVersion
: Byte;
1345 gWADEditorFactory
.RegisterEditor('DFZIP', TZIPEditor
);