Repository navigation
Improve exception message when passing directory to ZipFile methods - #121917
Merged
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves error messages when directory paths are incorrectly passed to ZipFile methods that expect file paths. Instead of the generic UnauthorizedAccessException, users now receive a clearer error message explicitly stating that a directory is not allowed.
Key changes:
- Enhanced
GetFileStreamForOpen()to detect when a directory path is passed and throw a more descriptive error message - Added comprehensive test coverage for Read and Update modes across sync and async methods
- Added new localized error message string
IO_DirectoryNotAllowed
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
src/libraries/System.IO.Compression.ZipFile/src/System/IO/Compression/ZipFile.Create.cs |
Added try-catch block in GetFileStreamForOpen() to detect directory paths and throw improved error messages |
src/libraries/System.IO.Compression.ZipFile/src/Resources/Strings.resx |
Added new error message resource IO_DirectoryNotAllowed for directory-related errors |
src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Open.cs |
Added test methods to verify improved error messages for sync and async Open methods |
src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Extract.cs |
Added test to verify improved error message for ExtractToDirectory methods |
…ression/ZipFile.Create.cs Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
jkotas
reviewed
Nov 24, 2025
This was referenced Nov 24, 2025
Open
rzikm
approved these changes
Nov 28, 2025
rokonec
approved these changes
Nov 28, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes an issue where passing a directory path to
ZipFilemethods resulted in an unclearUnauthorizedAccessExceptionwithout explaining that the problem was passing a directory instead of a file.Modified
GetFileStreamForOpen()to catchUnauthorizedAccessExceptionwhenFileStreamfails to open a directory, and re-throw with a clearer error message that explicitly states the path is a directory.Fixes #100720