Automatically rewind base stream position when decompression finishes#121111
Merged
iremyux merged 23 commits intodotnet:mainfrom Nov 24, 2025
Merged
Automatically rewind base stream position when decompression finishes#121111iremyux merged 23 commits intodotnet:mainfrom
iremyux merged 23 commits intodotnet:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR fixes an issue where DeflateStream, GZipStream, and ZLibStream would leave the underlying stream position past the end of compressed data due to internal buffering, making it impossible to read additional content after decompression in mixed-format files.
Key Changes:
- Added automatic stream rewinding logic that repositions seekable streams to the exact end of compressed data when decompression finishes
- Exposed internal method
GetAvailableInput()inInflaterto track unconsumed buffered bytes - Added comprehensive test coverage for both sync and async decompression scenarios
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
CompressionStreamUnitTests.Deflate.cs |
Added two test methods (AutomaticStreamRewinds_WhenDecompressionFinishes and async variant) to verify stream position is correctly restored after decompression |
Inflater.cs |
Added GetAvailableInput() method to expose the count of unconsumed bytes in the internal buffer |
DeflateStream.cs |
Implemented automatic stream rewinding in both Dispose and DisposeAsync by seeking backwards by the number of unconsumed bytes |
Open
3 tasks
rzikm
reviewed
Nov 3, 2025
src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/tests/CompressionStreamUnitTests.Deflate.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs
Outdated
Show resolved
Hide resolved
This was referenced Nov 7, 2025
rzikm
reviewed
Nov 10, 2025
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/BrotliStream.cs
Outdated
Show resolved
Hide resolved
rzikm
reviewed
Nov 12, 2025
...raries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs
Outdated
Show resolved
Hide resolved
...raries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression/src/System/IO/Compression/DeflateZLib/DeflateStream.cs
Outdated
Show resolved
Hide resolved
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/BrotliStream.cs
Outdated
Show resolved
Hide resolved
This was referenced Nov 23, 2025
…ession/BrotliStream.cs Co-authored-by: Copilot <[email protected]>
…DeflateZLib/DeflateStream.cs Co-authored-by: Copilot <[email protected]>
rzikm
approved these changes
Nov 24, 2025
Member
rzikm
left a comment
There was a problem hiding this comment.
LGTM, apart from unresolved Copilot comments about comment wording
Contributor
|
Thanks, this solves a lot of issues I've had to work around in he past. Thanks!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes automatic stream positioning issue when using compression streams with mixed-content data.
When using
DeflateStream,GZipStream,ZLibStreamorBrotliStreamfor decompression, the underlying stream position would advance past the end of compressed data due to internal buffering. This caused issues when reading additional content after decompression in mixed-format files._decompressionFinishedflag to ensure rewinding happens only once, even with multiple Read/ReadAsync/CopyTo calls after decompression completesFixes #73770