-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Improve exception message when passing directory to ZipFile methods #121917
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?
Conversation
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.
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 |
| { | ||
| // If we get UnauthorizedAccessException and the path is a directory, provide a clearer error message | ||
| throw new UnauthorizedAccessException(SR.Format(SR.IO_DirectoryNotAllowed, archiveFileName)); | ||
| } |
Copilot
AI
Nov 24, 2025
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 catch block only handles UnauthorizedAccessException, but when ZipArchiveMode.Create is used with an existing directory, FileStream will throw IOException instead (because FileMode.CreateNew is used). This means the improved error message won't be provided for Create mode.
Consider adding an additional catch block for IOException when the mode is ZipArchiveMode.Create:
catch (UnauthorizedAccessException) when (Directory.Exists(archiveFileName))
{
throw new UnauthorizedAccessException(SR.Format(SR.IO_DirectoryNotAllowed, archiveFileName));
}
catch (IOException) when (mode == ZipArchiveMode.Create && Directory.Exists(archiveFileName))
{
throw new IOException(SR.Format(SR.IO_DirectoryNotAllowed, archiveFileName));
}| } | |
| } | |
| catch (IOException) when (mode == ZipArchiveMode.Create && Directory.Exists(archiveFileName)) | |
| { | |
| // If we get IOException in Create mode and the path is a directory, provide a clearer error message | |
| throw new IOException(SR.Format(SR.IO_DirectoryNotAllowed, archiveFileName)); | |
| } |
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.
@iremyux please review above, it looks legit to me. Also please consider to add test with ZipArchiveMode.Create if it make sense.
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.
It looks like The IOException from FileMode.CreateNew is specifically for when a file already exists. However, this PR is addressing the case where a directory path is passed, not an existing file.
So I'm not adding this catch since UnauthorizedAccessException will be thrown even in ZipArchiveMode.Create mode.
src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Extract.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Open.cs
Outdated
Show resolved
Hide resolved
…ression/ZipFile.Create.cs Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
src/libraries/System.IO.Compression.ZipFile/tests/ZipFile.Extract.cs
Outdated
Show resolved
Hide resolved
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.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
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.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
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.
Pull request overview
Copilot reviewed 4 out of 4 changed files in this pull request and generated no new comments.
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