mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-25 08:24:58 +00:00
65 lines
2.0 KiB
C#
65 lines
2.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using SharpCompress.Common;
|
|
using SharpCompress.Factories;
|
|
using SharpCompress.IO;
|
|
|
|
namespace SharpCompress.Readers;
|
|
|
|
public static class ReaderFactory
|
|
{
|
|
/// <summary>
|
|
/// Opens a Reader for Non-seeking usage
|
|
/// </summary>
|
|
/// <param name="stream"></param>
|
|
/// <param name="options"></param>
|
|
/// <returns></returns>
|
|
public static IReader Open(Stream stream, ReaderOptions? options = null)
|
|
{
|
|
stream.CheckNotNull(nameof(stream));
|
|
options ??= new ReaderOptions() { LeaveStreamOpen = false };
|
|
|
|
var bStream = new SharpCompressStream(stream, bufferSize: options.BufferSize);
|
|
|
|
long pos = ((IStreamStack)bStream).GetPosition();
|
|
|
|
var factories = Factories.Factory.Factories.OfType<Factories.Factory>();
|
|
|
|
Factory? testedFactory = null;
|
|
|
|
if (!string.IsNullOrWhiteSpace(options.ExtensionHint))
|
|
{
|
|
testedFactory = factories.FirstOrDefault(a =>
|
|
a.GetSupportedExtensions()
|
|
.Contains(options.ExtensionHint, StringComparer.CurrentCultureIgnoreCase)
|
|
);
|
|
if (
|
|
testedFactory?.TryOpenReader(bStream, options, out var reader) == true
|
|
&& reader != null
|
|
)
|
|
{
|
|
return reader;
|
|
}
|
|
((IStreamStack)bStream).StackSeek(pos);
|
|
}
|
|
|
|
foreach (var factory in factories)
|
|
{
|
|
if (testedFactory == factory)
|
|
{
|
|
continue; // Already tested above
|
|
}
|
|
((IStreamStack)bStream).StackSeek(pos);
|
|
if (factory.TryOpenReader(bStream, options, out var reader) && reader != null)
|
|
{
|
|
return reader;
|
|
}
|
|
}
|
|
|
|
throw new InvalidFormatException(
|
|
"Cannot determine compressed stream type. Supported Reader Formats: Arc, Zip, GZip, BZip2, Tar, Rar, LZip, XZ, ZStandard"
|
|
);
|
|
}
|
|
}
|