Skip to content

Comments

fix(7z): handle empty-stream entries in WriteToDirectory (fixes #1217)#1218

Closed
YoshihiroIto wants to merge 1 commit intoadamhathcock:masterfrom
YoshihiroIto:master
Closed

fix(7z): handle empty-stream entries in WriteToDirectory (fixes #1217)#1218
YoshihiroIto wants to merge 1 commit intoadamhathcock:masterfrom
YoshihiroIto:master

Conversation

@YoshihiroIto
Copy link

Summary

Fixes a NullReferenceException when extracting 7z archives that contain empty-stream file entries (HasStream == false) via WriteToDirectory.

This PR addresses: #1217

Root cause

In SevenZipArchive reader 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

  • Add an early guard in SevenZipArchive entry stream creation:
    • if !entry.FilePart.Header.HasStream, return CreateEntryStream(Stream.Null).
  • Add a regression test that exercises the actual failing path (WriteToDirectory) with 7Zip.EmptyStream.7z.
  • Add test archive:
    • tests/TestArchives/Archives/7Zip.EmptyStream.7z

Why 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.Null preserves that behavior and avoids the NRE.

Verification

  • Reproduced the failure before fix:
    • NullReferenceException from 7z extraction path during WriteToDirectory.
  • Confirmed after fix:
    • extraction completes without exception for 7Zip.EmptyStream.7z.
  • Ran tests:
    • dotnet test tests/SharpCompress.Test/SharpCompress.Test.csproj -c Release --filter "SevenZipArchive_EmptyStream_WriteToDirectory_DoesNotThrow"

- 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.
@kiloconnect
Copy link
Contributor

kiloconnect bot commented Feb 14, 2026

Code Review Summary

Status: No Issues Found | Recommendation: Merge

Files Reviewed (2 files)
  • src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs
  • tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs

@adamhathcock
Copy link
Owner

Looks sound.

I'll probably base it on release to get it out faster.

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

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 HasStream guard in SevenZipArchive.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.

@adamhathcock
Copy link
Owner

Been merged into master now

@adamhathcock
Copy link
Owner

Originally contributed by @YoshihiroIto in #1218 (ported to release in #1219).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants