Invalid EndOfCentralDirectory Record generated for Zip64 archive with over 65,535 files in ZipWriter #602

Open
opened 2026-01-29 22:14:25 +00:00 by claunia · 0 comments
Owner

Originally created by @DannyBoyk on GitHub (Dec 22, 2023).

Discovered today that Zip64 files generated by the ZipWriter with over 65,535 files generates an invalid EndOfCentralDirectory record. When it goes to write the Number of central directory records on this disk (or 0xffff for ZIP64) and Total number of central directory records (or 0xffff for ZIP64) fields (see https://en.wikipedia.org/wiki/ZIP_(file_format)#End_of_central_directory_record_(EOCD)), it does not write 0xffff when the number of files exceeds ushort.MaxValue.

This line:
https://github.com/adamhathcock/sharpcompress/blob/master/src/SharpCompress/Writers/Zip/ZipWriter.cs#L307

Needs to be changed to:
BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)(entries.Count < 0xFFFF ? entries.Count : 0xFFFF));

As is, 7zip complains of a "Header error" for the EOCD record as written. And some other zip utilities refuse to process the zip with an error.

Originally created by @DannyBoyk on GitHub (Dec 22, 2023). Discovered today that Zip64 files generated by the ZipWriter with over 65,535 files generates an invalid EndOfCentralDirectory record. When it goes to write the `Number of central directory records on this disk (or 0xffff for ZIP64)` and `Total number of central directory records (or 0xffff for ZIP64) ` fields (see https://en.wikipedia.org/wiki/ZIP_(file_format)#End_of_central_directory_record_(EOCD)), it does not write 0xffff when the number of files exceeds ushort.MaxValue. This line: https://github.com/adamhathcock/sharpcompress/blob/master/src/SharpCompress/Writers/Zip/ZipWriter.cs#L307 Needs to be changed to: ` BinaryPrimitives.WriteUInt16LittleEndian(intBuf, (ushort)(entries.Count < 0xFFFF ? entries.Count : 0xFFFF));` As is, 7zip complains of a "Header error" for the EOCD record as written. And some other zip utilities refuse to process the zip with an error.
claunia added the bug label 2026-01-29 22:14:25 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#602