adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.22k stars 476 forks source link

Add option to leave base stream open when disposing #779

Open RayCarrot opened 7 months ago

RayCarrot commented 7 months ago

Currently when disposing a compression stream such as ZlibStream it will also dispose the base stream you passed in. There are however times where you still want the base stream to remain open for further use and currently the only way I could find to do this is by creating a wrapping stream which doesn't dispose. My suggestion would be to do what most built-in .NET streams do and have a parameter in the constructor called leaveOpen which indicates if the base stream you pass in should be left open even after disposing.

adamhathcock commented 7 months ago

We shouldn't be disposing streams we didn't create. A lot of StreamReaders and whatnot do dispose.

Please show what usage disposes unexpectedly

RayCarrot commented 7 months ago

Your implementation of ZlibStream disposes the stream you pass in when disposing the ZlibStream. It happens here as far as I can tell. I also checked the usage docs which say "I've decided to always close wrapped streams as of 0.21". The suggestion there is to use a wrapper stream, like I currently do, to avoid disposing. However I would suggest having the option to leave the stream open with a bool parameter just like the standard .NET libraries do. This avoids any additional overhead added by a wrapping stream.

Erior commented 5 months ago

Options "LeaveStreamOpen" true/false Think we have tests regarding dispose, are you using ZlibStream directly or what is the scenario?

RayCarrot commented 5 months ago

I am using ZlibStream directly, yes. And from what I can tell that's the only option in this library. Currently I made my own wrapper stream to avoid disposing, but I'd prefer if there was simply an option in the API directly like there is in the streams in the .NET libraries. There should be a difference between wanting to dispose the compression stream and whatever data it might be using and the base stream that you pass in.

adamhathcock commented 5 months ago

It can be a feature added, I think the compression streams directly haven't been changed to respond to that option