Skip to content

Fix TarFactory extension hinting in TryOpenReader, TryOpenReaderAsync, and GetCompressionType#1256

Draft
Copilot wants to merge 2 commits into
masterfrom
copilot/fix-tar-archive-extension-hinting
Draft

Fix TarFactory extension hinting in TryOpenReader, TryOpenReaderAsync, and GetCompressionType#1256
Copilot wants to merge 2 commits into
masterfrom
copilot/fix-tar-archive-extension-hinting

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 17, 2026

TarFactory.IsArchive and GetCompressionType had no path to receive ReaderOptions, so ExtensionHint and custom Providers were silently dropped whenever Factory.TryOpenReader drove detection. Every call fell back to brute-forcing all TarWrapper candidates and the GZip probe lacked its CompressionContext.

Changes

TarFactory — override TryOpenReader / TryOpenReaderAsync

  • TryOpenReader: replaces the base-class pattern of IsArchiveRewind(true)OpenReader with a direct wrapper loop that passes options.Providers and full ReaderOptions into CreateProbeDecompressionStream. Uses stream.Rewind(true) (not StopRecording()) to correctly reset both seekable (SeekableSharpCompressStream) and ring-buffer streams.
  • TryOpenReaderAsync: wraps the incoming SharpCompressStream in a fresh ring-buffer SharpCompressStream before probing. This is required because decompressor constructors (e.g. LZipStream, XZStream) perform synchronous reads; the ring buffer lets those sync reads replay data that was fetched asynchronously — critical for async-only transports.
// Before: options.ExtensionHint was ignored, default providers were used
Factory.TryOpenReader → TarFactory.IsArchive(stream, password)  // no ReaderOptions

// After: hints and providers flow through the entire detection path
TarFactory.TryOpenReader(stream, options) → CreateProbeDecompressionStream(..., options)

TarFactory.GetCompressionType / GetCompressionTypeAsync

Added optional IReaderOptions? readerOptions = null parameter, threaded through to CreateProbeDecompressionStream / CreateProbeDecompressionStreamAsync.
For GetCompressionTypeAsync the new parameter is inserted before CancellationToken; all in-tree callers are updated.

TarArchive.Factory.cs

All GetCompressionType / GetCompressionTypeAsync call-sites now pass sourceStream.ReaderOptions, so archive-open paths also benefit from hint propagation.

Tests

Added TarReaderTests cases that open compressed tar archives via ReaderFactory.OpenReader with an explicit ExtensionHint (.tar.gz, .tar.bz2, .tar.xz, .tar), exercising the previously broken detection path.

Original prompt

This section details on the original issue you should resolve

<issue_title>Tar archive doesn't use extension hinting on two primary functions</issue_title>
<issue_description>The CreateProbeDecompressionStream is the only way hinting happens with tar currently. Unfortunately that requires the reader options parameter to be passed which it is not in:

  • GetCompressionType
  • IsArchive

IsArchive is once again in the code flow for auto detection on OpenReader but that quickly causes this stack:

 	SharpCompress.dll!SharpCompress.Archives.Tar.TarArchive.IsTarFile(System.IO.Stream stream) Line 222	C#
 	SharpCompress.dll!SharpCompress.Factories.TarFactory.IsArchive(System.IO.Stream stream, string password) Line 67	C#
 	SharpCompress.dll!SharpCompress.Factories.Factory.TryOpenReader(SharpCompress.IO.SharpCompressStream stream, SharpCompress.Readers.ReaderOptions options, out SharpCompress.Readers.IReader reader) Line 89	C#
>	SharpCompress.dll!SharpCompress.Readers.ReaderFactory.OpenReader(System.IO.Stream stream, SharpCompress.Readers.ReaderOptions options) Line 51	C#

As TryOpenReader is calling IsArchive and IsArchive doesn't have the ability to take reader options it loses hinting and goes back to brute forcing.</issue_description>

Comments on the Issue (you are @copilot in this section)


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

…e, and TryOpenReaderAsync

Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix tar archive extension hinting in key functions Fix TarFactory extension hinting in TryOpenReader, TryOpenReaderAsync, and GetCompressionType Mar 17, 2026
Copilot AI requested a review from adamhathcock March 17, 2026 13:10
Base automatically changed from release to master March 28, 2026 09:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants