mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-22 15:04:43 +00:00
more tests and a removal of a test
This commit is contained in:
@@ -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<bool> 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<bool> 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,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user