Skip to content

Add leaveOpen parameter to LZipStream and BZip2Stream#1145

Merged
adamhathcock merged 3 commits intomasterfrom
copilot/add-leaveopen-parameter-lzipstream
Jan 19, 2026
Merged

Add leaveOpen parameter to LZipStream and BZip2Stream#1145
adamhathcock merged 3 commits intomasterfrom
copilot/add-leaveopen-parameter-lzipstream

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

PR #1113 changed disposal behavior for LZipStream and BZip2Stream, breaking code that expected the underlying stream to remain open. These streams lacked the leaveOpen parameter available in other compression streams like ZStandard.

Changes

  • Added leaveOpen parameter to LZipStream and BZip2Stream constructors (defaults to false for backward compatibility)
  • Updated internal streams (CBZip2InputStream, CBZip2OutputStream, LzmaStream) to propagate and respect the leaveOpen flag
  • Fixed disposal logic in CBZip2InputStream.BsFinishedWithStream() which was disposing streams unconditionally during decompression

Usage

using var memoryStream = new MemoryStream();

// Stream is disposed when compression stream is disposed (default behavior)
using (var bzip2 = new BZip2Stream(memoryStream, CompressionMode.Compress, false))
{
    bzip2.Write(data, 0, data.Length);
}
// memoryStream is now disposed

// Stream remains open after compression stream is disposed
using (var lzip = new LZipStream(memoryStream, CompressionMode.Compress, leaveOpen: true))
{
    lzip.Write(data, 0, data.Length);
}
// memoryStream is still usable

Testing

Added comprehensive tests in LeaveOpenBehaviorTests.cs covering compress/decompress modes for both streams with leaveOpen=true/false.

Original prompt

This section details on the original issue you should resolve

<issue_title>LZipStream and disposal of original stream</issue_title>
<issue_description>@adamhathcock PR #1113 changed the Disposal behaviour of LZipStream/BZip2Stream and broke my unit test which expected the stream to still be open.
Now there also is an inconsistency between the Compress and Decompress modes.

All other compression streams my library uses have a leaveOpen parameter.

Would it be possible to add an leaveOpen parameter to LZipStream and BZip2Stream?

I could create a PR for this if you like.

</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 19, 2026 07:31
Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
…isposal

Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot AI changed the title [WIP] Add leaveOpen parameter to LZipStream and BZip2Stream Add leaveOpen parameter to LZipStream and BZip2Stream Jan 19, 2026
Copilot AI requested a review from adamhathcock January 19, 2026 07:40
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

This pull request adds a leaveOpen parameter to LZipStream and BZip2Stream compression streams, addressing a breaking change introduced in PR #1113. The parameter controls whether the underlying stream should remain open after the compression stream is disposed, matching the behavior of other compression streams like ZStandard.

Changes:

  • Added leaveOpen parameter (defaults to false) to BZip2Stream and LZipStream constructors
  • Updated internal streams (CBZip2InputStream, CBZip2OutputStream, LzmaStream) to propagate and respect the flag
  • Fixed disposal logic in CBZip2InputStream.BsFinishedWithStream() which was disposing streams unconditionally

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/SharpCompress.Test/Streams/LeaveOpenBehaviorTests.cs New comprehensive test file covering compress/decompress modes for both BZip2Stream and LZipStream with leaveOpen true/false
tests/SharpCompress.Test/Streams/DisposalTests.cs Updated existing disposal tests to verify leaveOpen behavior for LZipStream and BZip2Stream
src/SharpCompress/Compressors/BZip2/BZip2Stream.cs Added leaveOpen parameter to constructor and propagated it to internal streams
src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs Added leaveOpen field and updated disposal logic to conditionally dispose the underlying stream
src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs Added leaveOpen field and fixed disposal logic in both Dispose() and BsFinishedWithStream() methods
src/SharpCompress/Compressors/LZMA/LZipStream.cs Added leaveOpen parameter and updated compress mode disposal logic
src/SharpCompress/Compressors/LZMA/LzmaStream.cs Added leaveOpen parameter to all decoder constructors and updated disposal logic

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@adamhathcock adamhathcock marked this pull request as ready for review January 19, 2026 08:58
@adamhathcock adamhathcock merged commit ce01cc7 into master Jan 19, 2026
11 checks passed
@adamhathcock adamhathcock deleted the copilot/add-leaveopen-parameter-lzipstream branch January 19, 2026 09:57
adamhathcock added a commit that referenced this pull request Jan 19, 2026
…meter-lzipstream

Add leaveOpen parameter to LZipStream and BZip2Stream
adamhathcock added a commit that referenced this pull request Jan 19, 2026
Merge pull request #1145 from adamhathcock/copilot/add-leaveopen-para…
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.

LZipStream and disposal of original stream

2 participants

Comments