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

Closed
opened 2026-01-29 22:14:32 +00:00 by claunia · 4 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:32 +00:00
Author
Owner

@adamhathcock commented on GitHub (Dec 22, 2023):

Do you mind making that a PR? Thanks!

@adamhathcock commented on GitHub (Dec 22, 2023): Do you mind making that a PR? Thanks!
Author
Owner

@DannyBoyk commented on GitHub (Dec 22, 2023):

PR created. It's not 100% clear to me what the build failures on GitHub are about. It compiles fine and passes all tests on my Windows machine. Just a one line change.

@DannyBoyk commented on GitHub (Dec 22, 2023): PR created. It's not 100% clear to me what the build failures on GitHub are about. It compiles fine and passes all tests on my Windows machine. Just a one line change.
Author
Owner

@DannyBoyk commented on GitHub (Dec 22, 2023):

I see that it was some formatting thing in the build project, which I typically don't use. Can those rules not be enforced with code style rules? Anyway, build passing now.

@DannyBoyk commented on GitHub (Dec 22, 2023): I see that it was some formatting thing in the build project, which I typically don't use. Can those rules not be enforced with code style rules? Anyway, build passing now.
Author
Owner

@adamhathcock commented on GitHub (Dec 27, 2023):

It's enforced and correct with a dotnet tool. Thanks!

@adamhathcock commented on GitHub (Dec 27, 2023): It's enforced and correct with a dotnet tool. Thanks!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#607