Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ protected override EntryStream GetEntryStream()
return CreateEntryStream(Stream.Null);
}

// 7z may contain valid "empty stream" file entries (HasStream == false).
// These entries represent zero-byte files and do not have folder mapping.
// Returning Stream.Null keeps extraction behavior correct and avoids
// null-folder dereference when requesting a folder stream.
if (!entry.FilePart.Header.HasStream)
{
return CreateEntryStream(Stream.Null);
}

var folder = entry.FilePart.Folder;

// Check if we're starting a new folder - dispose old folder stream if needed
Expand Down
27 changes: 27 additions & 0 deletions tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,33 @@ public void SevenZipArchive_BZip2_Split_FirstFileRead() =>
[Fact]
public void SevenZipArchive_Copy_PathRead() => ArchiveFileRead("7Zip.Copy.7z");

[Fact]
public void SevenZipArchive_EmptyStream_WriteToDirectory_DoesNotThrow()
{
var archivePath = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.EmptyStream.7z");
var outputPath = Path.Combine(
SCRATCH_FILES_PATH,
nameof(SevenZipArchive_EmptyStream_WriteToDirectory_DoesNotThrow));

if (Directory.Exists(outputPath))
{
Directory.Delete(outputPath, true);
}

Directory.CreateDirectory(outputPath);

using var archive = SevenZipArchive.OpenArchive(archivePath);
var progress = new Progress<ProgressReport>(_ => { });

var ex = Record.Exception(() => archive.WriteToDirectory(outputPath, progress));

Assert.Null(ex);

var files = Directory.GetFiles(outputPath, "*", SearchOption.AllDirectories);
Assert.NotEmpty(files);
Assert.Contains(files, p => new FileInfo(p).Length == 0);
}

[Fact]
public void SevenZipArchive_Copy_CompressionType()
{
Expand Down
Binary file added tests/TestArchives/Archives/7Zip.EmptyStream.7z
Binary file not shown.
Loading