[PR #400] Avoid throwing NotSupportedException in ReaderFactory hot path #1008

Open
opened 2026-01-29 22:18:35 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/adamhathcock/sharpcompress/pull/400

State: closed
Merged: Yes


ReaderFactory.Open() calls ZipArchive.IsZipFile() to determine if the stream is a zip archive, which calls into ZipHeaderFactory.ReadHeader(), which throws a NotSupportedException when the bytes do not match a valid header.

To be clear, this exception is caught and IsZipFile() returns false, but when called in a hot-path, these exceptions can become expensive.

To address this issue, ReadHeader now returns null in the default cause instead of throwing. All callsites were already checking for and handling null, so no behavior should change.

**Original Pull Request:** https://github.com/adamhathcock/sharpcompress/pull/400 **State:** closed **Merged:** Yes --- `ReaderFactory.Open()` calls `ZipArchive.IsZipFile()` to determine if the stream is a zip archive, which calls into `ZipHeaderFactory.ReadHeader()`, which throws a `NotSupportedException` when the bytes do not match a valid header. To be clear, this exception is caught and `IsZipFile()` returns `false`, but when called in a hot-path, these exceptions can become expensive. To address this issue, `ReadHeader` now returns `null` in the default cause instead of throwing. All callsites were already checking for and handling `null`, so no behavior should change.
claunia added the pull-request label 2026-01-29 22:18:35 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1008