-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add ZipArchiveEntry.Open(FileAccess) overload #122032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f56ccb6
b977ca7
04ddf87
b35de54
24f7ebe
31f1476
0f2e2ce
a046dfa
a3cf84b
9878931
760a5aa
2fda98c
1320d08
1aa78ae
a376e3a
f63c42a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -378,6 +378,48 @@ public Stream Open() | |||||||
| } | ||||||||
| } | ||||||||
|
|
||||||||
| /// <summary> | ||||||||
| /// Opens the entry with the specified access mode. This allows for more granular control over the returned stream's capabilities. | ||||||||
| /// </summary> | ||||||||
| /// <param name="access">The file access mode for the returned stream.</param> | ||||||||
| /// <returns>A <see cref="Stream"/> that represents the contents of the entry with the specified access capabilities.</returns> | ||||||||
| /// <exception cref="ArgumentException">The requested access is not compatible with the archive's open mode.</exception> | ||||||||
| /// <exception cref="IOException">The entry is already currently open for writing. -or- The entry has been deleted from the archive. -or- The archive that this entry belongs to was opened in ZipArchiveMode.Create, and this entry has already been written to once.</exception> | ||||||||
| /// <exception cref="InvalidDataException">The entry is missing from the archive or is corrupt and cannot be read. -or- The entry has been compressed using a compression method that is not supported.</exception> | ||||||||
| /// <exception cref="ObjectDisposedException">The ZipArchive that this entry belongs to has been disposed.</exception> | ||||||||
| public Stream Open(FileAccess access) | ||||||||
| { | ||||||||
| ThrowIfInvalidArchive(); | ||||||||
|
|
||||||||
| if (access is not FileAccess.Read and not FileAccess.Write and not FileAccess.ReadWrite) | ||||||||
| throw new ArgumentException(SR.InvalidFileAccess, nameof(access)); | ||||||||
|
|
||||||||
| // Validate that the requested access is compatible with the archive's mode | ||||||||
| switch (_archive.Mode) | ||||||||
| { | ||||||||
| case ZipArchiveMode.Read: | ||||||||
| if (access != FileAccess.Read) | ||||||||
| throw new ArgumentException(SR.CannotBeWrittenInReadMode, nameof(access)); | ||||||||
| return OpenInReadMode(checkOpenable: true); | ||||||||
|
|
||||||||
| case ZipArchiveMode.Create: | ||||||||
| if (access == FileAccess.Read) | ||||||||
| throw new ArgumentException(SR.CannotBeReadInCreateMode, nameof(access)); | ||||||||
| return OpenInWriteMode(); | ||||||||
iremyux marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||
|
|
||||||||
| case ZipArchiveMode.Update: | ||||||||
| default: | ||||||||
| Debug.Assert(_archive.Mode == ZipArchiveMode.Update); | ||||||||
| return access switch | ||||||||
| { | ||||||||
| FileAccess.Read => OpenInReadMode(checkOpenable: true), | ||||||||
| FileAccess.Write => OpenInWriteModeForUpdate(), | ||||||||
| FileAccess.ReadWrite => OpenInUpdateMode(), | ||||||||
| _ => throw new UnreachableException() | ||||||||
|
Comment on lines
+417
to
+418
|
||||||||
| FileAccess.ReadWrite => OpenInUpdateMode(), | |
| _ => throw new UnreachableException() | |
| FileAccess.ReadWrite => OpenInUpdateMode() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The XML documentation summary doesn't adequately describe what each FileAccess mode does in different ZipArchiveModes, particularly the important behavioral differences in Update mode. Consider adding a remarks section that explains:
This is especially important because the behavior in Update mode with FileAccess.Write (discarding existing data) is semantically different from intuitive expectations.