fix(7z): handle empty-stream entries in WriteToDirectory (fixes #1217)#1218
Closed
YoshihiroIto wants to merge 1 commit intoadamhathcock:masterfrom
Closed
fix(7z): handle empty-stream entries in WriteToDirectory (fixes #1217)#1218YoshihiroIto wants to merge 1 commit intoadamhathcock:masterfrom
YoshihiroIto wants to merge 1 commit intoadamhathcock:masterfrom
Conversation
- Handles zero-byte files in 7z archives by returning a null stream. - Adds a test to verify that writing an archive containing an empty stream to a directory does not throw an exception. - Adds a new archive to the test suite.
Contributor
Code Review SummaryStatus: No Issues Found | Recommendation: Merge Files Reviewed (2 files)
|
adamhathcock
approved these changes
Feb 14, 2026
Owner
|
Looks sound. I'll probably base it on release to get it out faster. |
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a 7z extraction crash in the solid-extraction reader path by treating valid “empty stream” entries (HasStream == false) as zero-byte files during WriteToDirectory.
Changes:
- Add an early
HasStreamguard inSevenZipArchive.SevenZipReader.GetEntryStream()to return an empty stream for empty-stream entries. - Add a regression test that exercises the real failing API (
WriteToDirectory) against a dedicated 7z fixture. - Add a new test archive containing an empty-stream file entry.
Reviewed changes
Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs | Avoids null-folder dereference by short-circuiting empty-stream entries to Stream.Null. |
| tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs | Adds regression coverage for WriteToDirectory extracting an empty-stream entry without throwing and producing a zero-byte file. |
| tests/TestArchives/Archives/7Zip.EmptyStream.7z | New fixture archive to reproduce the empty-stream entry scenario. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Owner
|
Been merged into master now |
Owner
|
Originally contributed by @YoshihiroIto in #1218 (ported to release in #1219). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes a
NullReferenceExceptionwhen extracting 7z archives that contain empty-stream file entries (HasStream == false) viaWriteToDirectory.This PR addresses: #1217
Root cause
In
SevenZipArchivereader flow, non-directory entries were always routed to folder stream handling.For valid 7z empty-stream file entries, no folder stream exists, and this led to a null-folder dereference downstream.
Changes
SevenZipArchiveentry stream creation:!entry.FilePart.Header.HasStream, returnCreateEntryStream(Stream.Null).WriteToDirectory) with7Zip.EmptyStream.7z.tests/TestArchives/Archives/7Zip.EmptyStream.7zWhy this is correct
For 7z empty-stream file entries, the correct behavior is to create an empty output file without attempting folder-
based decompression.
Returning
Stream.Nullpreserves that behavior and avoids the NRE.Verification
NullReferenceExceptionfrom 7z extraction path duringWriteToDirectory.7Zip.EmptyStream.7z.dotnet test tests/SharpCompress.Test/SharpCompress.Test.csproj -c Release --filter "SevenZipArchive_EmptyStream_WriteToDirectory_DoesNotThrow"