Skip to content

Improve exception message when passing directory to ZipFile methods - #121917

Merged
iremyux merged 10 commits into
dotnet:mainfrom
iremyux:100720-improve-exception-message
Nov 28, 2025
Merged

iremyux merged 10 commits into
dotnet:mainfrom
iremyux:100720-improve-exception-message

Conversation

@iremyux

@iremyux iremyux commented Nov 24, 2025

Copy link
Copy Markdown
Contributor

Fixes an issue where passing a directory path to ZipFile methods resulted in an unclear UnauthorizedAccessException without explaining that the problem was passing a directory instead of a file.

Modified GetFileStreamForOpen() to catch UnauthorizedAccessException when FileStream fails to open a directory, and re-throw with a clearer error message that explicitly states the path is a directory.

Fixes #100720

@iremyux iremyux self-assigned this Nov 24, 2025
@iremyux
iremyux marked this pull request as ready for review November 24, 2025 15:36
@iremyux
iremyux requested review from a team and Copilot November 24, 2025 15:36

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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

iremyux and others added 3 commits November 24, 2025 17:14
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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.

@rzikm rzikm left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM

@iremyux
iremyux merged commit 2e0cb6b into dotnet:main Nov 28, 2025
@iremyux
iremyux deleted the 100720-improve-exception-message branch November 28, 2025 14:21
@github-actions github-actions Bot locked and limited conversation to collaborators Dec 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve exception message when trying to open a directory as a file.

5 participants