Skip to content

More helpful exception when compressing large files in seek mode #43542

@carlossanlop

Description

@carlossanlop

When the user opens a stream in Create mode, and passes it to a ZipArchive opened in Update mode, then attempts to add a very large file to the zip, an exception will be thrown on dispose stating Stream was too long, which is confusing for the user, and frustrating if the file was large and this happens so late.

The exception is thrown because we restrict the size of a file to Int32.MaxValue. The restriction is reached because the Update mode requires a stream that allows seeking, so we internally create a MemoryStream so that we can update the archive, but the size limit for the file is Int32.MaxValue.

We do have it documented here, but it's not very obvious for the user to look for answers in that location.

We should explore two improvements:

  1. Throw a more helpful error message when this combination of conditions meet (Stream in Create mode, ZipArchive in Update mode, file too large).
  2. Try to detect this case much earlier, instead of waiting it to happen on Dispose.

I also opened to suggest a Roslyn analyzer that would help users avoid falling into this: #35815

This has been reported a few times already:

/cc @ericstj

Metadata

Metadata

Labels

area-System.IO.Compressionhelp wanted[up-for-grabs] Good issue for external contributorsin-prThere is an active PR which will close this issue when it is merged

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions