Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Fix local zip entry header for large files - #36027

Merged
ericstj merged 1 commit into
dotnet:masterfrom
ericstj:compresssion-big-writeOnlyFile
Mar 14, 2019
Merged

ericstj merged 1 commit into
dotnet:masterfrom
ericstj:compresssion-big-writeOnlyFile

Conversation

@ericstj

@ericstj ericstj commented Mar 13, 2019 •

Copy link
Copy Markdown
Member

When opened for Write ZipArchiveEntry needs to write the local header
information to the archive stream upon the first call to write since the
compressed content comes after that and we write directly to the backing stream
rather than buffering in memory.

By default header information only accommodates 32-bit sizes since this is much
more likely and saves space in archives with lots of files and at the first
write we don’t know how big the entry is going to be.

Once the entry is disposed we know the final length and may find it cannot be
represented in 32-bit fields. It can’t insert the extra bytes it would need
in the header region since it’s already written the compressed data and
streams/files don’t have insertion operations, so instead it sets a bit that
says the entry's size information is in a descriptor at the end of the entry.

The entry was setting the bit that indicated the descriptor existed but didn't
set the version in the header to ZIP64 to indicate wether that descriptor had
64-bit fields. Additionally we were writing the central-directory header for
the file with a version of ZIP64 so our local header and central header didn't
match.

Many ZIP implementations were OK with this, but not all. Specifically
WindowsBase.dll's implementation of the System.IO.Packaging APIs has a
ZipArchive that insists the local header version must be ZIP64 to have a
64-bit descriptor. Moreover it also insists that the local-header version must
match the central header version.

To ensure zips created with .NETCore's System.IO.Packaging APIs work with
desktop we should set this version field in the local header.

There's not a great way to test this in CI since the scenario is produce a zip
with file over 4GB on .NETCore and read it on desktop and we don't have
test scenarios that cross frameworks like that. We do cover this codepath
in the System.IO.Packaging outerloop test I added recently. Feel free to
chime in if you have any suggestions here.

Fixes #36013

/cc @pensivebrian

When opened for Write ZipArchiveEntry needs to write the local header
information to the archive stream upon the first call to write since the
compressed content comes after that and we write directly to the backing stream
rather than buffering in memory.

By default header information only accommodates 32-bit sizes since this is much
more likely and saves space in archives with lots of files and at the first
write we don’t know how big the entry is going to be.

Once the entry is disposed we know the final length and may find it cannot be
represented in 32-bit fields.  It can’t insert the extra bytes it would need
in the header region since it’s already written the compressed data and
streams/files don’t have insertion operations, so instead it sets a bit that
says the entry's size information is in a descriptor at the end of the entry.

The entry was setting the bit that indicated the descriptor existed but didn't
set the version in the header to ZIP64 to indicate wether that descriptor had
64-bit fields.  Additionally we were writing the central-directory header for
the file with a version of ZIP64 so our local header and central header didn't
match.

May ZIP implementations were OK with this, but not all.  Specifically
WindowsBase.dll's implementation of the System.IO.Packaging APIs has a
ZipArchive that insists the local header version must be ZIP64 to have a
64-bit descriptor.  Moreover it also insists that the local-header version must
match the central header version.

To ensure zips created with .NETCore's System.IO.Packaging APIs work with
desktop we should set this version field in the local header.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants