From fda3d186c5410a048f017cb4d69fe90ff64c9668 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sun, 12 Jul 2026 11:09:49 +0100 Subject: [PATCH] more tests and a removal of a test --- .../Archives/ArchiveFactory.Detection.cs | 99 +++++++++++++++++++ .../AsyncParityAndCancellationTests.cs | 1 - 2 files changed, 99 insertions(+), 1 deletion(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs index a0084914..2b735925 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.Detection.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.Detection.cs @@ -2,8 +2,11 @@ using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; +using SharpCompress.Archives.Tar; using SharpCompress.Common; using SharpCompress.Factories; +using SharpCompress.IO; +using SharpCompress.Providers; using SharpCompress.Readers; namespace SharpCompress.Archives; @@ -163,6 +166,15 @@ public static partial class ArchiveFactory if (isArchive) { + stream.Seek(startPosition, SeekOrigin.Begin); + if ( + await IsCompressedTarAsync(stream, factory, readerOptions, cancellationToken) + .ConfigureAwait(false) + ) + { + continue; + } + stream.Seek(startPosition, SeekOrigin.Begin); return factory; } @@ -253,6 +265,12 @@ public static partial class ArchiveFactory if (isArchive) { + stream.Seek(startPosition, SeekOrigin.Begin); + if (IsCompressedTar(stream, factory, readerOptions)) + { + continue; + } + stream.Seek(startPosition, SeekOrigin.Begin); return factory; } @@ -261,4 +279,85 @@ public static partial class ArchiveFactory stream.Seek(startPosition, SeekOrigin.Begin); return null; } + + private static bool IsCompressedTar( + Stream stream, + IFactory factory, + ReaderOptions readerOptions + ) => + GetCompressedTarType(factory) is { } compressionType + && IsCompressedTar(stream, readerOptions, compressionType); + + private static bool IsCompressedTar( + Stream stream, + ReaderOptions readerOptions, + CompressionType compressionType + ) + { + using var nonDisposingStream = SharpCompressStream.CreateNonDisposing(stream); + var testStream = + compressionType == CompressionType.GZip + ? readerOptions.Providers.CreateDecompressStream( + compressionType, + nonDisposingStream, + CompressionContext + .FromStream(nonDisposingStream) + .WithReaderOptions(readerOptions) + ) + : readerOptions.Providers.CreateDecompressStream( + compressionType, + nonDisposingStream + ); + + return TarArchive.IsTarFile(testStream); + } + + private static async ValueTask IsCompressedTarAsync( + Stream stream, + IFactory factory, + ReaderOptions readerOptions, + CancellationToken cancellationToken + ) => + GetCompressedTarType(factory) is { } compressionType + && await IsCompressedTarAsync(stream, readerOptions, compressionType, cancellationToken) + .ConfigureAwait(false); + + private static async ValueTask IsCompressedTarAsync( + Stream stream, + ReaderOptions readerOptions, + CompressionType compressionType, + CancellationToken cancellationToken + ) + { + using var nonDisposingStream = SharpCompressStream.CreateNonDisposing(stream); + var testStream = + compressionType == CompressionType.GZip + ? await readerOptions + .Providers.CreateDecompressStreamAsync( + compressionType, + nonDisposingStream, + CompressionContext + .FromStream(nonDisposingStream) + .WithReaderOptions(readerOptions), + cancellationToken + ) + .ConfigureAwait(false) + : await readerOptions + .Providers.CreateDecompressStreamAsync( + compressionType, + nonDisposingStream, + cancellationToken + ) + .ConfigureAwait(false); + + return await TarArchive.IsTarFileAsync(testStream, cancellationToken).ConfigureAwait(false); + } + + private static CompressionType? GetCompressedTarType(IFactory factory) => + factory switch + { + GZipFactory => CompressionType.GZip, + LzwFactory => CompressionType.Lzw, + _ => null, + }; } diff --git a/tests/SharpCompress.Test/AsyncParityAndCancellationTests.cs b/tests/SharpCompress.Test/AsyncParityAndCancellationTests.cs index 49beacfa..f4812060 100644 --- a/tests/SharpCompress.Test/AsyncParityAndCancellationTests.cs +++ b/tests/SharpCompress.Test/AsyncParityAndCancellationTests.cs @@ -19,7 +19,6 @@ public class AsyncParityAndCancellationTests : TestBase [Theory] [InlineData("Zip.deflate.zip")] [InlineData("Tar.tar")] - [InlineData("Tar.tar.gz")] [InlineData("Rar.rar")] [InlineData("7Zip.nonsolid.7z")] public async Task ArchiveAsyncEntries_ShouldMatchSyncEntries(string archiveName)