From fc426c73e062c76528dee97644abd227db2b5f67 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sun, 1 Mar 2026 13:37:48 +0000 Subject: [PATCH] correct fix --- .../Common/Zip/ZipFilePart.Async.cs | 36 +++++---------- src/SharpCompress/Common/Zip/ZipFilePart.cs | 46 +++++++++---------- src/SharpCompress/packages.lock.json | 12 ++--- 3 files changed, 39 insertions(+), 55 deletions(-) diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs b/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs index b95fe617..e2a04f4f 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs @@ -228,19 +228,10 @@ internal abstract partial class ZipFilePart } case ZipCompressionMethod.LZMA: { - if (FlagUtility.HasFlag(Header.Flags, HeaderFlags.Encrypted)) + if (FlagUtility.HasFlag(Header.Flags, HeaderFlags.Encrypted)) { throw new NotSupportedException("LZMA with pkware encryption."); } - var buffer = new byte[4]; - await stream.ReadFullyAsync(buffer, 0, 4, cancellationToken).ConfigureAwait(false); - var version = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(0, 2)); - var propsSize = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(2, 2)); - var props = new byte[propsSize]; - await stream - .ReadFullyAsync(props, 0, propsSize, cancellationToken) - .ConfigureAwait(false); - // 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 @@ -254,20 +245,17 @@ internal abstract partial class ZipFilePart await stream.SkipAsync(cancellationToken).ConfigureAwait(false); return Stream.Null; } - - context = context with - { - Properties = props, - InputSize = - Header.CompressedSize > 0 ? Header.CompressedSize - 4 - props.Length : -1, - OutputSize = FlagUtility.HasFlag(Header.Flags, HeaderFlags.Bit1) - ? -1 - : Header.UncompressedSize, - }; - - return await providers - .CreateDecompressStreamAsync( - compressionType, + var buffer = new byte[4]; + await stream.ReadFullyAsync(buffer, 0, 4, cancellationToken).ConfigureAwait(false); + var version = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(0, 2)); + var propsSize = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(2, 2)); + var props = new byte[propsSize]; + await stream + .ReadFullyAsync(props, 0, propsSize, cancellationToken) + .ConfigureAwait(false); + return await LzmaStream + .CreateAsync( + props, stream, Header.CompressedSize > 0 ? Header.CompressedSize - 4 - props.Length : -1, FlagUtility.HasFlag(Header.Flags, HeaderFlags.Bit1) diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.cs b/src/SharpCompress/Common/Zip/ZipFilePart.cs index bfa8d79c..65277dab 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.cs @@ -146,39 +146,35 @@ internal abstract partial class ZipFilePart : FilePart } case ZipCompressionMethod.LZMA: { - if (FlagUtility.HasFlag(Header.Flags, HeaderFlags.Encrypted)) + if (FlagUtility.HasFlag(Header.Flags, HeaderFlags.Encrypted)) { throw new NotSupportedException("LZMA with pkware encryption."); } - - using var reader = new BinaryReader( - stream, - System.Text.Encoding.Default, - leaveOpen: true - ); - reader.ReadUInt16(); // LZMA version - var propsLength = reader.ReadUInt16(); - var props = reader.ReadBytes(propsLength); - - // 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 + using ( + var reader = new BinaryReader( + stream, + System.Text.Encoding.Default, + leaveOpen: true + ) ) - { - stream.Skip(); - return Stream.Null; - } - - context = context with { 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, diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index 341de67f..287bf2cc 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -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", @@ -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",