-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Automatically rewind base stream position when decompression finishes #121111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this 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 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 |
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
src/libraries/System.IO.Compression.Brotli/src/System/IO/Compression/BrotliStream.cs
Outdated
Show resolved
Hide resolved
...raries/System.IO.Compression.Brotli/src/System/IO/Compression/dec/BrotliStream.Decompress.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
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
…ession/BrotliStream.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…DeflateZLib/DeflateStream.cs Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…et-runtime into 73770-stream-rewind
rzikm
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, apart from unresolved Copilot comments about comment wording
|
Thanks, this solves a lot of issues I've had to work around in he past. Thanks!! |
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