diff --git a/.vscode/settings.json b/.vscode/settings.json index 07998539..1d13071f 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -25,5 +25,8 @@ "csharpier.enableDebugLogs": false, "omnisharp.enableRoslynAnalyzers": true, "omnisharp.enableEditorConfigSupport": true, - "dotnet-test-explorer.testProjectPath": "tests/**/*.csproj" + "dotnet-test-explorer.testProjectPath": "tests/**/*.csproj", + "chat.tools.terminal.autoApprove": { + "dotnet csharpier": true + } } diff --git a/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs b/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs index 90f9ae2b..3c60e709 100644 --- a/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs +++ b/src/SharpCompress/Common/Zip/ZipFilePart.Async.cs @@ -150,6 +150,19 @@ internal abstract partial class ZipFilePart { 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 propsSize = BinaryPrimitives.ReadUInt16LittleEndian(buffer.AsSpan(2, 2)); diff --git a/src/SharpCompress/IO/SeekableSharpCompressStream.cs b/src/SharpCompress/IO/SeekableSharpCompressStream.cs index 25675875..0aba9c12 100644 --- a/src/SharpCompress/IO/SeekableSharpCompressStream.cs +++ b/src/SharpCompress/IO/SeekableSharpCompressStream.cs @@ -17,12 +17,6 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream /// public override bool LeaveStreamOpen { get; } - /// - /// Gets or sets whether to throw an exception when Dispose is called. - /// Useful for testing to ensure streams are not disposed prematurely. - /// - public override bool ThrowOnDispose { get; set; } - public SeekableSharpCompressStream(Stream stream, bool leaveStreamOpen = false) : base(Null, true, false, null) { diff --git a/src/SharpCompress/IO/SharpCompressStream.Create.cs b/src/SharpCompress/IO/SharpCompressStream.Create.cs index 8011641b..37d91ebf 100644 --- a/src/SharpCompress/IO/SharpCompressStream.Create.cs +++ b/src/SharpCompress/IO/SharpCompressStream.Create.cs @@ -7,13 +7,66 @@ namespace SharpCompress.IO; public partial class SharpCompressStream { /// - /// Creates a SharpCompressStream that acts as a passthrough wrapper. - /// No buffering is performed; CanSeek delegates to the underlying stream. - /// The underlying stream will not be disposed when this stream is disposed. + /// Creates a that acts as a zero-overhead passthrough wrapper + /// around without taking ownership of it. /// + /// + /// + /// This is a thin wrapper: all reads, writes, and seeks are forwarded directly to the underlying + /// stream with no ring-buffer overhead. delegates to the underlying + /// stream's own value. + /// + /// + /// The resulting stream does not support , , + /// or . Call on the passthrough stream to obtain + /// a recording-capable wrapper when needed. + /// + /// + /// Because the stream does not take ownership, the underlying stream is never disposed when + /// this wrapper is disposed. Use this when you need to satisfy an API that expects a + /// without transferring lifetime responsibility. + /// + /// + /// The underlying stream to wrap. Must not be . + /// + /// A passthrough that does not dispose . + /// public static SharpCompressStream CreateNonDisposing(Stream stream) => new(stream, leaveStreamOpen: true, passthrough: true, bufferSize: null); + /// + /// Creates a that supports recording and rewinding over + /// , choosing the most efficient strategy based on the stream's + /// capabilities. + /// + /// + /// Seekable streams — wraps in a thin delegate that calls the underlying + /// stream's native directly. No ring buffer is allocated. + /// stores the current position; seeks + /// back to it. + /// Non-seekable streams (network streams, compressed streams, pipes) — allocates + /// a ring buffer of bytes. All bytes read from the underlying + /// stream are kept in the ring buffer so that can replay them without + /// re-reading the underlying stream. If more bytes have been read than the ring buffer can hold, + /// a subsequent rewind will throw ; increase + /// or to + /// avoid this. + /// Already-wrapped streams — if is already a + /// (or a stack that contains one), it is returned as-is to + /// prevent double-wrapping and double-buffering. + /// + /// The underlying stream to wrap. Must not be . + /// + /// Size in bytes of the ring buffer allocated for non-seekable streams. + /// Defaults to (81 920 bytes) when + /// . Has no effect when is seekable, because + /// no ring buffer is needed in that case. + /// + /// + /// A wrapping . The returned instance + /// owns the stream and will dispose it unless the original source was a non-disposing passthrough + /// wrapper. + /// public static SharpCompressStream Create(Stream stream, int? bufferSize = null) { var rewindableBufferSize = bufferSize ?? Constants.RewindableBufferSize; @@ -54,6 +107,6 @@ public partial class SharpCompressStream // For non-seekable streams, create a SharpCompressStream with rolling buffer // to allow limited backward seeking (required by decompressors that over-read) - return new SharpCompressStream(stream, false, false, bufferSize); + return new SharpCompressStream(stream, false, false, rewindableBufferSize); } } diff --git a/src/SharpCompress/IO/SharpCompressStream.cs b/src/SharpCompress/IO/SharpCompressStream.cs index 770379fc..53e2b864 100644 --- a/src/SharpCompress/IO/SharpCompressStream.cs +++ b/src/SharpCompress/IO/SharpCompressStream.cs @@ -50,7 +50,7 @@ public partial class SharpCompressStream : Stream, IStreamStack /// Gets or sets whether to throw an exception when Dispose is called. /// Useful for testing to ensure streams are not disposed prematurely. /// - public virtual bool ThrowOnDispose { get; set; } + internal bool ThrowOnDispose { get; set; } public SharpCompressStream(Stream stream) { @@ -193,7 +193,7 @@ public partial class SharpCompressStream : Stream, IStreamStack // Ensure ring buffer exists if (_ringBuffer is null) { - _ringBuffer = new RingBuffer(Constants.BufferSize); + _ringBuffer = new RingBuffer(Constants.RewindableBufferSize); } // Mark current position as recording anchor