Skip to content

Conversation

@iremyux
Copy link
Contributor

@iremyux iremyux commented Nov 28, 2025

This PR adds new overloads to ZipArchiveEntry.Open() and ZipArchiveEntry.OpenAsync() that accept a FileAccess parameter, allowing users to specify the desired access mode when opening an entry stream.

When a ZipArchive is opened in ZipArchiveMode.Update, calling ZipArchiveEntry.Open() always returns a read-write stream by invoking OpenInUpdateMode(). This causes the entire entry to be decompressed into memory, even when the caller only intends to read the entry's contents.

New APIs:

public Stream Open(FileAccess access);
public Task<Stream> OpenAsync(FileAccess access, CancellationToken cancellationToken = default);

Update Mode Details:

  • FileAccess.Read: Opens a read-only stream over the entry's compressed data without loading it into memory. Useful for streaming reads.

  • FileAccess.Write: Opens an empty writable stream, discarding any existing entry data. Semantically equivalent to "replace the entry content entirely" (like FileMode.Create. The stream is backed by a MemoryStream and content is written to the archive when disposed.

  • FileAccess.ReadWrite: Same as parameterless Open()/OpenAsync() - loads existing data into memory and returns a read/write/seekable stream.

Fixes #101243

@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-io-compression
See info in area-owners.md if you want to be subscribed.

@iremyux iremyux marked this pull request as ready for review November 28, 2025 11:26
Copilot AI review requested due to automatic review settings November 28, 2025 11:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds a new Open(FileAccess access) overload to ZipArchiveEntry that allows callers to explicitly specify the desired file access mode when opening an entry. This is particularly valuable in ZipArchiveMode.Update, where the existing parameterless Open() always returns a read-write stream by decompressing the entire entry into memory. The new overload allows specifying FileAccess.Read to avoid this memory overhead when only reading is needed.

Key Changes:

  • New Open(FileAccess access) method validates that the requested access is compatible with the archive's mode and calls the appropriate internal open method (OpenInReadMode, OpenInWriteMode, or OpenInUpdateMode)
  • Three new error message resources for incompatible access modes
  • Comprehensive test coverage for all combinations of archive modes and access types

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 4 comments.

File Description
src/libraries/System.IO.Compression/src/System/IO/Compression/ZipArchiveEntry.cs Implements the new Open(FileAccess access) overload with validation logic for different archive modes
src/libraries/System.IO.Compression/ref/System.IO.Compression.cs Adds the new public API surface to the reference assembly
src/libraries/System.IO.Compression/src/Resources/Strings.resx Adds three new error message resources for invalid access scenarios
src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs Adds 11 new test methods covering all valid and invalid access combinations for each archive mode

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

src/libraries/System.IO.Compression/tests/ZipArchive/zip_InvalidParametersAndStrangeFiles.cs:2256

  • The comment states that ReadWrite "opens in write mode" but based on the implementation (line 392 in ZipArchiveEntry.cs), it actually calls OpenInWriteMode() which may return a stream with different capabilities than implied. The comment should clarify what capabilities the returned stream actually has. Consider: "// ReadWrite is allowed in Create mode and calls OpenInWriteMode()"
            // ReadWrite should be allowed in Create mode (it opens in write mode)

@iremyux iremyux marked this pull request as ready for review December 10, 2025 22:35
@iremyux iremyux requested a review from Copilot December 10, 2025 22:35
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

@iremyux iremyux requested a review from a team January 8, 2026 17:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.

{
foreach (bool async in new[] { true, false })
{
// (async, mode, access, createNewEntry, expectedCanRead, expectedCanWrite, expectedCanSeek, expectedLength)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this comment used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a documentation comment that explains the structure of the test data that is being yielded - i saw it used in a few places.

@iremyux iremyux changed the title Add ZipArchiveEntry.Open(FileAccess) overload Add ZipArchiveEntry.Open(FileAccess) and OpenAsync(FileAccess, CancellationToken) overloads to ZipArchiveEntry Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Add ZipArchiveEntry.Open overload

3 participants