Skip to content

Conversation

@iremyux
Copy link
Contributor

@iremyux iremyux commented Oct 27, 2025

Fixes automatic stream positioning issue when using compression streams with mixed-content data.

When using DeflateStream, GZipStream, ZLibStream or BrotliStream for 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.

  • Added automatic stream rewinding when decompression completes and the underlying stream supports seeking
  • The stream is now rewound to the exact end of compressed data by seeking back by the number of unconsumed bytes in the inflater/decoder buffer
  • Added _decompressionFinished flag to ensure rewinding happens only once, even with multiple Read/ReadAsync/CopyTo calls after decompression completes
  • Added new tests to verify stream rewinding behavior and prevention of multiple rewinds

Fixes #73770

@iremyux iremyux self-assigned this Oct 27, 2025
@iremyux iremyux marked this pull request as ready for review October 27, 2025 11:25
Copilot AI review requested due to automatic review settings October 27, 2025 11:25
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 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() in Inflater to 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

@iremyux iremyux changed the title Automatically rewind base stream position when DeflateStream decompression finishes Automatically rewind base stream position when decompression finishes Nov 7, 2025
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

Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.

iremyux and others added 4 commits November 24, 2025 10:50
…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>
Copy link
Member

@rzikm rzikm left a 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

@iremyux iremyux merged commit cf9869e into dotnet:main Nov 24, 2025
86 checks passed
@rhuijben
Copy link
Contributor

Thanks, this solves a lot of issues I've had to work around in he past.
Too bad this will probably need a few years to be everywhere in stable releases.

Thanks!!

@iremyux iremyux deleted the 73770-stream-rewind branch November 26, 2025 20:10
@github-actions github-actions bot locked and limited conversation to collaborators Dec 27, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compression streams should seek to the end of compressed data when finished

3 participants