add seekable checks

This commit is contained in:
Adam Hathcock
2026-04-23 09:56:26 +01:00
parent d3ff2ebe0a
commit 3ed94dd462
14 changed files with 112 additions and 0 deletions

View File

@@ -99,6 +99,8 @@ public static partial class ArchiveFactory
throw new ArchiveOperationException("No streams");
}
EnsureSeekable(streamsArray);
var firstStream = streamsArray[0];
if (streamsArray.Count == 1)
{

View File

@@ -13,6 +13,22 @@ namespace SharpCompress.Archives;
public static partial class ArchiveFactory
{
internal static void EnsureSeekable(Stream stream)
{
if (stream is null || !stream.CanSeek)
{
throw new ArgumentException("Stream must be seekable", nameof(stream));
}
}
internal static void EnsureSeekable(IReadOnlyList<Stream> streams)
{
foreach (var stream in streams)
{
EnsureSeekable(stream);
}
}
public static IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null)
{
readerOptions ??= ReaderOptions.ForExternalStream;
@@ -80,6 +96,8 @@ public static partial class ArchiveFactory
throw new ArchiveOperationException("No streams");
}
EnsureSeekable(streamsArray);
var firstStream = streamsArray[0];
if (streamsArray.Count == 1)
{

View File

@@ -77,6 +77,7 @@ public partial class GZipArchive
)
{
streams.NotNull(nameof(streams));
SharpCompress.Archives.ArchiveFactory.EnsureSeekable(streams);
var strms = streams;
return new GZipArchive(
new SourceStream(

View File

@@ -92,6 +92,7 @@ public partial class RarArchive
)
{
streams.NotNull(nameof(streams));
SharpCompress.Archives.ArchiveFactory.EnsureSeekable(streams);
var strms = streams;
return new RarArchive(
new SourceStream(

View File

@@ -72,6 +72,7 @@ public partial class SevenZipArchive
)
{
streams.NotNull(nameof(streams));
SharpCompress.Archives.ArchiveFactory.EnsureSeekable(streams);
var strms = streams;
return new SevenZipArchive(
new SourceStream(

View File

@@ -67,6 +67,7 @@ public partial class TarArchive
)
{
streams.NotNull(nameof(streams));
SharpCompress.Archives.ArchiveFactory.EnsureSeekable(streams);
var strms = streams;
var sourceStream = new SourceStream(
strms[0],
@@ -103,6 +104,10 @@ public partial class TarArchive
)
{
stream.NotNull(nameof(stream));
if (!stream.CanSeek)
{
throw new ArgumentException("Stream must be seekable", nameof(stream));
}
var sourceStream = new SourceStream(
stream,
i => null,
@@ -159,6 +164,7 @@ public partial class TarArchive
{
cancellationToken.ThrowIfCancellationRequested();
streams.NotNull(nameof(streams));
SharpCompress.Archives.ArchiveFactory.EnsureSeekable(streams);
var strms = streams;
var sourceStream = new SourceStream(
strms[0],

View File

@@ -68,6 +68,7 @@ public partial class ZipArchive
)
{
streams.NotNull(nameof(streams));
SharpCompress.Archives.ArchiveFactory.EnsureSeekable(streams);
var strms = streams;
return new ZipArchive(
new SourceStream(
@@ -132,6 +133,7 @@ public partial class ZipArchive
)
{
cancellationToken.ThrowIfCancellationRequested();
SharpCompress.Archives.ArchiveFactory.EnsureSeekable(streams);
return new((IWritableAsyncArchive<ZipWriterOptions>)OpenArchive(streams, readerOptions));
}