diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs
index 7c88b1d6..38dd7ab9 100644
--- a/src/SharpCompress/Factories/TarFactory.cs
+++ b/src/SharpCompress/Factories/TarFactory.cs
@@ -296,11 +296,53 @@ public class TarFactory
#region IReaderFactory
+ ///
+ /// Checks whether is backed by a genuinely seekable stream.
+ /// Note: always reports for
+ /// non-passthrough instances (it simulates seeking via its ring buffer even over a
+ /// non-seekable underlying stream), so it must be unwrapped to check the real stream.
+ ///
+ private static bool IsGenuinelySeekable(Stream stream)
+ {
+ while (stream is SharpCompressStream scs)
+ {
+ stream = scs.BaseStream();
+ }
+ return stream.CanSeek;
+ }
+
+ ///
+ /// Creates a new wrapper around with
+ /// its own independent recording/rewind scope, isolated from any recording session the caller may
+ /// already have in progress on itself.
+ ///
+ ///
+ /// When the stream is genuinely seekable, a is used:
+ /// it delegates recording/rewinding to the underlying stream's native and
+ /// never allocates a rewind ring buffer. For non-seekable streams, a ring-buffered
+ /// is required to support rewinding during format detection.
+ ///
+ private static SharpCompressStream CreateNestedRecordingStream(Stream stream)
+ {
+ if (!IsGenuinelySeekable(stream))
+ {
+ return new SharpCompressStream(stream);
+ }
+
+ // Unwrap buffered SharpCompressStream wrappers so we use the real stream's native Seek.
+ while (stream is SharpCompressStream scs && !scs.IsPassthrough)
+ {
+ stream = scs.BaseStream();
+ }
+
+ return new SeekableSharpCompressStream(stream, leaveStreamOpen: false);
+ }
+
///
public IReader OpenReader(Stream stream, ReaderOptions? options)
{
options ??= ReaderOptions.ForExternalStream;
- var sharpCompressStream = new SharpCompressStream(stream);
+ var sharpCompressStream = CreateNestedRecordingStream(stream);
sharpCompressStream.StartRecording(TarWrapper.MaximumRewindBufferSize);
foreach (var wrapper in TarWrapper.Wrappers)
{
@@ -315,6 +357,7 @@ public class TarFactory
);
if (TarArchive.IsTarFile(decompressedStream))
{
+ sharpCompressStream.Rewind();
sharpCompressStream.StopRecording();
return new TarReader(sharpCompressStream, options, wrapper.CompressionType);
}