[PR #1145] [MERGED] Add leaveOpen parameter to LZipStream and BZip2Stream #1582

Closed
opened 2026-01-29 22:21:14 +00:00 by claunia · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/adamhathcock/sharpcompress/pull/1145
Author: @Copilot
Created: 1/19/2026
Status: Merged
Merged: 1/19/2026
Merged by: @adamhathcock

Base: masterHead: copilot/add-leaveopen-parameter-lzipstream


📝 Commits (3)

  • 4998676 Initial plan
  • 0e4a159 Add leaveOpen parameter to LZipStream and BZip2Stream
  • 9454466 Add comprehensive tests for leaveOpen behavior and fix BZip2 stream disposal

📊 Changes

7 files changed (+309 additions, -23 deletions)

View changed files

📝 src/SharpCompress/Compressors/BZip2/BZip2Stream.cs (+15 -3)
📝 src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs (+11 -3)
📝 src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs (+9 -4)
📝 src/SharpCompress/Compressors/LZMA/LZipStream.cs (+5 -3)
📝 src/SharpCompress/Compressors/LZMA/LzmaStream.cs (+28 -8)
📝 tests/SharpCompress.Test/Streams/DisposalTests.cs (+15 -2)
tests/SharpCompress.Test/Streams/LeaveOpenBehaviorTests.cs (+226 -0)

📄 Description

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 adamhathcock/sharpcompress#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.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/adamhathcock/sharpcompress/pull/1145 **Author:** [@Copilot](https://github.com/apps/copilot-swe-agent) **Created:** 1/19/2026 **Status:** ✅ Merged **Merged:** 1/19/2026 **Merged by:** [@adamhathcock](https://github.com/adamhathcock) **Base:** `master` ← **Head:** `copilot/add-leaveopen-parameter-lzipstream` --- ### 📝 Commits (3) - [`4998676`](https://github.com/adamhathcock/sharpcompress/commit/4998676476a2657fee5b0debc585e82461c1287f) Initial plan - [`0e4a159`](https://github.com/adamhathcock/sharpcompress/commit/0e4a159998e34e08ae5644f969120a0c0e590f10) Add leaveOpen parameter to LZipStream and BZip2Stream - [`9454466`](https://github.com/adamhathcock/sharpcompress/commit/9454466be70c45b336ed5baf9b2299644e3d1f7e) Add comprehensive tests for leaveOpen behavior and fix BZip2 stream disposal ### 📊 Changes **7 files changed** (+309 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `src/SharpCompress/Compressors/BZip2/BZip2Stream.cs` (+15 -3) 📝 `src/SharpCompress/Compressors/BZip2/CBZip2InputStream.cs` (+11 -3) 📝 `src/SharpCompress/Compressors/BZip2/CBZip2OutputStream.cs` (+9 -4) 📝 `src/SharpCompress/Compressors/LZMA/LZipStream.cs` (+5 -3) 📝 `src/SharpCompress/Compressors/LZMA/LzmaStream.cs` (+28 -8) 📝 `tests/SharpCompress.Test/Streams/DisposalTests.cs` (+15 -2) ➕ `tests/SharpCompress.Test/Streams/LeaveOpenBehaviorTests.cs` (+226 -0) </details> ### 📄 Description 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 ```csharp 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`. <!-- START COPILOT ORIGINAL PROMPT --> <details> <summary>Original prompt</summary> > > ---- > > *This section details on the original issue you should resolve* > > <issue_title>LZipStream and disposal of original stream</issue_title> > <issue_description>@adamhathcock PR adamhathcock/sharpcompress#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) > > <comments> > </comments> > </details> <!-- START COPILOT CODING AGENT SUFFIX --> - Fixes adamhathcock/sharpcompress#1144 <!-- START COPILOT CODING AGENT TIPS --> --- 💡 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](https://gh.io/copilot-coding-agent-tips) in the docs. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-29 22:21:14 +00:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/sharpcompress#1582