dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.91k stars 4.63k forks source link

Misleading exception for ZipFile.ExtractToDirectory #100720

Open bsj0006 opened 5 months ago

bsj0006 commented 5 months ago

Description

If you mistakenly pass a directory as the sourceArchiveFileName parameter for ZipFile.ExtractToDirectory(), you receive an UnauthorizedAccessException.

Reproduction Steps

ZipFile.ExtractToDirectory("/temp/thisIsADirectory", "/temp/alsoADirectory")

Expected behavior

Based on the current docs, I would assume the exception would be a NotSupportedException or InvalidDataException with a message like "[sourceArchiveFileName] is not a valid zip archive".

Actual behavior

You get an UnauthorizedAccessException with the message "Access to the path '[sourceArchiveFileName]' is denied" regardless of permissions on the directory.

Regression?

.NET 8, 7, & 6

Known Workarounds

No response

Configuration

.NET 8 Windows 11 x64 and mcr.microsoft.com/dotnet/runtime:8.0 Docker image

Other information

No response

dotnet-policy-service[bot] commented 5 months ago

Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.

KalleOlaviNiemitalo commented 5 months ago

Perhaps FileStream could just change the Exception.Message of the UnauthorizedAccessException to say that it's a directory, when it gets an EISDIR error from the operating system. Changing the type of the exception seems more risky.

ericstj commented 1 month ago

I don't expect we can change the exception type, but perhaps a better error message is possible when a directory path is provided to open. Moving the suggestion to IO.

dotnet-policy-service[bot] commented 1 month ago

Tagging subscribers to this area: @dotnet/area-system-io See info in area-owners.md if you want to be subscribed.

jozkee commented 1 month ago

We're open to suggestions for improving the exception message. Some IO APIs document this behavior e.g. File.Open, some don't (e.g. FileStream.ctors, although it should).

ZipFile.ExtractToDirectory could also lift the exception documentation, so it becomes clear for its users.