Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/SharpCompress/Common/Zip/ZipFilePart.Async.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,19 @@ protected async ValueTask<Stream> CreateDecompressionStreamAsync(
{
throw new NotSupportedException("LZMA with pkware encryption.");
}
// When the uncompressed size is known to be zero, skip remaining compressed
// bytes (required for streaming reads) and return an empty stream.
// Bit1 (EOS marker flag) means the output size is not stored in the header
// (the LZMA stream itself contains an end-of-stream marker instead), so we
// only short-circuit when the size is explicitly known to be zero.
if (
!FlagUtility.HasFlag(Header.Flags, HeaderFlags.Bit1)
&& Header.UncompressedSize == 0
)
{
await stream.SkipAsync(cancellationToken).ConfigureAwait(false);
return Stream.Null;
}
var buffer = new byte[4];
await stream.ReadFullyAsync(buffer, 0, 4, cancellationToken).ConfigureAwait(false);
var version = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(0, 2));
Expand Down
14 changes: 14 additions & 0 deletions src/SharpCompress/Common/Zip/ZipFilePart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,20 @@ protected Stream CreateDecompressionStream(Stream stream, ZipCompressionMethod m
reader.ReadUInt16(); //LZMA version
var props = new byte[reader.ReadUInt16()];
reader.Read(props, 0, props.Length);

// When the uncompressed size is known to be zero, skip remaining compressed
// bytes (required for streaming reads) and return an empty stream.
// Bit1 (EOS marker flag) means the output size is not stored in the header
// (the LZMA stream itself contains an end-of-stream marker instead), so we
// only short-circuit when the size is explicitly known to be zero.
if (
!FlagUtility.HasFlag(Header.Flags, HeaderFlags.Bit1)
&& Header.UncompressedSize == 0
)
{
stream.Skip();
return Stream.Null;
}
return LzmaStream.Create(
props,
stream,
Expand Down
12 changes: 6 additions & 6 deletions src/SharpCompress/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@
"net10.0": {
"Microsoft.NET.ILLink.Tasks": {
"type": "Direct",
"requested": "[10.0.2, )",
"resolved": "10.0.2",
"contentHash": "sXdDtMf2qcnbygw9OdE535c2lxSxrZP8gO4UhDJ0xiJbl1wIqXS1OTcTDFTIJPOFd6Mhcm8gPEthqWGUxBsTqw=="
"requested": "[10.0.0, )",
"resolved": "10.0.0",
"contentHash": "kICGrGYEzCNI3wPzfEXcwNHgTvlvVn9yJDhSdRK+oZQy4jvYH529u7O0xf5ocQKzOMjfS07+3z9PKRIjrFMJDA=="
},
"Microsoft.NETFramework.ReferenceAssemblies": {
"type": "Direct",
Expand Down Expand Up @@ -254,9 +254,9 @@
"net8.0": {
"Microsoft.NET.ILLink.Tasks": {
"type": "Direct",
"requested": "[8.0.23, )",
"resolved": "8.0.23",
"contentHash": "GqHiB1HbbODWPbY/lc5xLQH8siEEhNA0ptpJCC6X6adtAYNEzu5ZlqV3YHA3Gh7fuEwgA8XqVwMtH2KNtuQM1Q=="
"requested": "[8.0.22, )",
"resolved": "8.0.22",
"contentHash": "MhcMithKEiyyNkD2ZfbDZPmcOdi0GheGfg8saEIIEfD/fol3iHmcV8TsZkD4ZYz5gdUuoX4YtlVySUU7Sxl9SQ=="
},
"Microsoft.NETFramework.ReferenceAssemblies": {
"type": "Direct",
Expand Down
14 changes: 14 additions & 0 deletions tests/SharpCompress.Test/Zip/ZipArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -846,4 +846,18 @@ public void Zip_FilePermissions()
const int expected = (S_IFREG | 0b110_100_100) << 16; // 0644 mode regular file
Assert.Equal(expected, firstEntry.Attrib);
}

[Fact]
public void Zip_LZMA_ZeroSizeEntry_CanExtract()
{
using var archive = ArchiveFactory.OpenArchive(
Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.empty.zip")
);
var entries = archive.Entries.Where(x => !x.IsDirectory).ToList();
Assert.Single(entries);
Assert.Equal(0, entries[0].Size);
var outStream = new MemoryStream();
entries[0].WriteTo(outStream);
Assert.Equal(0, outStream.Length);
}
}
23 changes: 23 additions & 0 deletions tests/SharpCompress.Test/Zip/ZipReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,4 +543,27 @@ public void Archive_Iteration_DoesNotBreak_WhenFlushThrows_LZMA()
// Should iterate through all entries, not just the first one
Assert.True(count > 1, $"Expected more than 1 entry, but got {count}");
}

[Fact]
public void Zip_LZMA_ZeroSizeEntry_CanExtract_Streaming()
{
var path = Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.empty.zip");
using var fileStream = File.OpenRead(path);
using Stream stream = new ForwardOnlyStream(fileStream);
using var reader = ReaderFactory.OpenReader(stream);

var count = 0;
while (reader.MoveToNextEntry())
{
if (!reader.Entry.IsDirectory)
{
count++;
Assert.Equal(0, reader.Entry.Size);
var outStream = new MemoryStream();
reader.WriteEntryTo(outStream);
Assert.Equal(0, outStream.Length);
}
}
Assert.Equal(1, count);
}
}
Binary file added tests/TestArchives/Archives/Zip.lzma.empty.zip
Binary file not shown.