Skip to content

Conversation

@iremyux
Copy link
Contributor

@iremyux iremyux commented Nov 24, 2025

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 finished reviewing on behalf of iremyux November 24, 2025 15:39
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 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));
}
Copy link

Copilot AI Nov 24, 2025

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));
}
Suggested change
}
}
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));
}

Copilot uses AI. Check for mistakes.
Copy link
Member

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.

Copy link
Contributor Author

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.

iremyux and others added 3 commits November 24, 2025 17:14
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
@iremyux iremyux requested a review from Copilot November 26, 2025 12:31
Copilot finished reviewing on behalf of iremyux November 26, 2025 12:34
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 no new comments.

@iremyux iremyux requested a review from Copilot November 26, 2025 19:55
Copilot finished reviewing on behalf of iremyux November 26, 2025 19:58
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 no new comments.

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 no new comments.

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.

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

3 participants