From 05ebf2200912231e4fbc5b464e6b8f6a4d2f84f2 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 12:08:25 +0000 Subject: [PATCH 01/36] Start using the interface to draw distinction between async and sync --- .../Archives/GZip/GZipArchive.cs | 20 ++++++------- src/SharpCompress/Archives/Rar/RarArchive.cs | 22 +++++++------- .../Archives/SevenZip/SevenZipArchive.cs | 21 ++++++------- src/SharpCompress/Archives/Tar/TarArchive.cs | 20 ++++++------- src/SharpCompress/Archives/Zip/ZipArchive.cs | 30 ++++++++++++------- src/SharpCompress/Factories/AceFactory.cs | 2 +- src/SharpCompress/Factories/ArcFactory.cs | 2 +- src/SharpCompress/Factories/ArjFactory.cs | 2 +- src/SharpCompress/Factories/GZipFactory.cs | 2 +- src/SharpCompress/Factories/RarFactory.cs | 2 +- src/SharpCompress/Factories/TarFactory.cs | 2 +- src/SharpCompress/Factories/ZipFactory.cs | 2 +- .../Polyfills/AsyncEnumerableExtensions.cs | 16 ++++++++-- src/SharpCompress/Readers/Ace/AceReader.cs | 4 +-- src/SharpCompress/Readers/Arc/ArcReader.cs | 2 +- src/SharpCompress/Readers/Arj/ArjReader.cs | 4 +-- src/SharpCompress/Readers/GZip/GZipReader.cs | 4 +-- src/SharpCompress/Readers/Rar/RarReader.cs | 12 ++++---- src/SharpCompress/Readers/Tar/TarReader.cs | 2 +- src/SharpCompress/Readers/Zip/ZipReader.cs | 8 ++--- tests/SharpCompress.Test/GZip/AsyncTests.cs | 6 ++-- .../GZip/GZipArchiveAsyncTests.cs | 8 ++--- 22 files changed, 103 insertions(+), 90 deletions(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 7c8c08f7..6214c301 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -21,7 +21,7 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static GZipArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); @@ -32,7 +32,7 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static GZipArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); return new GZipArchive( @@ -49,7 +49,7 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static GZipArchive Open( + public static IArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -70,7 +70,7 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static GZipArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -88,7 +88,7 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static GZipArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -115,7 +115,7 @@ public class GZipArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(stream, readerOptions)); + return new((IAsyncArchive)Open(stream, readerOptions)); } /// @@ -131,7 +131,7 @@ public class GZipArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(fileInfo, readerOptions)); + return new((IAsyncArchive)Open(fileInfo, readerOptions)); } /// @@ -147,7 +147,7 @@ public class GZipArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(streams, readerOptions)); + return new((IAsyncArchive)Open(streams, readerOptions)); } /// @@ -163,7 +163,7 @@ public class GZipArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(fileInfos, readerOptions)); + return new((IAsyncArchive)Open(fileInfos, readerOptions)); } public static GZipArchive Create() => new(); @@ -344,6 +344,6 @@ public class GZipArchive : AbstractWritableArchive { var stream = Volumes.Single().Stream; stream.Position = 0; - return new(GZipReader.Open(stream)); + return new((IAsyncReader)GZipReader.Open(stream)); } } diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index 03b8d4d9..374755af 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -82,12 +82,12 @@ public class RarArchive : AbstractArchive volume.Stream.Position = 0; return volume.Stream; }); - return RarReader.Open(streams, ReaderOptions); + return (RarReader)RarReader.Open(streams, ReaderOptions); } var stream = Volumes.First().Stream; stream.Position = 0; - return RarReader.Open(stream, ReaderOptions); + return (RarReader)RarReader.Open(stream, ReaderOptions); } public override bool IsSolid => Volumes.First().IsSolidArchive; @@ -103,7 +103,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static RarArchive Open(string filePath, ReaderOptions? options = null) + public static IArchive Open(string filePath, ReaderOptions? options = null) { filePath.NotNullOrEmpty(nameof(filePath)); var fileInfo = new FileInfo(filePath); @@ -121,7 +121,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static RarArchive Open(FileInfo fileInfo, ReaderOptions? options = null) + public static IArchive Open(FileInfo fileInfo, ReaderOptions? options = null) { fileInfo.NotNull(nameof(fileInfo)); return new RarArchive( @@ -138,7 +138,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static RarArchive Open(Stream stream, ReaderOptions? options = null) + public static IArchive Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); @@ -155,7 +155,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static RarArchive Open( + public static IArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -176,7 +176,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static RarArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -202,7 +202,7 @@ public class RarArchive : AbstractArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(stream, readerOptions)); + return new((IAsyncArchive)Open(stream, readerOptions)); } /// @@ -218,7 +218,7 @@ public class RarArchive : AbstractArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(fileInfo, readerOptions)); + return new((IAsyncArchive)Open(fileInfo, readerOptions)); } /// @@ -234,7 +234,7 @@ public class RarArchive : AbstractArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(streams, readerOptions)); + return new((IAsyncArchive)Open(streams, readerOptions)); } /// @@ -250,7 +250,7 @@ public class RarArchive : AbstractArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(fileInfos, readerOptions)); + return new((IAsyncArchive)Open(fileInfos, readerOptions)); } public static bool IsRarFile(string filePath) => IsRarFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs index 43f49abe..5b606327 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs @@ -21,7 +21,7 @@ public class SevenZipArchive : AbstractArchive /// /// - public static SevenZipArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty("filePath"); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); @@ -32,7 +32,7 @@ public class SevenZipArchive : AbstractArchive /// /// - public static SevenZipArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull("fileInfo"); return new SevenZipArchive( @@ -49,7 +49,7 @@ public class SevenZipArchive : AbstractArchive /// /// - public static SevenZipArchive Open( + public static IArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -70,10 +70,7 @@ public class SevenZipArchive : AbstractArchive /// /// - public static SevenZipArchive Open( - IEnumerable streams, - ReaderOptions? readerOptions = null - ) + public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -91,7 +88,7 @@ public class SevenZipArchive : AbstractArchive /// /// - public static SevenZipArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull("stream"); @@ -118,7 +115,7 @@ public class SevenZipArchive : AbstractArchive @@ -134,7 +131,7 @@ public class SevenZipArchive : AbstractArchive @@ -150,7 +147,7 @@ public class SevenZipArchive : AbstractArchive @@ -166,7 +163,7 @@ public class SevenZipArchive : AbstractArchive diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index 1aeaf9a7..fe5298b1 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -22,7 +22,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static TarArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); @@ -33,7 +33,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static TarArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); return new TarArchive( @@ -50,7 +50,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static TarArchive Open( + public static IArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -71,7 +71,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static TarArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -89,7 +89,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static TarArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -116,7 +116,7 @@ public class TarArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(stream, readerOptions)); + return new((IAsyncArchive)Open(stream, readerOptions)); } /// @@ -132,7 +132,7 @@ public class TarArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(fileInfo, readerOptions)); + return new((IAsyncArchive)Open(fileInfo, readerOptions)); } /// @@ -148,7 +148,7 @@ public class TarArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(streams, readerOptions)); + return new((IAsyncArchive)Open(streams, readerOptions)); } /// @@ -164,7 +164,7 @@ public class TarArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(fileInfos, readerOptions)); + return new((IAsyncArchive)Open(fileInfos, readerOptions)); } public static bool IsTarFile(string filePath) => IsTarFile(new FileInfo(filePath)); @@ -371,6 +371,6 @@ public class TarArchive : AbstractWritableArchive { var stream = Volumes.Single().Stream; stream.Position = 0; - return new(TarReader.Open(stream)); + return new((IAsyncReader)TarReader.Open(stream)); } } diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index 756bc886..e7bb2466 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -43,7 +43,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ZipArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); @@ -54,7 +54,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ZipArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); return new ZipArchive( @@ -71,7 +71,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ZipArchive Open( + public static IArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -92,7 +92,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ZipArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -110,7 +110,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ZipArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -124,6 +124,16 @@ public class ZipArchive : AbstractWritableArchive ); } + public static ValueTask OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return new((IAsyncArchive)Open(path, readerOptions)); + } + /// /// Opens a ZipArchive asynchronously from a stream. /// @@ -137,7 +147,7 @@ public class ZipArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(stream, readerOptions)); + return new((IAsyncArchive)Open(stream, readerOptions)); } /// @@ -153,7 +163,7 @@ public class ZipArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(fileInfo, readerOptions)); + return new((IAsyncArchive)Open(fileInfo, readerOptions)); } /// @@ -169,7 +179,7 @@ public class ZipArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(streams, readerOptions)); + return new((IAsyncArchive)Open(streams, readerOptions)); } /// @@ -185,7 +195,7 @@ public class ZipArchive : AbstractWritableArchive ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(fileInfos, readerOptions)); + return new((IAsyncArchive)Open(fileInfos, readerOptions)); } public static bool IsZipFile( @@ -597,6 +607,6 @@ public class ZipArchive : AbstractWritableArchive { var stream = Volumes.Single().Stream; stream.Position = 0; - return new(ZipReader.Open(stream)); + return new((IAsyncReader)ZipReader.Open(stream)); } } diff --git a/src/SharpCompress/Factories/AceFactory.cs b/src/SharpCompress/Factories/AceFactory.cs index 95f647dd..eb896b54 100644 --- a/src/SharpCompress/Factories/AceFactory.cs +++ b/src/SharpCompress/Factories/AceFactory.cs @@ -36,7 +36,7 @@ namespace SharpCompress.Factories Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default - ) => new(AceReader.Open(stream, options)); + ) => new((IAsyncReader)AceReader.Open(stream, options)); public override ValueTask IsArchiveAsync( Stream stream, diff --git a/src/SharpCompress/Factories/ArcFactory.cs b/src/SharpCompress/Factories/ArcFactory.cs index 37984112..a337798a 100644 --- a/src/SharpCompress/Factories/ArcFactory.cs +++ b/src/SharpCompress/Factories/ArcFactory.cs @@ -48,7 +48,7 @@ namespace SharpCompress.Factories Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default - ) => new(ArcReader.Open(stream, options)); + ) => new((IAsyncReader)ArcReader.Open(stream, options)); public override ValueTask IsArchiveAsync( Stream stream, diff --git a/src/SharpCompress/Factories/ArjFactory.cs b/src/SharpCompress/Factories/ArjFactory.cs index 6e5f7a30..dce8bd94 100644 --- a/src/SharpCompress/Factories/ArjFactory.cs +++ b/src/SharpCompress/Factories/ArjFactory.cs @@ -39,7 +39,7 @@ namespace SharpCompress.Factories Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default - ) => new(ArjReader.Open(stream, options)); + ) => new((IAsyncReader)ArjReader.Open(stream, options)); public override ValueTask IsArchiveAsync( Stream stream, diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index 48f5c63e..5707e7eb 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -160,7 +160,7 @@ public class GZipFactory ) { cancellationToken.ThrowIfCancellationRequested(); - return new(GZipReader.Open(stream, options)); + return new((IAsyncReader)GZipReader.Open(stream, options)); } #endregion diff --git a/src/SharpCompress/Factories/RarFactory.cs b/src/SharpCompress/Factories/RarFactory.cs index fb9e03ab..97e7dd4a 100644 --- a/src/SharpCompress/Factories/RarFactory.cs +++ b/src/SharpCompress/Factories/RarFactory.cs @@ -115,7 +115,7 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade ) { cancellationToken.ThrowIfCancellationRequested(); - return new(RarReader.Open(stream, options)); + return new((IAsyncReader)RarReader.Open(stream, options)); } #endregion diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index 4e22e0bd..59608cf5 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -278,7 +278,7 @@ public class TarFactory ) { cancellationToken.ThrowIfCancellationRequested(); - return new(TarReader.Open(stream, options)); + return new((IAsyncReader)TarReader.Open(stream, options)); } #endregion diff --git a/src/SharpCompress/Factories/ZipFactory.cs b/src/SharpCompress/Factories/ZipFactory.cs index a9e62f14..25465351 100644 --- a/src/SharpCompress/Factories/ZipFactory.cs +++ b/src/SharpCompress/Factories/ZipFactory.cs @@ -202,7 +202,7 @@ public class ZipFactory ) { cancellationToken.ThrowIfCancellationRequested(); - return new(ZipReader.Open(stream, options)); + return new((IAsyncReader)ZipReader.Open(stream, options)); } #endregion diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index 785d4c32..c5b1d57c 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -42,7 +42,7 @@ public static class AsyncEnumerableExtensions return list; } - public async IAsyncEnumerable Cast() + public async IAsyncEnumerable CastAsync() where TResult : class { await foreach (var item in source) @@ -51,7 +51,7 @@ public static class AsyncEnumerableExtensions } } - public async ValueTask All(Func predicate) + public async ValueTask AllAsync(Func predicate) { await foreach (var item in source) { @@ -64,7 +64,7 @@ public static class AsyncEnumerableExtensions return true; } - public async IAsyncEnumerable Where(Func predicate) + public async IAsyncEnumerable WhereAsync(Func predicate) { await foreach (var item in source) { @@ -75,6 +75,16 @@ public static class AsyncEnumerableExtensions } } + public async ValueTask FirstAsync() + { + await foreach (var item in source) + { + return item; // Returns the very first item found + } + throw new InvalidOperationException("The source sequence is empty."); // Throws if the stream is empty + } + + public async ValueTask FirstOrDefaultAsync() { await foreach (var item in source) diff --git a/src/SharpCompress/Readers/Ace/AceReader.cs b/src/SharpCompress/Readers/Ace/AceReader.cs index 9c491bd1..c74ad401 100644 --- a/src/SharpCompress/Readers/Ace/AceReader.cs +++ b/src/SharpCompress/Readers/Ace/AceReader.cs @@ -50,7 +50,7 @@ namespace SharpCompress.Readers.Ace /// The stream containing the ACE archive. /// Reader options. /// An AceReader instance. - public static AceReader Open(Stream stream, ReaderOptions? options = null) + public static IReader Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new SingleVolumeAceReader(stream, options ?? new ReaderOptions()); @@ -62,7 +62,7 @@ namespace SharpCompress.Readers.Ace /// /// /// - public static AceReader Open(IEnumerable streams, ReaderOptions? options = null) + public static IReader Open(IEnumerable streams, ReaderOptions? options = null) { streams.NotNull(nameof(streams)); return new MultiVolumeAceReader(streams, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/Arc/ArcReader.cs b/src/SharpCompress/Readers/Arc/ArcReader.cs index 439cdb12..9f1eb68e 100644 --- a/src/SharpCompress/Readers/Arc/ArcReader.cs +++ b/src/SharpCompress/Readers/Arc/ArcReader.cs @@ -22,7 +22,7 @@ namespace SharpCompress.Readers.Arc /// /// /// - public static ArcReader Open(Stream stream, ReaderOptions? options = null) + public static IReader Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new ArcReader(stream, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/Arj/ArjReader.cs b/src/SharpCompress/Readers/Arj/ArjReader.cs index 439fb22c..831daf05 100644 --- a/src/SharpCompress/Readers/Arj/ArjReader.cs +++ b/src/SharpCompress/Readers/Arj/ArjReader.cs @@ -27,7 +27,7 @@ namespace SharpCompress.Readers.Arj /// /// /// - public static ArjReader Open(Stream stream, ReaderOptions? options = null) + public static IReader Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new SingleVolumeArjReader(stream, options ?? new ReaderOptions()); @@ -39,7 +39,7 @@ namespace SharpCompress.Readers.Arj /// /// /// - public static ArjReader Open(IEnumerable streams, ReaderOptions? options = null) + public static IReader Open(IEnumerable streams, ReaderOptions? options = null) { streams.NotNull(nameof(streams)); return new MultiVolumeArjReader(streams, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/GZip/GZipReader.cs b/src/SharpCompress/Readers/GZip/GZipReader.cs index e10d509a..c1fd8447 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.IO; using SharpCompress.Common; using SharpCompress.Common.GZip; @@ -20,7 +20,7 @@ public class GZipReader : AbstractReader /// /// /// - public static GZipReader Open(Stream stream, ReaderOptions? options = null) + public static IReader Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new GZipReader(stream, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index 9c6de26f..a3976fe5 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -40,24 +40,24 @@ public abstract class RarReader : AbstractReader public override RarVolume? Volume => volume; - public static RarReader Open(string filePath, ReaderOptions? options = null) + public static IReader Open(string filePath, ReaderOptions? options = null) { filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), options); } - public static RarReader Open(FileInfo fileInfo, ReaderOptions? options = null) + public static IReader Open(FileInfo fileInfo, ReaderOptions? options = null) { options ??= new ReaderOptions { LeaveStreamOpen = false }; return Open(fileInfo.OpenRead(), options); } - public static RarReader Open(IEnumerable filePaths, ReaderOptions? options = null) + public static IReader Open(IEnumerable filePaths, ReaderOptions? options = null) { return Open(filePaths.Select(x => new FileInfo(x)), options); } - public static RarReader Open(IEnumerable fileInfos, ReaderOptions? options = null) + public static IReader Open(IEnumerable fileInfos, ReaderOptions? options = null) { options ??= new ReaderOptions { LeaveStreamOpen = false }; return Open(fileInfos.Select(x => x.OpenRead()), options); @@ -69,7 +69,7 @@ public abstract class RarReader : AbstractReader /// /// /// - public static RarReader Open(Stream stream, ReaderOptions? options = null) + public static IReader Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new SingleVolumeRarReader(stream, options ?? new ReaderOptions()); @@ -81,7 +81,7 @@ public abstract class RarReader : AbstractReader /// /// /// - public static RarReader Open(IEnumerable streams, ReaderOptions? options = null) + public static IReader Open(IEnumerable streams, ReaderOptions? options = null) { streams.NotNull(nameof(streams)); return new MultiVolumeRarReader(streams, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/Tar/TarReader.cs b/src/SharpCompress/Readers/Tar/TarReader.cs index c9218885..e1375523 100644 --- a/src/SharpCompress/Readers/Tar/TarReader.cs +++ b/src/SharpCompress/Readers/Tar/TarReader.cs @@ -53,7 +53,7 @@ public class TarReader : AbstractReader /// /// /// - public static TarReader Open(Stream stream, ReaderOptions? options = null) + public static IReader Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); options = options ?? new ReaderOptions(); diff --git a/src/SharpCompress/Readers/Zip/ZipReader.cs b/src/SharpCompress/Readers/Zip/ZipReader.cs index d15fa7e0..3d9c7acc 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.cs @@ -45,17 +45,13 @@ public class ZipReader : AbstractReader /// /// /// - public static ZipReader Open(Stream stream, ReaderOptions? options = null) + public static IReader Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new ZipReader(stream, options ?? new ReaderOptions()); } - public static ZipReader Open( - Stream stream, - ReaderOptions? options, - IEnumerable entries - ) + public static IReader Open(Stream stream, ReaderOptions? options, IEnumerable entries) { stream.NotNull(nameof(stream)); return new ZipReader(stream, options ?? new ReaderOptions(), entries); diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index 3961ffc6..d324cf6a 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -103,7 +103,7 @@ public class AsyncTests : TestBase #else await using (var stream = File.Create(outputPath)) #endif - using (var writer = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate)) + await using (var writer = await WriterFactory.OpenAsync(stream, ArchiveType.Zip, CompressionType.Deflate)) { var testFile = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"); @@ -117,8 +117,8 @@ public class AsyncTests : TestBase // Verify the archive was created and contains the entry Assert.True(File.Exists(outputPath)); - await using var archive = ZipArchive.Open(outputPath); - Assert.Single(archive.Entries.Where(e => !e.IsDirectory)); + await using var archive = await ZipArchive.OpenAsync(outputPath); + Assert.Single(await archive.EntriesAsync.WhereAsync(e => !e.IsDirectory).ToListAsync()); } [Fact] diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index caa30ffa..ac86cc8c 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -47,9 +47,9 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) #endif - await using (var archive = GZipArchive.Open(stream)) + await using (var archive = await GZipArchive.OpenAsync(stream)) { - var entry = archive.Entries.First(); + var entry = await archive.EntriesAsync.FirstAsync(); await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); var size = entry.Size; @@ -74,8 +74,8 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); #endif - await using var archive = GZipArchive.Open(stream); - Assert.Throws(() => archive.AddEntry("jpg\\test.jpg", jpg)); + await using var archive = await GZipArchive.OpenAsync(stream); + await Assert.ThrowsAsync(() => archive.AddEntry("jpg\\test.jpg", jpg)); await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); } From c1f8580d89861d496523bded13f0853bf3a20d8e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 13:16:44 +0000 Subject: [PATCH 02/36] Remove unnecessary ValueTask wrappers from async factory methods Change return types from ValueTask to direct interface types (IAsyncArchive, IAsyncReader, IWriter) for wrapper methods that don't perform async work. This eliminates unnecessary async state machine allocations while maintaining the same public API behavior. Changes: - Interface definitions: Updated IArchiveFactory, IMultiArchiveFactory, IReaderFactory, IWriterFactory - Concrete factories: Updated archive factories (Zip, Tar, Rar, GZip, SevenZip) and reader-only factories (Ace, Arc, Arj) - Static factory methods: Updated ReaderFactory, ArchiveFactory, WriterFactory to use new signatures - Archive classes: Updated static OpenAsync methods in ZipArchive, TarArchive, RarArchive, SevenZipArchive, GZipArchive - Supporting changes: Updated Factory.cs and async polyfills Performance benefit: Reduced GC pressure by eliminating unnecessary state machine overhead for non-async wrapper methods. --- src/SharpCompress/Archives/ArchiveFactory.cs | 34 ++++++-------- .../Archives/AutoArchiveFactory.cs | 12 +++-- .../Archives/GZip/GZipArchive.cs | 16 +++---- src/SharpCompress/Archives/IArchiveFactory.cs | 5 +-- .../Archives/IMultiArchiveFactory.cs | 5 +-- src/SharpCompress/Archives/Rar/RarArchive.cs | 16 +++---- .../Archives/SevenZip/SevenZipArchive.cs | 16 +++---- src/SharpCompress/Archives/Tar/TarArchive.cs | 16 +++---- src/SharpCompress/Archives/Zip/ZipArchive.cs | 20 ++++----- src/SharpCompress/Factories/AceFactory.cs | 8 +++- src/SharpCompress/Factories/ArcFactory.cs | 8 +++- src/SharpCompress/Factories/ArjFactory.cs | 8 +++- src/SharpCompress/Factories/Factory.cs | 5 +-- src/SharpCompress/Factories/GZipFactory.cs | 44 ++++++++++++------- src/SharpCompress/Factories/RarFactory.cs | 28 +++++++----- .../Factories/SevenZipFactory.cs | 24 ++++++---- src/SharpCompress/Factories/TarFactory.cs | 32 +++++++++----- src/SharpCompress/Factories/ZipFactory.cs | 32 +++++++++----- .../Polyfills/AsyncEnumerableExtensions.cs | 1 - src/SharpCompress/Readers/IReaderFactory.cs | 3 +- src/SharpCompress/Readers/ReaderFactory.cs | 33 +++----------- src/SharpCompress/Writers/IWriterFactory.cs | 3 +- src/SharpCompress/Writers/WriterFactory.cs | 6 +-- tests/SharpCompress.Test/GZip/AsyncTests.cs | 8 +++- .../GZip/GZipArchiveAsyncTests.cs | 4 +- 25 files changed, 209 insertions(+), 178 deletions(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index e052c3e8..17a7f463 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -33,7 +33,7 @@ public static class ArchiveFactory /// /// /// - public static async ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -41,11 +41,8 @@ public static class ArchiveFactory { readerOptions ??= new ReaderOptions(); stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); - var factory = await FindFactoryAsync(stream, cancellationToken) - .ConfigureAwait(false); - return await factory - .OpenAsync(stream, readerOptions, cancellationToken) - .ConfigureAwait(false); + var factory = FindFactory(stream); + return factory.OpenAsync(stream, readerOptions, cancellationToken); } public static IWritableArchive Create(ArchiveType type) @@ -79,7 +76,7 @@ public static class ArchiveFactory /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( string filePath, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -107,7 +104,7 @@ public static class ArchiveFactory /// /// /// - public static async ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -115,9 +112,8 @@ public static class ArchiveFactory { options ??= new ReaderOptions { LeaveStreamOpen = false }; - var factory = await FindFactoryAsync(fileInfo, cancellationToken) - .ConfigureAwait(false); - return await factory.OpenAsync(fileInfo, options, cancellationToken).ConfigureAwait(false); + var factory = FindFactory(fileInfo); + return factory.OpenAsync(fileInfo, options, cancellationToken); } /// @@ -152,7 +148,7 @@ public static class ArchiveFactory /// /// /// - public static async ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IEnumerable fileInfos, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -168,16 +164,14 @@ public static class ArchiveFactory var fileInfo = filesArray[0]; if (filesArray.Length == 1) { - return await OpenAsync(fileInfo, options, cancellationToken).ConfigureAwait(false); + return OpenAsync(fileInfo, options, cancellationToken); } fileInfo.NotNull(nameof(fileInfo)); options ??= new ReaderOptions { LeaveStreamOpen = false }; var factory = FindFactory(fileInfo); - return await factory - .OpenAsync(filesArray, options, cancellationToken) - .ConfigureAwait(false); + return factory.OpenAsync(filesArray, options, cancellationToken); } /// @@ -212,7 +206,7 @@ public static class ArchiveFactory /// /// /// - public static async ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IEnumerable streams, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -229,16 +223,14 @@ public static class ArchiveFactory var firstStream = streamsArray[0]; if (streamsArray.Length == 1) { - return await OpenAsync(firstStream, options, cancellationToken).ConfigureAwait(false); + return OpenAsync(firstStream, options, cancellationToken); } firstStream.NotNull(nameof(firstStream)); options ??= new ReaderOptions(); var factory = FindFactory(firstStream); - return await factory - .OpenAsync(streamsArray, options, cancellationToken) - .ConfigureAwait(false); + return factory.OpenAsync(streamsArray, options, cancellationToken); } /// diff --git a/src/SharpCompress/Archives/AutoArchiveFactory.cs b/src/SharpCompress/Archives/AutoArchiveFactory.cs index 7751c7e0..a6d3855e 100644 --- a/src/SharpCompress/Archives/AutoArchiveFactory.cs +++ b/src/SharpCompress/Archives/AutoArchiveFactory.cs @@ -34,18 +34,22 @@ internal class AutoArchiveFactory : IArchiveFactory public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) => ArchiveFactory.Open(stream, readerOptions); - public async ValueTask OpenAsync( + public IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => await ArchiveFactory.OpenAsync(stream, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(stream, readerOptions); public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => ArchiveFactory.Open(fileInfo, readerOptions); - public async ValueTask OpenAsync( + public IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => await ArchiveFactory.OpenAsync(fileInfo, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfo, readerOptions); + } } diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 6214c301..82ea7f67 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -108,14 +108,14 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(stream, readerOptions)); + return (IAsyncArchive)Open(stream, readerOptions); } /// @@ -124,14 +124,14 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfo, readerOptions)); + return (IAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -140,14 +140,14 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(streams, readerOptions)); + return (IAsyncArchive)Open(streams, readerOptions); } /// @@ -156,14 +156,14 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfos, readerOptions)); + return (IAsyncArchive)Open(fileInfos, readerOptions); } public static GZipArchive Create() => new(); diff --git a/src/SharpCompress/Archives/IArchiveFactory.cs b/src/SharpCompress/Archives/IArchiveFactory.cs index 1c1253f6..06ea7360 100644 --- a/src/SharpCompress/Archives/IArchiveFactory.cs +++ b/src/SharpCompress/Archives/IArchiveFactory.cs @@ -1,6 +1,5 @@ using System.IO; using System.Threading; -using System.Threading.Tasks; using SharpCompress.Factories; using SharpCompress.Readers; @@ -34,7 +33,7 @@ public interface IArchiveFactory : IFactory /// An open, readable and seekable stream. /// reading options. /// Cancellation token. - ValueTask OpenAsync( + IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -53,7 +52,7 @@ public interface IArchiveFactory : IFactory /// the file to open. /// reading options. /// Cancellation token. - ValueTask OpenAsync( + IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Archives/IMultiArchiveFactory.cs b/src/SharpCompress/Archives/IMultiArchiveFactory.cs index 4fa94d7f..b54b32ec 100644 --- a/src/SharpCompress/Archives/IMultiArchiveFactory.cs +++ b/src/SharpCompress/Archives/IMultiArchiveFactory.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.IO; using System.Threading; -using System.Threading.Tasks; using SharpCompress.Factories; using SharpCompress.Readers; @@ -35,7 +34,7 @@ public interface IMultiArchiveFactory : IFactory /// /// reading options. /// Cancellation token. - ValueTask OpenAsync( + IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -54,7 +53,7 @@ public interface IMultiArchiveFactory : IFactory /// /// reading options. /// Cancellation token. - ValueTask OpenAsync( + IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index 374755af..1cb7b00a 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -195,14 +195,14 @@ public class RarArchive : AbstractArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(stream, readerOptions)); + return (IAsyncArchive)Open(stream, readerOptions); } /// @@ -211,14 +211,14 @@ public class RarArchive : AbstractArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfo, readerOptions)); + return (IAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -227,14 +227,14 @@ public class RarArchive : AbstractArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(streams, readerOptions)); + return (IAsyncArchive)Open(streams, readerOptions); } /// @@ -243,14 +243,14 @@ public class RarArchive : AbstractArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfos, readerOptions)); + return (IAsyncArchive)Open(fileInfos, readerOptions); } public static bool IsRarFile(string filePath) => IsRarFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs index 5b606327..8532e837 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs @@ -108,14 +108,14 @@ public class SevenZipArchive : AbstractArchive /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(stream, readerOptions)); + return (IAsyncArchive)Open(stream, readerOptions); } /// @@ -124,14 +124,14 @@ public class SevenZipArchive : AbstractArchive /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfo, readerOptions)); + return (IAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -140,14 +140,14 @@ public class SevenZipArchive : AbstractArchive /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(streams, readerOptions)); + return (IAsyncArchive)Open(streams, readerOptions); } /// @@ -156,14 +156,14 @@ public class SevenZipArchive : AbstractArchive /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfos, readerOptions)); + return (IAsyncArchive)Open(fileInfos, readerOptions); } /// diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index fe5298b1..76d47d0b 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -109,14 +109,14 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(stream, readerOptions)); + return (IAsyncArchive)Open(stream, readerOptions); } /// @@ -125,14 +125,14 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfo, readerOptions)); + return (IAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -141,14 +141,14 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(streams, readerOptions)); + return (IAsyncArchive)Open(streams, readerOptions); } /// @@ -157,14 +157,14 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfos, readerOptions)); + return (IAsyncArchive)Open(fileInfos, readerOptions); } public static bool IsTarFile(string filePath) => IsTarFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index e7bb2466..9a0fbac5 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -124,14 +124,14 @@ public class ZipArchive : AbstractWritableArchive ); } - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(path, readerOptions)); + return (IAsyncArchive)Open(path, readerOptions); } /// @@ -140,14 +140,14 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(stream, readerOptions)); + return (IAsyncArchive)Open(stream, readerOptions); } /// @@ -156,14 +156,14 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfo, readerOptions)); + return (IAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -172,14 +172,14 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(streams, readerOptions)); + return (IAsyncArchive)Open(streams, readerOptions); } /// @@ -188,14 +188,14 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static ValueTask OpenAsync( + public static IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncArchive)Open(fileInfos, readerOptions)); + return (IAsyncArchive)Open(fileInfos, readerOptions); } public static bool IsZipFile( diff --git a/src/SharpCompress/Factories/AceFactory.cs b/src/SharpCompress/Factories/AceFactory.cs index eb896b54..82ebe82e 100644 --- a/src/SharpCompress/Factories/AceFactory.cs +++ b/src/SharpCompress/Factories/AceFactory.cs @@ -32,11 +32,15 @@ namespace SharpCompress.Factories public IReader OpenReader(Stream stream, ReaderOptions? options) => AceReader.Open(stream, options); - public ValueTask OpenReaderAsync( + public IAsyncReader OpenReaderAsync( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default - ) => new((IAsyncReader)AceReader.Open(stream, options)); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)AceReader.Open(stream, options); + } public override ValueTask IsArchiveAsync( Stream stream, diff --git a/src/SharpCompress/Factories/ArcFactory.cs b/src/SharpCompress/Factories/ArcFactory.cs index a337798a..acd361c8 100644 --- a/src/SharpCompress/Factories/ArcFactory.cs +++ b/src/SharpCompress/Factories/ArcFactory.cs @@ -44,11 +44,15 @@ namespace SharpCompress.Factories public IReader OpenReader(Stream stream, ReaderOptions? options) => ArcReader.Open(stream, options); - public ValueTask OpenReaderAsync( + public IAsyncReader OpenReaderAsync( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default - ) => new((IAsyncReader)ArcReader.Open(stream, options)); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)ArcReader.Open(stream, options); + } public override ValueTask IsArchiveAsync( Stream stream, diff --git a/src/SharpCompress/Factories/ArjFactory.cs b/src/SharpCompress/Factories/ArjFactory.cs index dce8bd94..245fea5f 100644 --- a/src/SharpCompress/Factories/ArjFactory.cs +++ b/src/SharpCompress/Factories/ArjFactory.cs @@ -35,11 +35,15 @@ namespace SharpCompress.Factories public IReader OpenReader(Stream stream, ReaderOptions? options) => ArjReader.Open(stream, options); - public ValueTask OpenReaderAsync( + public IAsyncReader OpenReaderAsync( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default - ) => new((IAsyncReader)ArjReader.Open(stream, options)); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)ArjReader.Open(stream, options); + } public override ValueTask IsArchiveAsync( Stream stream, diff --git a/src/SharpCompress/Factories/Factory.cs b/src/SharpCompress/Factories/Factory.cs index f28f3d24..a77eec2e 100644 --- a/src/SharpCompress/Factories/Factory.cs +++ b/src/SharpCompress/Factories/Factory.cs @@ -133,10 +133,7 @@ public abstract class Factory : IFactory ) { ((IStreamStack)stream).StackSeek(pos); - return ( - true, - await readerFactory.OpenReaderAsync(stream, options, cancellationToken) - ); + return (true, readerFactory.OpenReaderAsync(stream, options, cancellationToken)); } } diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index 5707e7eb..dd36a78c 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -65,11 +65,11 @@ public class GZipFactory GZipArchive.Open(stream, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => GZipArchive.OpenAsync(stream, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(stream, readerOptions); public override ValueTask IsArchiveAsync( Stream stream, @@ -78,15 +78,15 @@ public class GZipFactory ) => new(IsArchive(stream, password, bufferSize)); /// - public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => - GZipArchive.Open(fileInfo, readerOptions); - - /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => GZipArchive.OpenAsync(fileInfo, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfo, readerOptions); + } #endregion @@ -97,22 +97,26 @@ public class GZipFactory GZipArchive.Open(streams, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => GZipArchive.OpenAsync(streams, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(streams, readerOptions); /// public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => GZipArchive.Open(fileInfos, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => GZipArchive.OpenAsync(fileInfos, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfos, readerOptions); + } #endregion @@ -153,16 +157,20 @@ public class GZipFactory GZipReader.Open(stream, options); /// - public ValueTask OpenReaderAsync( + public IAsyncReader OpenReaderAsync( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncReader)GZipReader.Open(stream, options)); + return (IAsyncReader)GZipReader.Open(stream, options); } + /// + public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => + GZipArchive.Open(fileInfo, readerOptions); + #endregion #region IWriterFactory @@ -178,14 +186,18 @@ public class GZipFactory } /// - public ValueTask OpenAsync( + public IWriter OpenAsync( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(stream, writerOptions)); + if (writerOptions.CompressionType != CompressionType.GZip) + { + throw new InvalidFormatException("GZip archives only support GZip compression type."); + } + return Open(stream, writerOptions); } #endregion diff --git a/src/SharpCompress/Factories/RarFactory.cs b/src/SharpCompress/Factories/RarFactory.cs index 97e7dd4a..841cc7ff 100644 --- a/src/SharpCompress/Factories/RarFactory.cs +++ b/src/SharpCompress/Factories/RarFactory.cs @@ -50,22 +50,26 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade RarArchive.Open(stream, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => RarArchive.OpenAsync(stream, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(stream, readerOptions); /// public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => RarArchive.Open(fileInfo, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => RarArchive.OpenAsync(fileInfo, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfo, readerOptions); + } public override ValueTask IsArchiveAsync( Stream stream, @@ -82,22 +86,26 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade RarArchive.Open(streams, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => RarArchive.OpenAsync(streams, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(streams, readerOptions); /// public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => RarArchive.Open(fileInfos, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => RarArchive.OpenAsync(fileInfos, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfos, readerOptions); + } #endregion @@ -108,14 +116,14 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade RarReader.Open(stream, options); /// - public ValueTask OpenReaderAsync( + public IAsyncReader OpenReaderAsync( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncReader)RarReader.Open(stream, options)); + return (IAsyncReader)RarReader.Open(stream, options); } #endregion diff --git a/src/SharpCompress/Factories/SevenZipFactory.cs b/src/SharpCompress/Factories/SevenZipFactory.cs index c387e3b3..bb4e1093 100644 --- a/src/SharpCompress/Factories/SevenZipFactory.cs +++ b/src/SharpCompress/Factories/SevenZipFactory.cs @@ -45,22 +45,26 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory SevenZipArchive.Open(stream, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => SevenZipArchive.OpenAsync(stream, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(stream, readerOptions); /// public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => SevenZipArchive.Open(fileInfo, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => SevenZipArchive.OpenAsync(fileInfo, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfo, readerOptions); + } public override ValueTask IsArchiveAsync( Stream stream, @@ -77,22 +81,26 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory SevenZipArchive.Open(streams, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => SevenZipArchive.OpenAsync(streams, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(streams, readerOptions); /// public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => SevenZipArchive.Open(fileInfos, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => SevenZipArchive.OpenAsync(fileInfos, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfos, readerOptions); + } #endregion diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index 59608cf5..0b8038af 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -76,22 +76,26 @@ public class TarFactory TarArchive.Open(stream, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => TarArchive.OpenAsync(stream, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(stream, readerOptions); /// public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => TarArchive.Open(fileInfo, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => TarArchive.OpenAsync(fileInfo, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfo, readerOptions); + } #endregion @@ -102,22 +106,26 @@ public class TarFactory TarArchive.Open(streams, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => TarArchive.OpenAsync(streams, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(streams, readerOptions); /// public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => TarArchive.Open(fileInfos, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => TarArchive.OpenAsync(fileInfos, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfos, readerOptions); + } #endregion @@ -271,14 +279,14 @@ public class TarFactory TarReader.Open(stream, options); /// - public ValueTask OpenReaderAsync( + public IAsyncReader OpenReaderAsync( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncReader)TarReader.Open(stream, options)); + return (IAsyncReader)TarReader.Open(stream, options); } #endregion @@ -290,14 +298,14 @@ public class TarFactory new TarWriter(stream, new TarWriterOptions(writerOptions)); /// - public ValueTask OpenAsync( + public IWriter OpenAsync( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(stream, writerOptions)); + return Open(stream, writerOptions); } #endregion diff --git a/src/SharpCompress/Factories/ZipFactory.cs b/src/SharpCompress/Factories/ZipFactory.cs index 25465351..42047ca4 100644 --- a/src/SharpCompress/Factories/ZipFactory.cs +++ b/src/SharpCompress/Factories/ZipFactory.cs @@ -143,22 +143,26 @@ public class ZipFactory ZipArchive.Open(stream, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => ZipArchive.OpenAsync(stream, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(stream, readerOptions); /// public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => ZipArchive.Open(fileInfo, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => ZipArchive.OpenAsync(fileInfo, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfo, readerOptions); + } #endregion @@ -169,22 +173,26 @@ public class ZipFactory ZipArchive.Open(streams, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => ZipArchive.OpenAsync(streams, readerOptions, cancellationToken); + ) => (IAsyncArchive)Open(streams, readerOptions); /// public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => ZipArchive.Open(fileInfos, readerOptions); /// - public ValueTask OpenAsync( + public IAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default - ) => ZipArchive.OpenAsync(fileInfos, readerOptions, cancellationToken); + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfos, readerOptions); + } #endregion @@ -195,14 +203,14 @@ public class ZipFactory ZipReader.Open(stream, options); /// - public ValueTask OpenReaderAsync( + public IAsyncReader OpenReaderAsync( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new((IAsyncReader)ZipReader.Open(stream, options)); + return (IAsyncReader)ZipReader.Open(stream, options); } #endregion @@ -214,14 +222,14 @@ public class ZipFactory new ZipWriter(stream, new ZipWriterOptions(writerOptions)); /// - public ValueTask OpenAsync( + public IWriter OpenAsync( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return new(Open(stream, writerOptions)); + return Open(stream, writerOptions); } #endregion diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index c5b1d57c..aedc1d28 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -84,7 +84,6 @@ public static class AsyncEnumerableExtensions throw new InvalidOperationException("The source sequence is empty."); // Throws if the stream is empty } - public async ValueTask FirstOrDefaultAsync() { await foreach (var item in source) diff --git a/src/SharpCompress/Readers/IReaderFactory.cs b/src/SharpCompress/Readers/IReaderFactory.cs index 4757644b..ca38b2ba 100644 --- a/src/SharpCompress/Readers/IReaderFactory.cs +++ b/src/SharpCompress/Readers/IReaderFactory.cs @@ -1,6 +1,5 @@ using System.IO; using System.Threading; -using System.Threading.Tasks; namespace SharpCompress.Readers; @@ -13,7 +12,7 @@ public interface IReaderFactory : Factories.IFactory /// /// IReader OpenReader(Stream stream, ReaderOptions? options); - ValueTask OpenReaderAsync( + IAsyncReader OpenReaderAsync( Stream stream, ReaderOptions? options, CancellationToken cancellationToken diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index 6102f146..7bf72fe9 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -24,7 +24,7 @@ public static class ReaderFactory /// /// /// - public static ValueTask OpenAsync( + public static IAsyncReader OpenAsync( string filePath, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -47,7 +47,7 @@ public static class ReaderFactory /// /// /// - public static ValueTask OpenAsync( + public static IAsyncReader OpenAsync( FileInfo fileInfo, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -110,7 +110,7 @@ public static class ReaderFactory ); } - public static async ValueTask OpenAsync( + public static IAsyncReader OpenAsync( Stream stream, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -136,19 +136,10 @@ public static class ReaderFactory if (testedFactory is IReaderFactory readerFactory) { ((IStreamStack)bStream).StackSeek(pos); - if ( - await testedFactory.IsArchiveAsync( - bStream, - options.Password, - options.BufferSize, - cancellationToken - ) - ) + if (testedFactory.IsArchive(bStream)) { ((IStreamStack)bStream).StackSeek(pos); - return await readerFactory - .OpenReaderAsync(bStream, options, cancellationToken) - .ConfigureAwait(false); + return readerFactory.OpenReaderAsync(bStream, options, cancellationToken); } } ((IStreamStack)bStream).StackSeek(pos); @@ -161,20 +152,10 @@ public static class ReaderFactory continue; // Already tested above } ((IStreamStack)bStream).StackSeek(pos); - if ( - factory is IReaderFactory readerFactory - && await factory.IsArchiveAsync( - bStream, - options.Password, - options.BufferSize, - cancellationToken - ) - ) + if (factory is IReaderFactory readerFactory && factory.IsArchive(bStream)) { ((IStreamStack)bStream).StackSeek(pos); - return await readerFactory - .OpenReaderAsync(bStream, options, cancellationToken) - .ConfigureAwait(false); + return readerFactory.OpenReaderAsync(bStream, options, cancellationToken); } } diff --git a/src/SharpCompress/Writers/IWriterFactory.cs b/src/SharpCompress/Writers/IWriterFactory.cs index f933e819..2c68c249 100644 --- a/src/SharpCompress/Writers/IWriterFactory.cs +++ b/src/SharpCompress/Writers/IWriterFactory.cs @@ -1,6 +1,5 @@ using System.IO; using System.Threading; -using System.Threading.Tasks; using SharpCompress.Factories; namespace SharpCompress.Writers; @@ -9,7 +8,7 @@ public interface IWriterFactory : IFactory { IWriter Open(Stream stream, WriterOptions writerOptions); - ValueTask OpenAsync( + IWriter OpenAsync( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index b7ff5cd4..52b62e7d 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -31,7 +31,7 @@ public static class WriterFactory /// Writer options. /// Cancellation token. /// A task that returns an IWriter. - public static async ValueTask OpenAsync( + public static IWriter OpenAsync( Stream stream, ArchiveType archiveType, WriterOptions writerOptions, @@ -44,9 +44,7 @@ public static class WriterFactory if (factory != null) { - return await factory - .OpenAsync(stream, writerOptions, cancellationToken) - .ConfigureAwait(false); + return factory.OpenAsync(stream, writerOptions, cancellationToken); } throw new NotSupportedException("Archive Type does not have a Writer: " + archiveType); diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index d324cf6a..d000798e 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -103,7 +103,13 @@ public class AsyncTests : TestBase #else await using (var stream = File.Create(outputPath)) #endif - await using (var writer = await WriterFactory.OpenAsync(stream, ArchiveType.Zip, CompressionType.Deflate)) + await using ( + var writer = await WriterFactory.OpenAsync( + stream, + ArchiveType.Zip, + CompressionType.Deflate + ) + ) { var testFile = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"); diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index ac86cc8c..004d4ba8 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -75,7 +75,9 @@ public class GZipArchiveAsyncTests : ArchiveTests await using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); #endif await using var archive = await GZipArchive.OpenAsync(stream); - await Assert.ThrowsAsync(() => archive.AddEntry("jpg\\test.jpg", jpg)); + await Assert.ThrowsAsync(() => + archive.AddEntry("jpg\\test.jpg", jpg) + ); await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); } From 0615d17b8b232a0c0e7e8fb14ab45360bae64497 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 13:45:21 +0000 Subject: [PATCH 03/36] fix async interfacing for open --- src/SharpCompress/Archives/AbstractArchive.cs | 29 +++++++-- src/SharpCompress/Archives/ArchiveFactory.cs | 24 +++---- .../Archives/AutoArchiveFactory.cs | 7 +- src/SharpCompress/Archives/IArchiveFactory.cs | 4 +- .../Archives/IMultiArchiveFactory.cs | 4 +- src/SharpCompress/Archives/Zip/ZipArchive.cs | 2 +- src/SharpCompress/Factories/GZipFactory.cs | 10 +-- src/SharpCompress/Factories/RarFactory.cs | 10 +-- .../Factories/SevenZipFactory.cs | 10 +-- src/SharpCompress/Factories/TarFactory.cs | 10 +-- src/SharpCompress/Factories/ZipFactory.cs | 10 +-- .../Polyfills/AsyncEnumerableExtensions.cs | 64 +++++++++---------- 12 files changed, 87 insertions(+), 97 deletions(-) diff --git a/src/SharpCompress/Archives/AbstractArchive.cs b/src/SharpCompress/Archives/AbstractArchive.cs index 12ab15a6..0412f93e 100644 --- a/src/SharpCompress/Archives/AbstractArchive.cs +++ b/src/SharpCompress/Archives/AbstractArchive.cs @@ -1,6 +1,7 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; +using SharpCompress; using SharpCompress.Common; using SharpCompress.IO; using SharpCompress.Readers; @@ -187,10 +188,26 @@ public abstract class AbstractArchive : IArchive, IAsyncArchive } public virtual IAsyncEnumerable EntriesAsync => _lazyEntriesAsync; - IAsyncEnumerable IAsyncArchive.EntriesAsync => - EntriesAsync.Cast(); - public IAsyncEnumerable VolumesAsync => _lazyVolumesAsync.Cast(); + private async IAsyncEnumerable EntriesAsyncCast() + { + await foreach (var entry in EntriesAsync) + { + yield return entry; + } + } + + IAsyncEnumerable IAsyncArchive.EntriesAsync => EntriesAsyncCast(); + + private async IAsyncEnumerable VolumesAsyncCast() + { + await foreach (var volume in VolumesAsync) + { + yield return volume; + } + } + + public IAsyncEnumerable VolumesAsync => VolumesAsyncCast(); public async ValueTask ExtractAllEntriesAsync() { @@ -209,14 +226,14 @@ public abstract class AbstractArchive : IArchive, IAsyncArchive public async ValueTask IsCompleteAsync() { await EnsureEntriesLoadedAsync(); - return await EntriesAsync.All(x => x.IsComplete); + return await EntriesAsync.AllAsync(x => x.IsComplete); } public async ValueTask TotalSizeAsync() => - await EntriesAsync.Aggregate(0L, (total, cf) => total + cf.CompressedSize); + await EntriesAsync.AggregateAsync(0L, (total, cf) => total + cf.CompressedSize); public async ValueTask TotalUncompressSizeAsync() => - await EntriesAsync.Aggregate(0L, (total, cf) => total + cf.Size); + await EntriesAsync.AggregateAsync(0L, (total, cf) => total + cf.Size); #endregion } diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index 17a7f463..fb1addc0 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -33,7 +33,7 @@ public static class ArchiveFactory /// /// /// - public static IAsyncArchive OpenAsync( + public static async ValueTask OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -41,8 +41,8 @@ public static class ArchiveFactory { readerOptions ??= new ReaderOptions(); stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); - var factory = FindFactory(stream); - return factory.OpenAsync(stream, readerOptions, cancellationToken); + var factory = await FindFactoryAsync(stream, cancellationToken); + return factory.OpenAsync(stream, readerOptions); } public static IWritableArchive Create(ArchiveType type) @@ -76,7 +76,7 @@ public static class ArchiveFactory /// /// /// - public static IAsyncArchive OpenAsync( + public static ValueTask OpenAsync( string filePath, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -104,7 +104,7 @@ public static class ArchiveFactory /// /// /// - public static IAsyncArchive OpenAsync( + public static async ValueTask OpenAsync( FileInfo fileInfo, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -112,7 +112,7 @@ public static class ArchiveFactory { options ??= new ReaderOptions { LeaveStreamOpen = false }; - var factory = FindFactory(fileInfo); + var factory = await FindFactoryAsync(fileInfo, cancellationToken); return factory.OpenAsync(fileInfo, options, cancellationToken); } @@ -148,7 +148,7 @@ public static class ArchiveFactory /// /// /// - public static IAsyncArchive OpenAsync( + public static async ValueTask OpenAsync( IEnumerable fileInfos, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -164,13 +164,13 @@ public static class ArchiveFactory var fileInfo = filesArray[0]; if (filesArray.Length == 1) { - return OpenAsync(fileInfo, options, cancellationToken); + return await OpenAsync(fileInfo, options, cancellationToken); } fileInfo.NotNull(nameof(fileInfo)); options ??= new ReaderOptions { LeaveStreamOpen = false }; - var factory = FindFactory(fileInfo); + var factory = await FindFactoryAsync(fileInfo, cancellationToken); return factory.OpenAsync(filesArray, options, cancellationToken); } @@ -206,7 +206,7 @@ public static class ArchiveFactory /// /// /// - public static IAsyncArchive OpenAsync( + public static async ValueTask OpenAsync( IEnumerable streams, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -223,14 +223,14 @@ public static class ArchiveFactory var firstStream = streamsArray[0]; if (streamsArray.Length == 1) { - return OpenAsync(firstStream, options, cancellationToken); + return await OpenAsync(firstStream, options, cancellationToken); } firstStream.NotNull(nameof(firstStream)); options ??= new ReaderOptions(); var factory = FindFactory(firstStream); - return factory.OpenAsync(streamsArray, options, cancellationToken); + return factory.OpenAsync(streamsArray, options); } /// diff --git a/src/SharpCompress/Archives/AutoArchiveFactory.cs b/src/SharpCompress/Archives/AutoArchiveFactory.cs index a6d3855e..c92c305f 100644 --- a/src/SharpCompress/Archives/AutoArchiveFactory.cs +++ b/src/SharpCompress/Archives/AutoArchiveFactory.cs @@ -34,11 +34,8 @@ internal class AutoArchiveFactory : IArchiveFactory public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) => ArchiveFactory.Open(stream, readerOptions); - public IAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) => (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)Open(stream, readerOptions); public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => ArchiveFactory.Open(fileInfo, readerOptions); diff --git a/src/SharpCompress/Archives/IArchiveFactory.cs b/src/SharpCompress/Archives/IArchiveFactory.cs index 06ea7360..3271f16d 100644 --- a/src/SharpCompress/Archives/IArchiveFactory.cs +++ b/src/SharpCompress/Archives/IArchiveFactory.cs @@ -32,11 +32,9 @@ public interface IArchiveFactory : IFactory /// /// An open, readable and seekable stream. /// reading options. - /// Cancellation token. IAsyncArchive OpenAsync( Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default + ReaderOptions? readerOptions = null ); /// diff --git a/src/SharpCompress/Archives/IMultiArchiveFactory.cs b/src/SharpCompress/Archives/IMultiArchiveFactory.cs index b54b32ec..d74ffb4a 100644 --- a/src/SharpCompress/Archives/IMultiArchiveFactory.cs +++ b/src/SharpCompress/Archives/IMultiArchiveFactory.cs @@ -33,11 +33,9 @@ public interface IMultiArchiveFactory : IFactory /// /// /// reading options. - /// Cancellation token. IAsyncArchive OpenAsync( IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default + ReaderOptions? readerOptions = null ); /// diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index 9a0fbac5..48786117 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -311,7 +311,7 @@ public class ZipArchive : AbstractWritableArchive var header = await headerFactory .ReadStreamHeaderAsync(stream) - .Where(x => x.ZipHeaderType != ZipHeaderType.Split) + .WhereAsync(x => x.ZipHeaderType != ZipHeaderType.Split) .FirstOrDefaultAsync(); if (header is null) { diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index dd36a78c..f8eada3f 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -65,11 +65,8 @@ public class GZipFactory GZipArchive.Open(stream, readerOptions); /// - public IAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) => (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)Open(stream, readerOptions); public override ValueTask IsArchiveAsync( Stream stream, @@ -99,8 +96,7 @@ public class GZipFactory /// public IAsyncArchive OpenAsync( IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default + ReaderOptions? readerOptions = null ) => (IAsyncArchive)Open(streams, readerOptions); /// diff --git a/src/SharpCompress/Factories/RarFactory.cs b/src/SharpCompress/Factories/RarFactory.cs index 841cc7ff..faf35ab5 100644 --- a/src/SharpCompress/Factories/RarFactory.cs +++ b/src/SharpCompress/Factories/RarFactory.cs @@ -50,11 +50,8 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade RarArchive.Open(stream, readerOptions); /// - public IAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) => (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)Open(stream, readerOptions); /// public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => @@ -88,8 +85,7 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade /// public IAsyncArchive OpenAsync( IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default + ReaderOptions? readerOptions = null ) => (IAsyncArchive)Open(streams, readerOptions); /// diff --git a/src/SharpCompress/Factories/SevenZipFactory.cs b/src/SharpCompress/Factories/SevenZipFactory.cs index bb4e1093..f430919a 100644 --- a/src/SharpCompress/Factories/SevenZipFactory.cs +++ b/src/SharpCompress/Factories/SevenZipFactory.cs @@ -45,11 +45,8 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory SevenZipArchive.Open(stream, readerOptions); /// - public IAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) => (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)Open(stream, readerOptions); /// public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => @@ -83,8 +80,7 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory /// public IAsyncArchive OpenAsync( IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default + ReaderOptions? readerOptions = null ) => (IAsyncArchive)Open(streams, readerOptions); /// diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index 0b8038af..85832bab 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -76,11 +76,8 @@ public class TarFactory TarArchive.Open(stream, readerOptions); /// - public IAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) => (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)Open(stream, readerOptions); /// public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => @@ -108,8 +105,7 @@ public class TarFactory /// public IAsyncArchive OpenAsync( IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default + ReaderOptions? readerOptions = null ) => (IAsyncArchive)Open(streams, readerOptions); /// diff --git a/src/SharpCompress/Factories/ZipFactory.cs b/src/SharpCompress/Factories/ZipFactory.cs index 42047ca4..d0774a74 100644 --- a/src/SharpCompress/Factories/ZipFactory.cs +++ b/src/SharpCompress/Factories/ZipFactory.cs @@ -143,11 +143,8 @@ public class ZipFactory ZipArchive.Open(stream, readerOptions); /// - public IAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) => (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)Open(stream, readerOptions); /// public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => @@ -175,8 +172,7 @@ public class ZipFactory /// public IAsyncArchive OpenAsync( IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default + ReaderOptions? readerOptions = null ) => (IAsyncArchive)Open(streams, readerOptions); /// diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index aedc1d28..1d4d9d5a 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -29,28 +29,41 @@ public static class EnumerableExtensions public static class AsyncEnumerableExtensions { - extension(IAsyncEnumerable source) - where T : notnull + public static async ValueTask> ToListAsync(this IAsyncEnumerable source) { - public async ValueTask> ToListAsync() + var list = new List(); + await foreach (var item in source) { - var list = new List(); - await foreach (var item in source) - { - list.Add(item); - } - return list; + list.Add(item); } + return list; + } - public async IAsyncEnumerable CastAsync() - where TResult : class + public static async IAsyncEnumerable CastAsync(this IAsyncEnumerable source) + where TResult : class + { + await foreach (var item in source) { - await foreach (var item in source) - { - yield return (item as TResult).NotNull(); - } + yield return (item as TResult).NotNull(); } + } + public static async ValueTask AggregateAsync( + this IAsyncEnumerable source, + TAccumulate seed, + Func func + ) + { + TAccumulate result = seed; + await foreach (var element in source) + { + result = func(result, element); + } + return result; + } + + extension(IAsyncEnumerable source) + { public async ValueTask AllAsync(Func predicate) { await foreach (var item in source) @@ -79,32 +92,19 @@ public static class AsyncEnumerableExtensions { await foreach (var item in source) { - return item; // Returns the very first item found + return item; } - throw new InvalidOperationException("The source sequence is empty."); // Throws if the stream is empty + throw new InvalidOperationException("The source sequence is empty."); } public async ValueTask FirstOrDefaultAsync() { await foreach (var item in source) { - return item; // Returns the very first item found + return item; } - return default; // Returns null/default if the stream is empty - } - - public async ValueTask Aggregate( - TAccumulate seed, - Func func - ) - { - TAccumulate result = seed; - await foreach (var element in source) - { - result = func(result, element); - } - return result; + return default; } } } From 38203fb950fbd35fca55a512d0f040e3ee3e5ca0 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 14:14:46 +0000 Subject: [PATCH 04/36] Fix async reader variable types - Remove double await on ReaderFactory.OpenAsync and use IAsyncReader - Removed 'await' keyword before ReaderFactory.OpenAsync() calls since the method returns IAsyncReader directly (not Task) - Changed ZipReader.Open() to ReaderFactory.OpenAsync() in Zip64AsyncTests.ReadForwardOnlyAsync() - Changed TarReader.Open() to ReaderFactory.OpenAsync() in TarReaderAsyncTests.Tar_BZip2_Entry_Stream_Async() - Fixed EntryStream disposal from 'await using' to 'using' since EntryStream doesn't implement IAsyncDisposable - These changes fix compilation errors where async methods were being called on IReader (synchronous) instead of IAsyncReader (asynchronous) --- src/SharpCompress/Archives/IArchiveFactory.cs | 5 +- .../Archives/IMultiArchiveFactory.cs | 5 +- .../Polyfills/AsyncEnumerableExtensions.cs | 99 ++++++++++++------- tests/SharpCompress.Test/GZip/AsyncTests.cs | 18 ++-- .../GZip/GZipArchiveAsyncTests.cs | 42 +++++--- .../GZip/GZipReaderAsyncTests.cs | 4 +- .../SharpCompress.Test/ProgressReportTests.cs | 5 +- .../Rar/RarReaderAsyncTests.cs | 10 +- tests/SharpCompress.Test/ReaderTests.cs | 2 +- .../Tar/TarReaderAsyncTests.cs | 10 +- tests/SharpCompress.Test/WriterTests.cs | 2 +- .../SharpCompress.Test/Zip/Zip64AsyncTests.cs | 35 ++++--- .../Zip/ZipArchiveAsyncTests.cs | 64 ++++++++---- .../Zip/ZipReaderAsyncTests.cs | 8 +- 14 files changed, 182 insertions(+), 127 deletions(-) diff --git a/src/SharpCompress/Archives/IArchiveFactory.cs b/src/SharpCompress/Archives/IArchiveFactory.cs index 3271f16d..b94128d6 100644 --- a/src/SharpCompress/Archives/IArchiveFactory.cs +++ b/src/SharpCompress/Archives/IArchiveFactory.cs @@ -32,10 +32,7 @@ public interface IArchiveFactory : IFactory /// /// An open, readable and seekable stream. /// reading options. - IAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null - ); + IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null); /// /// Constructor with a FileInfo object to an existing file. diff --git a/src/SharpCompress/Archives/IMultiArchiveFactory.cs b/src/SharpCompress/Archives/IMultiArchiveFactory.cs index d74ffb4a..e0064cc0 100644 --- a/src/SharpCompress/Archives/IMultiArchiveFactory.cs +++ b/src/SharpCompress/Archives/IMultiArchiveFactory.cs @@ -33,10 +33,7 @@ public interface IMultiArchiveFactory : IFactory /// /// /// reading options. - IAsyncArchive OpenAsync( - IReadOnlyList streams, - ReaderOptions? readerOptions = null - ); + IAsyncArchive OpenAsync(IReadOnlyList streams, ReaderOptions? readerOptions = null); /// /// Constructor with IEnumerable Stream objects, multi and split support. diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index 1d4d9d5a..7ce90ded 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -39,7 +39,9 @@ public static class AsyncEnumerableExtensions return list; } - public static async IAsyncEnumerable CastAsync(this IAsyncEnumerable source) + public static async IAsyncEnumerable CastAsync( + this IAsyncEnumerable source + ) where TResult : class { await foreach (var item in source) @@ -62,49 +64,74 @@ public static class AsyncEnumerableExtensions return result; } - extension(IAsyncEnumerable source) + public static async ValueTask AllAsync( + this IAsyncEnumerable source, + Func predicate + ) { - public async ValueTask AllAsync(Func predicate) + await foreach (var item in source) { - await foreach (var item in source) + if (!predicate(item)) { - if (!predicate(item)) - { - return false; - } - } - - return true; - } - - public async IAsyncEnumerable WhereAsync(Func predicate) - { - await foreach (var item in source) - { - if (predicate(item)) - { - yield return item; - } + return false; } } - public async ValueTask FirstAsync() - { - await foreach (var item in source) - { - return item; - } - throw new InvalidOperationException("The source sequence is empty."); - } + return true; + } - public async ValueTask FirstOrDefaultAsync() - { - await foreach (var item in source) - { - return item; - } + public static IAsyncEnumerable Where( + this IAsyncEnumerable source, + Func predicate + ) + { + return WhereIterator(source, predicate); + } - return default; + private static async IAsyncEnumerable WhereIterator( + IAsyncEnumerable source, + Func predicate + ) + { + await foreach (var item in source) + { + if (predicate(item)) + { + yield return item; + } } } + + public static async IAsyncEnumerable WhereAsync( + this IAsyncEnumerable source, + Func predicate + ) + { + await foreach (var item in source) + { + if (predicate(item)) + { + yield return item; + } + } + } + + public static async ValueTask FirstAsync(this IAsyncEnumerable source) + { + await foreach (var item in source) + { + return item; + } + throw new InvalidOperationException("The source sequence is empty."); + } + + public static async ValueTask FirstOrDefaultAsync(this IAsyncEnumerable source) + { + await foreach (var item in source) + { + return item; + } + + return default; + } } diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index d000798e..fabdafc0 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -26,7 +26,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); await reader.WriteAllToDirectoryAsync( SCRATCH_FILES_PATH, @@ -51,7 +51,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { @@ -103,12 +103,8 @@ public class AsyncTests : TestBase #else await using (var stream = File.Create(outputPath)) #endif - await using ( - var writer = await WriterFactory.OpenAsync( - stream, - ArchiveType.Zip, - CompressionType.Deflate - ) + using ( + var writer = WriterFactory.OpenAsync(stream, ArchiveType.Zip, CompressionType.Deflate) ) { var testFile = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"); @@ -123,7 +119,7 @@ public class AsyncTests : TestBase // Verify the archive was created and contains the entry Assert.True(File.Exists(outputPath)); - await using var archive = await ZipArchive.OpenAsync(outputPath); + await using var archive = ZipArchive.OpenAsync(outputPath); Assert.Single(await archive.EntriesAsync.WhereAsync(e => !e.IsDirectory).ToListAsync()); } @@ -139,7 +135,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = await ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsync( new AsyncOnlyStream(stream), cancellationToken: cts.Token ); @@ -193,7 +189,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index 004d4ba8..6b27aa50 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -47,17 +47,24 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) #endif - await using (var archive = await GZipArchive.OpenAsync(stream)) { - var entry = await archive.EntriesAsync.FirstAsync(); - await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); + IAsyncArchive archive = await GZipArchive.OpenAsync(stream); + try + { + var entry = await archive.EntriesAsync.FirstAsync(); + await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); - var size = entry.Size; - var scratch = new FileInfo(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar")); - var test = new FileInfo(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); + var size = entry.Size; + var scratch = new FileInfo(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar")); + var test = new FileInfo(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); - Assert.Equal(size, scratch.Length); - Assert.Equal(size, test.Length); + Assert.Equal(size, scratch.Length); + Assert.Equal(size, test.Length); + } + finally + { + archive.DisposeAsync().AsTask().Wait(); + } } CompareArchivesByPath( Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"), @@ -74,11 +81,18 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); #endif - await using var archive = await GZipArchive.OpenAsync(stream); - await Assert.ThrowsAsync(() => - archive.AddEntry("jpg\\test.jpg", jpg) - ); - await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); + IAsyncArchive archive = await GZipArchive.OpenAsync(stream); + try + { + await Assert.ThrowsAsync(() => + archive.AddEntry("jpg\\test.jpg", jpg) + ); + await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); + } + finally + { + await archive.DisposeAsync(); + } } [Fact] @@ -95,7 +109,7 @@ public class GZipArchiveAsyncTests : ArchiveTests inputStream.Position = 0; } - await using var archive = GZipArchive.Open(inputStream); + using var archive = GZipArchive.Open(inputStream); var archiveEntry = archive.Entries.First(); MemoryStream tarStream; diff --git a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs index befd1440..58f5d9dc 100644 --- a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs @@ -22,7 +22,7 @@ public class GZipReaderAsyncTests : ReaderTests { //read only as GZip item using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { Assert.NotEqual(0, reader.Entry.Size); @@ -70,7 +70,7 @@ public class GZipReaderAsyncTests : ReaderTests bufferSize: options.BufferSize ); using var testStream = new TestStream(protectedStream); - await using (var reader = await ReaderFactory.OpenAsync(testStream, options, default)) + await using (var reader = ReaderFactory.OpenAsync(testStream, options, default)) { await UseReaderAsync(reader, expectedCompression); protectedStream.ThrowOnDispose = false; diff --git a/tests/SharpCompress.Test/ProgressReportTests.cs b/tests/SharpCompress.Test/ProgressReportTests.cs index 2f0dd802..f2c7121e 100644 --- a/tests/SharpCompress.Test/ProgressReportTests.cs +++ b/tests/SharpCompress.Test/ProgressReportTests.cs @@ -541,10 +541,7 @@ public class ProgressReportTests : TestBase var readerOptions = new ReaderOptions { Progress = progress }; await using ( - var reader = await ReaderFactory.OpenAsync( - new AsyncOnlyStream(archiveStream), - readerOptions - ) + var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(archiveStream), readerOptions) ) { while (await reader.MoveToNextEntryAsync()) diff --git a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs index 6fd78314..0de82da5 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs @@ -205,7 +205,7 @@ public class RarReaderAsyncTests : ReaderTests private async ValueTask DoRar_Entry_Stream_Async(string filename) { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename))) - await using (var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream))) + await using (var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream))) { while (await reader.MoveToNextEntryAsync()) { @@ -250,7 +250,7 @@ public class RarReaderAsyncTests : ReaderTests var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.Audio_program.rar")) ) await using ( - var reader = await ReaderFactory.OpenAsync( + var reader = ReaderFactory.OpenAsync( new AsyncOnlyStream(stream), new ReaderOptions { LookForHeader = true } ) @@ -316,7 +316,7 @@ public class RarReaderAsyncTests : ReaderTests private async ValueTask DoRar_Solid_Skip_Reader_Async(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - await using var reader = await ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsync( new AsyncOnlyStream(stream), new ReaderOptions { LookForHeader = true } ); @@ -342,7 +342,7 @@ public class RarReaderAsyncTests : ReaderTests private async ValueTask DoRar_Reader_Skip_Async(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - await using var reader = await ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsync( new AsyncOnlyStream(stream), new ReaderOptions { LookForHeader = true } ); @@ -367,7 +367,7 @@ public class RarReaderAsyncTests : ReaderTests { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); using Stream stream = File.OpenRead(testArchive); - await using var reader = await ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsync( new AsyncOnlyStream(stream), readerOptions ?? new ReaderOptions() ); diff --git a/tests/SharpCompress.Test/ReaderTests.cs b/tests/SharpCompress.Test/ReaderTests.cs index 67c4371c..cb1dac03 100644 --- a/tests/SharpCompress.Test/ReaderTests.cs +++ b/tests/SharpCompress.Test/ReaderTests.cs @@ -146,7 +146,7 @@ public abstract class ReaderTests : TestBase ); using var testStream = new TestStream(protectedStream); await using ( - var reader = await ReaderFactory.OpenAsync( + var reader = ReaderFactory.OpenAsync( new AsyncOnlyStream(testStream), options, cancellationToken diff --git a/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs index 4be59584..1a18f3ce 100644 --- a/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs @@ -23,7 +23,7 @@ public class TarReaderAsyncTests : ReaderTests using Stream stream = new ForwardOnlyStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")) ); - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); var x = 0; while (await reader.MoveToNextEntryAsync()) { @@ -73,7 +73,7 @@ public class TarReaderAsyncTests : ReaderTests public async ValueTask Tar_BZip2_Entry_Stream_Async() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.bz2"))) - using (var reader = TarReader.Open(stream)) + await using (var reader = ReaderFactory.OpenAsync(stream)) { while (await reader.MoveToNextEntryAsync()) { @@ -184,7 +184,7 @@ public class TarReaderAsyncTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"); using Stream stream = File.OpenRead(archiveFullPath); - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); var memoryStream = new MemoryStream(); Assert.True(await reader.MoveToNextEntryAsync()); @@ -201,7 +201,7 @@ public class TarReaderAsyncTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "TarCorrupted.tar"); using Stream stream = File.OpenRead(archiveFullPath); - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); var memoryStream = new MemoryStream(); Assert.True(await reader.MoveToNextEntryAsync()); @@ -220,7 +220,7 @@ public class TarReaderAsyncTests : ReaderTests using Stream stream = File.OpenRead( Path.Combine(TEST_ARCHIVES_PATH, "TarWithSymlink.tar.gz") ); - await using var reader = await ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsync( new AsyncOnlyStream(stream), new ReaderOptions { LookForHeader = true } ); diff --git a/tests/SharpCompress.Test/WriterTests.cs b/tests/SharpCompress.Test/WriterTests.cs index 984d3b91..9a6a43de 100644 --- a/tests/SharpCompress.Test/WriterTests.cs +++ b/tests/SharpCompress.Test/WriterTests.cs @@ -92,7 +92,7 @@ public class WriterTests : TestBase readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - await using var reader = await ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsync( new AsyncOnlyStream(SharpCompressStream.Create(stream, leaveOpen: true)), readerOptions, cancellationToken diff --git a/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs b/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs index 2c943553..eab7e1c7 100644 --- a/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs @@ -198,28 +198,31 @@ public class Zip64AsyncTests : WriterTests long size = 0; ZipEntry? prev = null; using (var fs = File.OpenRead(filename)) - using (var rd = ZipReader.Open(fs, new ReaderOptions { LookForHeader = false })) { - while (await rd.MoveToNextEntryAsync()) + var rd = ReaderFactory.OpenAsync(fs, new ReaderOptions { LookForHeader = false }); + await using (rd) { + while (await rd.MoveToNextEntryAsync()) + { #if NETFRAMEWORK || NETSTANDARD2_0 - using (var entryStream = await rd.OpenEntryStreamAsync()) - { - await entryStream.SkipEntryAsync(); - } + using (var entryStream = await rd.OpenEntryStreamAsync()) + { + await entryStream.SkipEntryAsync(); + } #else - await using (var entryStream = await rd.OpenEntryStreamAsync()) - { - await entryStream.SkipEntryAsync(); - } + await using (var entryStream = await rd.OpenEntryStreamAsync()) + { + await entryStream.SkipEntryAsync(); + } #endif - count++; - if (prev != null) - { - size += prev.Size; - } + count++; + if (prev != null) + { + size += prev.Size; + } - prev = rd.Entry; + prev = (ZipEntry)rd.Entry; + } } } diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs index 41420f98..ffec6efc 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs @@ -3,6 +3,7 @@ using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; +using SharpCompress; using SharpCompress.Archives; using SharpCompress.Archives.Zip; using SharpCompress.Common; @@ -125,8 +126,9 @@ public class ZipArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip"); - using (var archive = ZipArchive.Open(unmodified)) + using (IArchive baseArchive = ZipArchive.Open(unmodified)) { + IWritableArchive archive = (IWritableArchive)baseArchive; var entry = archive.Entries.Single(x => x.Key.NotNull().EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ); @@ -148,8 +150,9 @@ public class ZipArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using (var archive = ZipArchive.Open(unmodified)) + using (IArchive baseArchive = ZipArchive.Open(unmodified)) { + IWritableArchive archive = (IWritableArchive)baseArchive; archive.AddEntry("jpg\\test.jpg", jpg); WriterOptions writerOptions = new ZipWriterOptions(CompressionType.Deflate); @@ -166,7 +169,7 @@ public class ZipArchiveAsyncTests : ArchiveTests var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Zip.deflate.noEmptyDirs.zip"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using (var archive = ZipArchive.Create()) + using (ZipArchive archive = ZipArchive.Create()) { archive.DeflateCompressionLevel = CompressionLevel.BestSpeed; archive.AddAllFromDirectory(ORIGINAL_FILES_PATH); @@ -183,14 +186,21 @@ public class ZipArchiveAsyncTests : ArchiveTests public async ValueTask Zip_Deflate_Entry_Stream_Async() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.zip"))) - await using (var archive = await ZipArchive.OpenAsync(new AsyncOnlyStream(stream))) { - await foreach (var entry in archive.EntriesAsync.Where(entry => !entry.IsDirectory)) + IAsyncArchive archive = ZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + try { - await entry.WriteToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); + await foreach (var entry in archive.EntriesAsync.Where(entry => !entry.IsDirectory)) + { + await entry.WriteToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + finally + { + await archive.DisposeAsync(); } } VerifyFiles(); @@ -200,12 +210,19 @@ public class ZipArchiveAsyncTests : ArchiveTests public async ValueTask Zip_Deflate_Archive_WriteToDirectoryAsync() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.zip"))) - await using (var archive = await ZipArchive.OpenAsync(new AsyncOnlyStream(stream))) { - await archive.WriteToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); + IAsyncArchive archive = ZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + try + { + await archive.WriteToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + finally + { + await archive.DisposeAsync(); + } } VerifyFiles(); } @@ -217,13 +234,20 @@ public class ZipArchiveAsyncTests : ArchiveTests var progress = new Progress(report => progressReports.Add(report)); using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.zip"))) - await using (var archive = await ZipArchive.OpenAsync(new AsyncOnlyStream(stream))) { - await archive.WriteToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true }, - progress - ); + IAsyncArchive archive = ZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + try + { + await archive.WriteToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true }, + progress + ); + } + finally + { + await archive.DisposeAsync(); + } } VerifyFiles(); diff --git a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs index 33a673f4..9d5f4da9 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs @@ -20,7 +20,7 @@ public class ZipReaderAsyncTests : ReaderTests { var path = Path.Combine(TEST_ARCHIVES_PATH, "PrePostHeaders.zip"); using Stream stream = new ForwardOnlyStream(File.OpenRead(path)); - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); var count = 0; while (await reader.MoveToNextEntryAsync()) { @@ -65,7 +65,7 @@ public class ZipReaderAsyncTests : ReaderTests using Stream stream = new ForwardOnlyStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ); - await using var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); var x = 0; while (await reader.MoveToNextEntryAsync()) { @@ -144,7 +144,7 @@ public class ZipReaderAsyncTests : ReaderTests using var stream = new TestStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ); - await using (var reader = await ReaderFactory.OpenAsync(new AsyncOnlyStream(stream))) + await using (var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream))) { while (await reader.MoveToNextEntryAsync()) { @@ -168,7 +168,7 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ) ); - await using var reader = await ReaderFactory.OpenAsync(stream); + await using var reader = ReaderFactory.OpenAsync(stream); while (await reader.MoveToNextEntryAsync()) { if (!reader.Entry.IsDirectory) From fce4a96718f4cf5295c78e1c75645eb3f9b59ab0 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 14:57:13 +0000 Subject: [PATCH 05/36] make Writable interfaces for archive --- src/SharpCompress/Archives/AbstractArchive.cs | 1 - .../Archives/AbstractWritableArchive.cs | 8 +- .../Archives/GZip/GZipArchive.cs | 18 +- src/SharpCompress/Archives/IArchive.cs | 2 + .../Archives/IWritableArchive.cs | 21 +- .../IWritableArchiveCommonExtensions.cs | 75 +++++++ .../Archives/IWritableArchiveExtensions.cs | 93 +-------- .../IWritableAsyncArchiveExtensions.cs | 29 +++ .../Archives/Rar/RarArchive.Extensions.cs | 23 ++- src/SharpCompress/Archives/Rar/RarArchive.cs | 44 ++-- src/SharpCompress/Archives/Tar/TarArchive.cs | 36 ++-- src/SharpCompress/Archives/Zip/ZipArchive.cs | 30 +-- .../Polyfills/AsyncEnumerableExtensions.cs | 18 ++ src/SharpCompress/packages.lock.json | 6 +- tests/SharpCompress.Test/ArchiveTests.cs | 2 +- .../GZip/GZipArchiveAsyncTests.cs | 19 +- .../Rar/RarReaderAsyncTests.cs | 188 +++++++++--------- .../Tar/TarArchiveAsyncTests.cs | 10 +- .../SharpCompress.Test/Tar/TarArchiveTests.cs | 8 +- .../Zip/ZipArchiveAsyncTests.cs | 8 +- .../SharpCompress.Test/Zip/ZipArchiveTests.cs | 4 +- .../Zip/ZipReaderAsyncTests.cs | 14 +- 22 files changed, 372 insertions(+), 285 deletions(-) create mode 100644 src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs create mode 100644 src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs diff --git a/src/SharpCompress/Archives/AbstractArchive.cs b/src/SharpCompress/Archives/AbstractArchive.cs index 0412f93e..844035cd 100644 --- a/src/SharpCompress/Archives/AbstractArchive.cs +++ b/src/SharpCompress/Archives/AbstractArchive.cs @@ -1,7 +1,6 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using SharpCompress; using SharpCompress.Common; using SharpCompress.IO; using SharpCompress.Readers; diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.cs b/src/SharpCompress/Archives/AbstractWritableArchive.cs index 13fb66f9..e7bb2af2 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.cs @@ -12,7 +12,7 @@ namespace SharpCompress.Archives; public abstract class AbstractWritableArchive : AbstractArchive, - IWritableArchive + IWritableArchive, IWritableAsyncArchive where TEntry : IArchiveEntry where TVolume : IVolume { @@ -83,12 +83,12 @@ public abstract class AbstractWritableArchive } } - void IWritableArchive.RemoveEntry(IArchiveEntry entry) => RemoveEntry((TEntry)entry); + void IWritableArchiveCommon.RemoveEntry(IArchiveEntry entry) => RemoveEntry((TEntry)entry); public TEntry AddEntry(string key, Stream source, long size = 0, DateTime? modified = null) => AddEntry(key, source, false, size, modified); - IArchiveEntry IWritableArchive.AddEntry( + IArchiveEntry IWritableArchiveCommon.AddEntry( string key, Stream source, bool closeStream, @@ -96,7 +96,7 @@ public abstract class AbstractWritableArchive DateTime? modified ) => AddEntry(key, source, closeStream, size, modified); - IArchiveEntry IWritableArchive.AddDirectoryEntry(string key, DateTime? modified) => + IArchiveEntry IWritableArchiveCommon.AddDirectoryEntry(string key, DateTime? modified) => AddDirectoryEntry(key, modified); public TEntry AddEntry( diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 82ea7f67..b4c1b86f 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -88,7 +88,7 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -108,14 +108,14 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(stream, readerOptions); + return (IWritableAsyncArchive)Open(stream, readerOptions); } /// @@ -124,14 +124,14 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IWritableAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -140,14 +140,14 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(streams, readerOptions); + return (IWritableAsyncArchive)Open(streams, readerOptions); } /// @@ -156,14 +156,14 @@ public class GZipArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IWritableAsyncArchive)Open(fileInfos, readerOptions); } public static GZipArchive Create() => new(); diff --git a/src/SharpCompress/Archives/IArchive.cs b/src/SharpCompress/Archives/IArchive.cs index 3ed7490d..0abb2f1b 100644 --- a/src/SharpCompress/Archives/IArchive.cs +++ b/src/SharpCompress/Archives/IArchive.cs @@ -39,4 +39,6 @@ public interface IArchive : IDisposable /// The total size of the files as uncompressed in the archive. /// long TotalUncompressSize { get; } + + bool IsEncrypted { get; } } diff --git a/src/SharpCompress/Archives/IWritableArchive.cs b/src/SharpCompress/Archives/IWritableArchive.cs index 74d8da76..514b8cdc 100644 --- a/src/SharpCompress/Archives/IWritableArchive.cs +++ b/src/SharpCompress/Archives/IWritableArchive.cs @@ -6,8 +6,13 @@ using SharpCompress.Writers; namespace SharpCompress.Archives; -public interface IWritableArchive : IArchive +public interface IWritableArchiveCommon { + /// + /// Use this to pause entry rebuilding when adding large collections of entries. Dispose when complete. A using statement is recommended. + /// + /// IDisposeable to resume entry rebuilding + IDisposable PauseEntryRebuilding(); void RemoveEntry(IArchiveEntry entry); IArchiveEntry AddEntry( @@ -19,18 +24,20 @@ public interface IWritableArchive : IArchive ); IArchiveEntry AddDirectoryEntry(string key, DateTime? modified = null); +} + +public interface IWritableArchive : IArchive, IWritableArchiveCommon +{ void SaveTo(Stream stream, WriterOptions options); +} + +public interface IWritableAsyncArchive : IAsyncArchive, IWritableArchiveCommon +{ ValueTask SaveToAsync( Stream stream, WriterOptions options, CancellationToken cancellationToken = default ); - - /// - /// Use this to pause entry rebuilding when adding large collections of entries. Dispose when complete. A using statement is recommended. - /// - /// IDisposeable to resume entry rebuilding - IDisposable PauseEntryRebuilding(); } diff --git a/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs b/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs new file mode 100644 index 00000000..2fa86ee3 --- /dev/null +++ b/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs @@ -0,0 +1,75 @@ +using System; +using System.IO; + +namespace SharpCompress.Archives; + +public static class IWritableArchiveCommonExtensions +{ + public static void AddEntry( + this IWritableArchive writableArchive, + string entryPath, + string filePath + ) + { + var fileInfo = new FileInfo(filePath); + if (!fileInfo.Exists) + { + throw new FileNotFoundException("Could not AddEntry: " + filePath); + } + writableArchive.AddEntry( + entryPath, + new FileInfo(filePath).OpenRead(), + true, + fileInfo.Length, + fileInfo.LastWriteTime + ); + } + + public static void AddAllFromDirectory( + this IWritableArchiveCommon writableArchive, + string filePath, + string searchPattern = "*.*", + SearchOption searchOption = SearchOption.AllDirectories + ) + { + using (writableArchive.PauseEntryRebuilding()) + { + foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption)) + { + var fileInfo = new FileInfo(path); + writableArchive.AddEntry( + path.Substring(filePath.Length), + fileInfo.OpenRead(), + true, + fileInfo.Length, + fileInfo.LastWriteTime + ); + } + } + } + + public static IArchiveEntry AddEntry( + this IWritableArchiveCommon writableArchive, + string key, + string file + ) => AddEntry(writableArchive, key, new FileInfo(file)); + + public static IArchiveEntry AddEntry( + this IWritableArchiveCommon writableArchive, + string key, + FileInfo fileInfo + ) + { + if (!fileInfo.Exists) + { + throw new ArgumentException("FileInfo does not exist."); + } + return writableArchive.AddEntry( + key, + fileInfo.OpenRead(), + true, + fileInfo.Length, + fileInfo.LastWriteTime + ); + } +} diff --git a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs index 60ec83d8..07b642ae 100644 --- a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs @@ -1,106 +1,25 @@ -using System; -using System.IO; -using System.Threading; -using System.Threading.Tasks; +using System.IO; +using SharpCompress.Common; using SharpCompress.Writers; namespace SharpCompress.Archives; public static class IWritableArchiveExtensions { - public static void AddEntry( - this IWritableArchive writableArchive, - string entryPath, - string filePath - ) - { - var fileInfo = new FileInfo(filePath); - if (!fileInfo.Exists) - { - throw new FileNotFoundException("Could not AddEntry: " + filePath); - } - writableArchive.AddEntry( - entryPath, - new FileInfo(filePath).OpenRead(), - true, - fileInfo.Length, - fileInfo.LastWriteTime - ); - } public static void SaveTo( this IWritableArchive writableArchive, string filePath, - WriterOptions options - ) => writableArchive.SaveTo(new FileInfo(filePath), options); + WriterOptions? options = null + ) => writableArchive.SaveTo(new FileInfo(filePath), options ?? new (CompressionType.Deflate)); public static void SaveTo( this IWritableArchive writableArchive, FileInfo fileInfo, - WriterOptions options + WriterOptions? options = null ) { using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); - writableArchive.SaveTo(stream, options); - } - - public static ValueTask SaveToAsync( - this IWritableArchive writableArchive, - string filePath, - WriterOptions options, - CancellationToken cancellationToken = default - ) => writableArchive.SaveToAsync(new FileInfo(filePath), options, cancellationToken); - - public static async ValueTask SaveToAsync( - this IWritableArchive writableArchive, - FileInfo fileInfo, - WriterOptions options, - CancellationToken cancellationToken = default - ) - { - using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); - await writableArchive.SaveToAsync(stream, options, cancellationToken).ConfigureAwait(false); - } - - public static void AddAllFromDirectory( - this IWritableArchive writableArchive, - string filePath, - string searchPattern = "*.*", - SearchOption searchOption = SearchOption.AllDirectories - ) - { - using (writableArchive.PauseEntryRebuilding()) - { - foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption)) - { - var fileInfo = new FileInfo(path); - writableArchive.AddEntry( - path.Substring(filePath.Length), - fileInfo.OpenRead(), - true, - fileInfo.Length, - fileInfo.LastWriteTime - ); - } - } - } - - public static IArchiveEntry AddEntry( - this IWritableArchive writableArchive, - string key, - FileInfo fileInfo - ) - { - if (!fileInfo.Exists) - { - throw new ArgumentException("FileInfo does not exist."); - } - return writableArchive.AddEntry( - key, - fileInfo.OpenRead(), - true, - fileInfo.Length, - fileInfo.LastWriteTime - ); + writableArchive.SaveTo(stream, options?? new (CompressionType.Deflate)); } } diff --git a/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs b/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs new file mode 100644 index 00000000..ec43872c --- /dev/null +++ b/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs @@ -0,0 +1,29 @@ +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Common; +using SharpCompress.Writers; + +namespace SharpCompress.Archives; + +public static class IWritableAsyncArchiveExtensions +{ + public static ValueTask SaveToAsync( + this IWritableAsyncArchive writableArchive, + string filePath, + WriterOptions? options = null, + CancellationToken cancellationToken = default + ) => writableArchive.SaveToAsync(new FileInfo(filePath), options ?? new (CompressionType.Deflate), cancellationToken); + + public static async ValueTask SaveToAsync( + this IWritableAsyncArchive writableArchive, + FileInfo fileInfo, + WriterOptions? options = null, + CancellationToken cancellationToken = default + ) + { + using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); + await writableArchive.SaveToAsync(stream, options?? new (CompressionType.Deflate), cancellationToken).ConfigureAwait(false); + } + +} diff --git a/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs b/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs index bf6f0cd4..ff19275f 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs @@ -1,4 +1,6 @@ using System.Linq; +using System.Threading.Tasks; +using SharpCompress.Common.Rar; namespace SharpCompress.Archives.Rar; @@ -7,12 +9,25 @@ public static class RarArchiveExtensions /// /// RarArchive is the first volume of a multi-part archive. If MultipartVolume is true and IsFirstVolume is false then the first volume file must be missing. /// - public static bool IsFirstVolume(this RarArchive archive) => - archive.Volumes.First().IsFirstVolume; + public static bool IsFirstVolume(this IRarArchive archive) => + archive.Volumes.Cast().First().IsFirstVolume; /// /// RarArchive is part of a multi-part archive. /// - public static bool IsMultipartVolume(this RarArchive archive) => - archive.Volumes.First().IsMultiVolume; + public static bool IsMultipartVolume(this IRarArchive archive) => + archive.Volumes.Cast().First().IsMultiVolume; + + + /// + /// RarArchive is the first volume of a multi-part archive. If MultipartVolume is true and IsFirstVolume is false then the first volume file must be missing. + /// + public static async ValueTask IsFirstVolumeAsync(this IRarAsyncArchive archive) => + (await archive.VolumesAsync.CastAsync().FirstAsync()).IsFirstVolume; + + /// + /// RarArchive is part of a multi-part archive. + /// + public static async ValueTask IsMultipartVolumeAsync(this IRarAsyncArchive archive) => + (await archive.VolumesAsync.CastAsync().FirstAsync()).IsMultiVolume; } diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index 1cb7b00a..77568e86 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -14,7 +14,23 @@ using SharpCompress.Readers.Rar; namespace SharpCompress.Archives.Rar; -public class RarArchive : AbstractArchive +public interface IRarArchiveCommon +{ + + + int MinVersion { get; } + int MaxVersion { get; } +} + +public interface IRarArchive : IArchive, IRarArchiveCommon +{ +} + +public interface IRarAsyncArchive : IAsyncArchive, IRarArchiveCommon +{ +} + +public class RarArchive : AbstractArchive, IRarArchive { private bool _disposed; internal Lazy UnpackV2017 { get; } = @@ -103,7 +119,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static IArchive Open(string filePath, ReaderOptions? options = null) + public static IRarArchive Open(string filePath, ReaderOptions? options = null) { filePath.NotNullOrEmpty(nameof(filePath)); var fileInfo = new FileInfo(filePath); @@ -121,7 +137,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static IArchive Open(FileInfo fileInfo, ReaderOptions? options = null) + public static IRarArchive Open(FileInfo fileInfo, ReaderOptions? options = null) { fileInfo.NotNull(nameof(fileInfo)); return new RarArchive( @@ -138,7 +154,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static IArchive Open(Stream stream, ReaderOptions? options = null) + public static IRarArchive Open(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); @@ -155,7 +171,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static IArchive Open( + public static IRarArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -176,7 +192,7 @@ public class RarArchive : AbstractArchive /// /// /// - public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IRarArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -195,14 +211,14 @@ public class RarArchive : AbstractArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(stream, readerOptions); + return (IRarAsyncArchive)Open(stream, readerOptions); } /// @@ -211,14 +227,14 @@ public class RarArchive : AbstractArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IRarAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -227,14 +243,14 @@ public class RarArchive : AbstractArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(streams, readerOptions); + return (IRarAsyncArchive)Open(streams, readerOptions); } /// @@ -243,14 +259,14 @@ public class RarArchive : AbstractArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IRarAsyncArchive)Open(fileInfos, readerOptions); } public static bool IsRarFile(string filePath) => IsRarFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index 76d47d0b..b1d19bda 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -22,7 +22,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); @@ -33,7 +33,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); return new TarArchive( @@ -50,7 +50,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IArchive Open( + public static IWritableArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -71,7 +71,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -89,7 +89,7 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -109,14 +109,24 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(stream, readerOptions); + return (IWritableAsyncArchive)Open(stream, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + string file, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(new FileInfo(file), readerOptions); } /// @@ -125,14 +135,14 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IWritableAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -141,14 +151,14 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(streams, readerOptions); + return (IWritableAsyncArchive)Open(streams, readerOptions); } /// @@ -157,14 +167,14 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IWritableAsyncArchive)Open(fileInfos, readerOptions); } public static bool IsTarFile(string filePath) => IsTarFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index 48786117..d23925b5 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -43,7 +43,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); @@ -54,7 +54,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); return new ZipArchive( @@ -71,7 +71,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IArchive Open( + public static IWritableArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -92,7 +92,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -110,7 +110,7 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -124,14 +124,14 @@ public class ZipArchive : AbstractWritableArchive ); } - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(path, readerOptions); + return (IWritableAsyncArchive)Open(path, readerOptions); } /// @@ -140,14 +140,14 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(stream, readerOptions); + return (IWritableAsyncArchive)Open(stream, readerOptions); } /// @@ -156,14 +156,14 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IWritableAsyncArchive)Open(fileInfo, readerOptions); } /// @@ -172,14 +172,14 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(streams, readerOptions); + return (IWritableAsyncArchive)Open(streams, readerOptions); } /// @@ -188,14 +188,14 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsync( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IWritableAsyncArchive)Open(fileInfos, readerOptions); } public static bool IsZipFile( diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index 7ce90ded..5e8be6ec 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -116,6 +116,24 @@ public static class AsyncEnumerableExtensions } } + public static async ValueTask SingleAsync( + this IAsyncEnumerable source, + Func predicate + ) + { + var enumerator = source.WhereAsync(predicate).GetAsyncEnumerator(); + if (!await enumerator.MoveNextAsync()) + { + throw new InvalidOperationException("The source sequence is empty."); + } + var value = enumerator.Current; + if (await enumerator.MoveNextAsync()) + { + throw new InvalidOperationException("The source sequence contains more than one element."); + } + return value; + } + public static async ValueTask FirstAsync(this IAsyncEnumerable source) { await foreach (var item in source) diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index 032c15c4..41325333 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -216,9 +216,9 @@ "net10.0": { "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[10.0.1, )", - "resolved": "10.0.1", - "contentHash": "ISahzLHsHY7vrwqr2p1YWZ+gsxoBRtH7gWRDK8fDUst9pp2He0GiesaqEfeX0V8QMCJM3eNEHGGpnIcPjFo2NQ==" + "requested": "[10.0.0, )", + "resolved": "10.0.0", + "contentHash": "kICGrGYEzCNI3wPzfEXcwNHgTvlvVn9yJDhSdRK+oZQy4jvYH529u7O0xf5ocQKzOMjfS07+3z9PKRIjrFMJDA==" }, "Microsoft.NETFramework.ReferenceAssemblies": { "type": "Direct", diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index 3001b4bd..cdd98a33 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -601,7 +601,7 @@ public class ArchiveTests : ReaderTests ) ) await using ( - var archive = await archiveFactory.OpenAsync( + var archive = archiveFactory.OpenAsync( new AsyncOnlyStream(stream), readerOptions ) diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index 6b27aa50..ba74de93 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -48,8 +48,7 @@ public class GZipArchiveAsyncTests : ArchiveTests await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) #endif { - IAsyncArchive archive = await GZipArchive.OpenAsync(stream); - try + await using (var archive = GZipArchive.OpenAsync(stream)) { var entry = await archive.EntriesAsync.FirstAsync(); await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); @@ -61,10 +60,6 @@ public class GZipArchiveAsyncTests : ArchiveTests Assert.Equal(size, scratch.Length); Assert.Equal(size, test.Length); } - finally - { - archive.DisposeAsync().AsTask().Wait(); - } } CompareArchivesByPath( Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"), @@ -81,18 +76,12 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); #endif - IAsyncArchive archive = await GZipArchive.OpenAsync(stream); - try + await using (var archive = GZipArchive.OpenAsync(stream)) { - await Assert.ThrowsAsync(() => - archive.AddEntry("jpg\\test.jpg", jpg) - ); + Assert.Throws(() => + archive.AddEntry("jpg\\test.jpg", jpg)); await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); } - finally - { - await archive.DisposeAsync(); - } } [Fact] diff --git a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs index 0de82da5..fe8d3b25 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs @@ -36,59 +36,61 @@ public class RarReaderAsyncTests : ReaderTests "Rar5.multi.part06.rar", ]); - private async ValueTask DoRar_Multi_Reader_Async(string[] archives) - { - using ( - var reader = RarReader.Open( - archives - .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) - .Select(p => File.OpenRead(p)) - ) - ) - { - while (await reader.MoveToNextEntryAsync()) - { - await reader.WriteEntryToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); - } - } - VerifyFiles(); - } + private async ValueTask DoRar_Multi_Reader_Async(string[] archives) + { + using ( + IReader baseReader = RarReader.Open( + archives + .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) + .Select(p => File.OpenRead(p)) + ) + ) + { + IAsyncReader reader = (IAsyncReader)baseReader; + while (await reader.MoveToNextEntryAsync()) + { + await reader.WriteEntryToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + VerifyFiles(); + } - [Fact] - public async ValueTask Rar_Multi_Reader_Encrypted_Async() => - await Assert.ThrowsAsync(async () => - { - string[] archives = - [ - "Rar.EncryptedParts.part01.rar", - "Rar.EncryptedParts.part02.rar", - "Rar.EncryptedParts.part03.rar", - "Rar.EncryptedParts.part04.rar", - "Rar.EncryptedParts.part05.rar", - "Rar.EncryptedParts.part06.rar", - ]; - using ( - var reader = RarReader.Open( - archives - .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) - .Select(p => File.OpenRead(p)), - new ReaderOptions { Password = "test" } - ) - ) - { - while (await reader.MoveToNextEntryAsync()) - { - await reader.WriteEntryToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); - } - } - VerifyFiles(); - }); + [Fact] + public async ValueTask Rar_Multi_Reader_Encrypted_Async() => + await Assert.ThrowsAsync(async () => + { + string[] archives = + [ + "Rar.EncryptedParts.part01.rar", + "Rar.EncryptedParts.part02.rar", + "Rar.EncryptedParts.part03.rar", + "Rar.EncryptedParts.part04.rar", + "Rar.EncryptedParts.part05.rar", + "Rar.EncryptedParts.part06.rar", + ]; + using ( + IReader baseReader = RarReader.Open( + archives + .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) + .Select(p => File.OpenRead(p)), + new ReaderOptions { Password = "test" } + ) + ) + { + IAsyncReader reader = (IAsyncReader)baseReader; + while (await reader.MoveToNextEntryAsync()) + { + await reader.WriteEntryToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + VerifyFiles(); + }); [Fact] public async ValueTask Rar_Multi_Reader_Delete_Files_Async() => @@ -121,30 +123,31 @@ public class RarReaderAsyncTests : ReaderTests Path.Combine(SCRATCH2_FILES_PATH, file) ); } - var streams = archives - .Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)) - .Select(File.OpenRead) - .ToList(); - using (var reader = RarReader.Open(streams)) - { - while (await reader.MoveToNextEntryAsync()) - { - await reader.WriteEntryToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); - } - } - foreach (var stream in streams) - { - stream.Dispose(); - } - VerifyFiles(); + var streams = archives + .Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)) + .Select(File.OpenRead) + .ToList(); + using (IReader baseReader = RarReader.Open(streams)) + { + IAsyncReader reader = (IAsyncReader)baseReader; + while (await reader.MoveToNextEntryAsync()) + { + await reader.WriteEntryToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + foreach (var stream in streams) + { + stream.Dispose(); + } + VerifyFiles(); - foreach (var file in archives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))) - { - File.Delete(file); - } + foreach (var file in archives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))) + { + File.Delete(file); + } } [Fact] @@ -271,23 +274,24 @@ public class RarReaderAsyncTests : ReaderTests ); } - [Fact] - public async ValueTask Rar_Jpg_Reader_Async() - { - using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) - using (var reader = RarReader.Open(stream, new ReaderOptions { LookForHeader = true })) - { - while (await reader.MoveToNextEntryAsync()) - { - Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType); - await reader.WriteEntryToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); - } - } - VerifyFiles(); - } + [Fact] + public async ValueTask Rar_Jpg_Reader_Async() + { + using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) + using (IReader baseReader = RarReader.Open(stream, new ReaderOptions { LookForHeader = true })) + { + IAsyncReader reader = (IAsyncReader)baseReader; + while (await reader.MoveToNextEntryAsync()) + { + Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType); + await reader.WriteEntryToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + VerifyFiles(); + } [Fact] public async ValueTask Rar_Solid_Reader_Async() => diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index 9fae1c58..d140c788 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -47,7 +47,7 @@ public class TarArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); using (var archive2 = TarArchive.Open(unmodified)) { - Assert.Equal(1, archive2.Entries.Count); + Assert.Equal(1, archive2.Entries.Count()); Assert.Contains(filename, archive2.Entries.Select(entry => entry.Key)); foreach (var entry in archive2.Entries) @@ -91,7 +91,7 @@ public class TarArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); using (var archive2 = TarArchive.Open(unmodified)) { - Assert.Equal(1, archive2.Entries.Count); + Assert.Equal(1, archive2.Entries.Count()); Assert.Contains(longFilename, archive2.Entries.Select(entry => entry.Key)); foreach (var entry in archive2.Entries) @@ -128,7 +128,7 @@ public class TarArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.mod.tar"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - using (var archive = TarArchive.Open(unmodified)) + await using (var archive = TarArchive.OpenAsync(unmodified)) { archive.AddEntry("jpg\\test.jpg", jpg); await archive.SaveToAsync(scratchPath, new WriterOptions(CompressionType.None)); @@ -143,9 +143,9 @@ public class TarArchiveAsyncTests : ArchiveTests var modified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.mod.tar"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - using (var archive = TarArchive.Open(unmodified)) + await using (var archive = TarArchive.OpenAsync(unmodified)) { - var entry = archive.Entries.Single(x => + var entry = await archive.EntriesAsync.SingleAsync(x => x.Key.NotNull().EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ); archive.RemoveEntry(entry); diff --git a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs index efbde472..0f92681c 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs @@ -49,7 +49,7 @@ public class TarArchiveTests : ArchiveTests var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); using (var archive2 = TarArchive.Open(unmodified)) { - Assert.Equal(1, archive2.Entries.Count); + Assert.Equal(1, archive2.Entries.Count()); Assert.Contains(filename, archive2.Entries.Select(entry => entry.Key)); foreach (var entry in archive2.Entries) @@ -67,7 +67,7 @@ public class TarArchiveTests : ArchiveTests { var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "very long filename.tar"); using var archive = TarArchive.Open(unmodified); - Assert.Equal(5, archive.Entries.Count); + Assert.Equal(5, archive.Entries.Count()); Assert.Contains("very long filename/", archive.Entries.Select(entry => entry.Key)); Assert.Contains( "very long filename/very long filename very long filename very long filename very long filename very long filename very long filename very long filename very long filename very long filename very long filename.jpg", @@ -109,7 +109,7 @@ public class TarArchiveTests : ArchiveTests var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); using (var archive2 = TarArchive.Open(unmodified)) { - Assert.Equal(1, archive2.Entries.Count); + Assert.Equal(1, archive2.Entries.Count()); Assert.Contains(longFilename, archive2.Entries.Select(entry => entry.Key)); foreach (var entry in archive2.Entries) @@ -127,7 +127,7 @@ public class TarArchiveTests : ArchiveTests { var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "ustar with long names.tar"); using var archive = TarArchive.Open(unmodified); - Assert.Equal(6, archive.Entries.Count); + Assert.Equal(6, archive.Entries.Count()); Assert.Contains("Directory/", archive.Entries.Select(entry => entry.Key)); Assert.Contains( "Directory/Some file with veeeeeeeeeery loooooooooong name", diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs index ffec6efc..ecc1c9c8 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs @@ -126,10 +126,9 @@ public class ZipArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip"); - using (IArchive baseArchive = ZipArchive.Open(unmodified)) + await using (var archive = ZipArchive.OpenAsync(unmodified)) { - IWritableArchive archive = (IWritableArchive)baseArchive; - var entry = archive.Entries.Single(x => + var entry = await archive.EntriesAsync.SingleAsync(x => x.Key.NotNull().EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ); archive.RemoveEntry(entry); @@ -150,9 +149,8 @@ public class ZipArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using (IArchive baseArchive = ZipArchive.Open(unmodified)) + await using (var archive = ZipArchive.OpenAsync(unmodified)) { - IWritableArchive archive = (IWritableArchive)baseArchive; archive.AddEntry("jpg\\test.jpg", jpg); WriterOptions writerOptions = new ZipWriterOptions(CompressionType.Deflate); diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index 067e9953..a4e4712d 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -495,7 +495,7 @@ public class ZipArchiveTests : ArchiveTests new ReaderOptions { Password = "test" } ); var isComplete = reader.IsComplete; - Assert.Equal(1, reader.Volumes.Count); + Assert.Equal(1, reader.Volumes.Count()); var expectedComment = "Encoding:utf-8 || Compression:Deflate levelDefault || Encrypt:None || ZIP64:Always\r\nCreated at 2017-Jan-23 14:10:43 || DotNetZip Tool v1.9.1.8\r\nTest zip64 archive"; @@ -708,7 +708,7 @@ public class ZipArchiveTests : ArchiveTests var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.LongComment.zip"); using var za = ZipArchive.Open(zipPath); - var count = za.Entries.Count; + var count = za.Entries.Count(); Assert.Equal(1, count); } diff --git a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs index 9d5f4da9..7c2d85aa 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs @@ -121,8 +121,9 @@ public class ZipReaderAsyncTests : ReaderTests using ( Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip")) ) - using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (IReader baseReader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) { + IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) { if (!reader.Entry.IsDirectory) @@ -191,8 +192,11 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.WinzipAES.zip")) ) ) - using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using ( + IReader baseReader = ZipReader.Open(stream, new ReaderOptions { Password = "test" }) + ) { + IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) { if (!reader.Entry.IsDirectory) @@ -216,8 +220,9 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip")) ) ) - using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (IReader baseReader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) { + IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) { if (!reader.Entry.IsDirectory) @@ -242,8 +247,9 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "zipcrypto.zip")) ) ) - using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (IReader baseReader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) { + IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) { if (!reader.Entry.IsDirectory) From 0502ff545ed5bd4c5c1f937b6bdb8ad6ca14dd52 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 15:01:29 +0000 Subject: [PATCH 06/36] test fixes and fmt --- .../Archives/AbstractWritableArchive.cs | 3 +- .../Archives/IWritableArchive.cs | 2 - .../Archives/IWritableArchiveExtensions.cs | 5 +- .../IWritableAsyncArchiveExtensions.cs | 12 +- .../Archives/Rar/RarArchive.Extensions.cs | 1 - src/SharpCompress/Archives/Rar/RarArchive.cs | 10 +- src/SharpCompress/Archives/Tar/TarArchive.cs | 5 +- src/SharpCompress/Archives/Zip/ZipArchive.cs | 5 +- .../Polyfills/AsyncEnumerableExtensions.cs | 6 +- tests/SharpCompress.Test/ArchiveTests.cs | 5 +- .../GZip/GZipArchiveAsyncTests.cs | 3 +- .../Rar/RarReaderAsyncTests.cs | 194 +++++++++--------- tests/SharpCompress.Test/Zip/Zip64Tests.cs | 2 +- .../SharpCompress.Test/Zip/ZipArchiveTests.cs | 5 +- 14 files changed, 131 insertions(+), 127 deletions(-) diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.cs b/src/SharpCompress/Archives/AbstractWritableArchive.cs index e7bb2af2..6e482f23 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.cs @@ -12,7 +12,8 @@ namespace SharpCompress.Archives; public abstract class AbstractWritableArchive : AbstractArchive, - IWritableArchive, IWritableAsyncArchive + IWritableArchive, + IWritableAsyncArchive where TEntry : IArchiveEntry where TVolume : IVolume { diff --git a/src/SharpCompress/Archives/IWritableArchive.cs b/src/SharpCompress/Archives/IWritableArchive.cs index 514b8cdc..7c10910c 100644 --- a/src/SharpCompress/Archives/IWritableArchive.cs +++ b/src/SharpCompress/Archives/IWritableArchive.cs @@ -28,13 +28,11 @@ public interface IWritableArchiveCommon public interface IWritableArchive : IArchive, IWritableArchiveCommon { - void SaveTo(Stream stream, WriterOptions options); } public interface IWritableAsyncArchive : IAsyncArchive, IWritableArchiveCommon { - ValueTask SaveToAsync( Stream stream, WriterOptions options, diff --git a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs index 07b642ae..71b790b7 100644 --- a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs @@ -6,12 +6,11 @@ namespace SharpCompress.Archives; public static class IWritableArchiveExtensions { - public static void SaveTo( this IWritableArchive writableArchive, string filePath, WriterOptions? options = null - ) => writableArchive.SaveTo(new FileInfo(filePath), options ?? new (CompressionType.Deflate)); + ) => writableArchive.SaveTo(new FileInfo(filePath), options ?? new(CompressionType.Deflate)); public static void SaveTo( this IWritableArchive writableArchive, @@ -20,6 +19,6 @@ public static class IWritableArchiveExtensions ) { using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); - writableArchive.SaveTo(stream, options?? new (CompressionType.Deflate)); + writableArchive.SaveTo(stream, options ?? new(CompressionType.Deflate)); } } diff --git a/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs b/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs index ec43872c..ffe35eda 100644 --- a/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs @@ -13,7 +13,12 @@ public static class IWritableAsyncArchiveExtensions string filePath, WriterOptions? options = null, CancellationToken cancellationToken = default - ) => writableArchive.SaveToAsync(new FileInfo(filePath), options ?? new (CompressionType.Deflate), cancellationToken); + ) => + writableArchive.SaveToAsync( + new FileInfo(filePath), + options ?? new(CompressionType.Deflate), + cancellationToken + ); public static async ValueTask SaveToAsync( this IWritableAsyncArchive writableArchive, @@ -23,7 +28,8 @@ public static class IWritableAsyncArchiveExtensions ) { using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); - await writableArchive.SaveToAsync(stream, options?? new (CompressionType.Deflate), cancellationToken).ConfigureAwait(false); + await writableArchive + .SaveToAsync(stream, options ?? new(CompressionType.Deflate), cancellationToken) + .ConfigureAwait(false); } - } diff --git a/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs b/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs index ff19275f..71e2fa7c 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs @@ -18,7 +18,6 @@ public static class RarArchiveExtensions public static bool IsMultipartVolume(this IRarArchive archive) => archive.Volumes.Cast().First().IsMultiVolume; - /// /// RarArchive is the first volume of a multi-part archive. If MultipartVolume is true and IsFirstVolume is false then the first volume file must be missing. /// diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index 77568e86..05b41494 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -16,19 +16,13 @@ namespace SharpCompress.Archives.Rar; public interface IRarArchiveCommon { - - int MinVersion { get; } int MaxVersion { get; } } -public interface IRarArchive : IArchive, IRarArchiveCommon -{ -} +public interface IRarArchive : IArchive, IRarArchiveCommon { } -public interface IRarAsyncArchive : IAsyncArchive, IRarArchiveCommon -{ -} +public interface IRarAsyncArchive : IAsyncArchive, IRarArchiveCommon { } public class RarArchive : AbstractArchive, IRarArchive { diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index b1d19bda..f1c0254f 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -71,7 +71,10 @@ public class TarArchive : AbstractWritableArchive /// /// /// - public static IWritableArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IWritableArchive Open( + IEnumerable streams, + ReaderOptions? readerOptions = null + ) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index d23925b5..159cc6f4 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -92,7 +92,10 @@ public class ZipArchive : AbstractWritableArchive /// /// /// - public static IWritableArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IWritableArchive Open( + IEnumerable streams, + ReaderOptions? readerOptions = null + ) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index 5e8be6ec..e7cea151 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -126,10 +126,12 @@ public static class AsyncEnumerableExtensions { throw new InvalidOperationException("The source sequence is empty."); } - var value = enumerator.Current; + var value = enumerator.Current; if (await enumerator.MoveNextAsync()) { - throw new InvalidOperationException("The source sequence contains more than one element."); + throw new InvalidOperationException( + "The source sequence contains more than one element." + ); } return value; } diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index cdd98a33..5853e82c 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -601,10 +601,7 @@ public class ArchiveTests : ReaderTests ) ) await using ( - var archive = archiveFactory.OpenAsync( - new AsyncOnlyStream(stream), - readerOptions - ) + var archive = archiveFactory.OpenAsync(new AsyncOnlyStream(stream), readerOptions) ) { try diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index ba74de93..5eabdcb0 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -78,8 +78,7 @@ public class GZipArchiveAsyncTests : ArchiveTests #endif await using (var archive = GZipArchive.OpenAsync(stream)) { - Assert.Throws(() => - archive.AddEntry("jpg\\test.jpg", jpg)); + Assert.Throws(() => archive.AddEntry("jpg\\test.jpg", jpg)); await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); } } diff --git a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs index fe8d3b25..d58c2a93 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs @@ -36,61 +36,61 @@ public class RarReaderAsyncTests : ReaderTests "Rar5.multi.part06.rar", ]); - private async ValueTask DoRar_Multi_Reader_Async(string[] archives) - { - using ( - IReader baseReader = RarReader.Open( - archives - .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) - .Select(p => File.OpenRead(p)) - ) - ) - { - IAsyncReader reader = (IAsyncReader)baseReader; - while (await reader.MoveToNextEntryAsync()) - { - await reader.WriteEntryToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); - } - } - VerifyFiles(); - } + private async ValueTask DoRar_Multi_Reader_Async(string[] archives) + { + using ( + IReader baseReader = RarReader.Open( + archives + .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) + .Select(p => File.OpenRead(p)) + ) + ) + { + IAsyncReader reader = (IAsyncReader)baseReader; + while (await reader.MoveToNextEntryAsync()) + { + await reader.WriteEntryToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + VerifyFiles(); + } - [Fact] - public async ValueTask Rar_Multi_Reader_Encrypted_Async() => - await Assert.ThrowsAsync(async () => - { - string[] archives = - [ - "Rar.EncryptedParts.part01.rar", - "Rar.EncryptedParts.part02.rar", - "Rar.EncryptedParts.part03.rar", - "Rar.EncryptedParts.part04.rar", - "Rar.EncryptedParts.part05.rar", - "Rar.EncryptedParts.part06.rar", - ]; - using ( - IReader baseReader = RarReader.Open( - archives - .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) - .Select(p => File.OpenRead(p)), - new ReaderOptions { Password = "test" } - ) - ) - { - IAsyncReader reader = (IAsyncReader)baseReader; - while (await reader.MoveToNextEntryAsync()) - { - await reader.WriteEntryToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); - } - } - VerifyFiles(); - }); + [Fact] + public async ValueTask Rar_Multi_Reader_Encrypted_Async() => + await Assert.ThrowsAsync(async () => + { + string[] archives = + [ + "Rar.EncryptedParts.part01.rar", + "Rar.EncryptedParts.part02.rar", + "Rar.EncryptedParts.part03.rar", + "Rar.EncryptedParts.part04.rar", + "Rar.EncryptedParts.part05.rar", + "Rar.EncryptedParts.part06.rar", + ]; + using ( + IReader baseReader = RarReader.Open( + archives + .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) + .Select(p => File.OpenRead(p)), + new ReaderOptions { Password = "test" } + ) + ) + { + IAsyncReader reader = (IAsyncReader)baseReader; + while (await reader.MoveToNextEntryAsync()) + { + await reader.WriteEntryToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + VerifyFiles(); + }); [Fact] public async ValueTask Rar_Multi_Reader_Delete_Files_Async() => @@ -123,31 +123,31 @@ public class RarReaderAsyncTests : ReaderTests Path.Combine(SCRATCH2_FILES_PATH, file) ); } - var streams = archives - .Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)) - .Select(File.OpenRead) - .ToList(); - using (IReader baseReader = RarReader.Open(streams)) - { - IAsyncReader reader = (IAsyncReader)baseReader; - while (await reader.MoveToNextEntryAsync()) - { - await reader.WriteEntryToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); - } - } - foreach (var stream in streams) - { - stream.Dispose(); - } - VerifyFiles(); + var streams = archives + .Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)) + .Select(File.OpenRead) + .ToList(); + using (IReader baseReader = RarReader.Open(streams)) + { + IAsyncReader reader = (IAsyncReader)baseReader; + while (await reader.MoveToNextEntryAsync()) + { + await reader.WriteEntryToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + foreach (var stream in streams) + { + stream.Dispose(); + } + VerifyFiles(); - foreach (var file in archives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))) - { - File.Delete(file); - } + foreach (var file in archives.Select(s => Path.Combine(SCRATCH2_FILES_PATH, s))) + { + File.Delete(file); + } } [Fact] @@ -274,24 +274,26 @@ public class RarReaderAsyncTests : ReaderTests ); } - [Fact] - public async ValueTask Rar_Jpg_Reader_Async() - { - using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) - using (IReader baseReader = RarReader.Open(stream, new ReaderOptions { LookForHeader = true })) - { - IAsyncReader reader = (IAsyncReader)baseReader; - while (await reader.MoveToNextEntryAsync()) - { - Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType); - await reader.WriteEntryToDirectoryAsync( - SCRATCH_FILES_PATH, - new ExtractionOptions { ExtractFullPath = true, Overwrite = true } - ); - } - } - VerifyFiles(); - } + [Fact] + public async ValueTask Rar_Jpg_Reader_Async() + { + using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) + using ( + IReader baseReader = RarReader.Open(stream, new ReaderOptions { LookForHeader = true }) + ) + { + IAsyncReader reader = (IAsyncReader)baseReader; + while (await reader.MoveToNextEntryAsync()) + { + Assert.Equal(CompressionType.Rar, reader.Entry.CompressionType); + await reader.WriteEntryToDirectoryAsync( + SCRATCH_FILES_PATH, + new ExtractionOptions { ExtractFullPath = true, Overwrite = true } + ); + } + } + VerifyFiles(); + } [Fact] public async ValueTask Rar_Solid_Reader_Async() => diff --git a/tests/SharpCompress.Test/Zip/Zip64Tests.cs b/tests/SharpCompress.Test/Zip/Zip64Tests.cs index 43dc1874..dc733df0 100644 --- a/tests/SharpCompress.Test/Zip/Zip64Tests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64Tests.cs @@ -180,7 +180,7 @@ public class Zip64Tests : WriterTests { long count = 0; long size = 0; - ZipEntry? prev = null; + IEntry? prev = null; using (var fs = File.OpenRead(filename)) using (var rd = ZipReader.Open(fs, new ReaderOptions { LookForHeader = false })) { diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index a4e4712d..a8192416 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -5,6 +5,7 @@ using System.Text; using SharpCompress.Archives; using SharpCompress.Archives.Zip; using SharpCompress.Common; +using SharpCompress.Common.Zip; using SharpCompress.Readers; using SharpCompress.Writers; using SharpCompress.Writers.Zip; @@ -499,7 +500,7 @@ public class ZipArchiveTests : ArchiveTests var expectedComment = "Encoding:utf-8 || Compression:Deflate levelDefault || Encrypt:None || ZIP64:Always\r\nCreated at 2017-Jan-23 14:10:43 || DotNetZip Tool v1.9.1.8\r\nTest zip64 archive"; - Assert.Equal(expectedComment, reader.Volumes.First().Comment); + Assert.Equal(expectedComment, ((ZipVolume)reader.Volumes.First()).Comment); } [Fact] @@ -824,7 +825,7 @@ public class ZipArchiveTests : ArchiveTests using var archive = ZipArchive.Open( Path.Combine(TEST_ARCHIVES_PATH, "Zip.EntryComment.zip") ); - var firstEntry = archive.Entries.First(); + var firstEntry = (ZipArchiveEntry)archive.Entries.First(); Assert.Equal(29, firstEntry.Comment!.Length); using var _ = firstEntry.OpenEntryStream(); Assert.Equal(29, firstEntry.Comment.Length); From 6d6103afd6a3f7868002e4e7c66a20af7cda8d6f Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 16:08:16 +0000 Subject: [PATCH 07/36] update docs --- docs/API.md | 89 +++++++++++++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 33 deletions(-) diff --git a/docs/API.md b/docs/API.md index 88dbf2bb..ccf3ab9b 100644 --- a/docs/API.md +++ b/docs/API.md @@ -21,8 +21,8 @@ using (var archive = SevenZipArchive.Open("file.7z")) using (var archive = GZipArchive.Open("file.gz")) // With options -var options = new ReaderOptions -{ +var options = new ReaderOptions +{ Password = "password", LeaveStreamOpen = true, ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) } @@ -66,23 +66,23 @@ using (var archive = ZipArchive.Create()) using (var archive = ZipArchive.Open("file.zip")) { // Get all entries - IEnumerable entries = archive.Entries; - + IEnumerable entries = archive.Entries; + // Find specific entry var entry = archive.Entries.FirstOrDefault(e => e.Key == "file.txt"); - + // Extract all archive.WriteToDirectory(@"C:\output", new ExtractionOptions { ExtractFullPath = true, Overwrite = true }); - + // Extract single entry var entry = archive.Entries.First(); entry.WriteToFile(@"C:\output\file.txt"); entry.WriteToFile(@"C:\output\file.txt", new ExtractionOptions { Overwrite = true }); - + // Get entry stream using (var stream = entry.OpenEntryStream()) { @@ -90,8 +90,15 @@ using (var archive = ZipArchive.Open("file.zip")) } } -// Async variants -await archive.WriteToDirectoryAsync(@"C:\output", options, cancellationToken); +// Async extraction (requires IAsyncArchive) +using (var asyncArchive = await ZipArchive.OpenAsync("file.zip")) +{ + await asyncArchive.WriteToDirectoryAsync( + @"C:\output", + new ExtractionOptions { ExtractFullPath = true, Overwrite = true }, + cancellationToken: cancellationToken + ); +} using (var stream = await entry.OpenEntryStreamAsync(cancellationToken)) { // ... @@ -118,15 +125,15 @@ foreach (var entry in archive.Entries) using (var archive = ZipArchive.Create()) { // Add file - archive.AddEntry("file.txt", "C:\\source\\file.txt"); - + archive.AddEntry("file.txt", @"C:\source\file.txt"); + // Add multiple files - archive.AddAllFromDirectory("C:\\source"); - archive.AddAllFromDirectory("C:\\source", "*.txt"); // Pattern - + archive.AddAllFromDirectory(@"C:\source"); + archive.AddAllFromDirectory(@"C:\source", "*.txt"); // Pattern + // Save to file archive.SaveTo("output.zip", CompressionType.Deflate); - + // Save to stream archive.SaveTo(outputStream, new WriterOptions(CompressionType.Deflate) { @@ -148,14 +155,14 @@ using (var reader = ReaderFactory.Open(stream)) { while (reader.MoveToNextEntry()) { - IEntry entry = reader.Entry; - + IArchiveEntry entry = reader.Entry; + if (!entry.IsDirectory) { // Extract entry reader.WriteEntryToDirectory(@"C:\output"); reader.WriteEntryToFile(@"C:\output\file.txt"); - + // Or get stream using (var entryStream = reader.OpenEntryStream()) { @@ -165,16 +172,25 @@ using (var reader = ReaderFactory.Open(stream)) } } -// Async variants -while (await reader.MoveToNextEntryAsync()) +// Async variants (use OpenAsync to get IAsyncReader) +using (var stream = File.OpenRead("file.zip")) +using (var reader = await ReaderFactory.OpenAsync(stream)) { - await reader.WriteEntryToFileAsync(@"C:\output\" + reader.Entry.Key, cancellationToken); -} + while (await reader.MoveToNextEntryAsync()) + { + await reader.WriteEntryToFileAsync( + @"C:\output\" + reader.Entry.Key, + cancellationToken: cancellationToken + ); + } -// Async extraction -await reader.WriteAllToDirectoryAsync(@"C:\output", - new ExtractionOptions { ExtractFullPath = true, Overwrite = true }, - cancellationToken); + // Async extraction of all entries + await reader.WriteAllToDirectoryAsync( + @"C:\output", + new ExtractionOptions { ExtractFullPath = true, Overwrite = true }, + cancellationToken + ); +} ``` --- @@ -262,15 +278,20 @@ archive.WriteToDirectory(@"C:\output", options); // For creating archives CompressionType.None // No compression (store) CompressionType.Deflate // DEFLATE (default for ZIP/GZip) +CompressionType.Deflate64 // Deflate64 CompressionType.BZip2 // BZip2 CompressionType.LZMA // LZMA (for 7Zip, LZip, XZ) CompressionType.PPMd // PPMd (for ZIP) CompressionType.Rar // RAR compression (read-only) +CompressionType.ZStandard // ZStandard +ArchiveType.Arc +ArchiveType.Arj +ArchiveType.Ace -// For Tar archives -// Use CompressionType in TarWriter constructor -using (var writer = TarWriter(stream, CompressionType.GZip)) // Tar.GZip -using (var writer = TarWriter(stream, CompressionType.BZip2)) // Tar.BZip2 +// For Tar archives with compression +// Use WriterFactory to create compressed tar archives +using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.GZip)) // Tar.GZip +using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.BZip2)) // Tar.BZip2 ``` ### Archive Types @@ -342,11 +363,13 @@ cts.CancelAfter(TimeSpan.FromMinutes(5)); try { - using (var archive = ZipArchive.Open("archive.zip")) + using (var archive = await ZipArchive.OpenAsync("archive.zip")) { - await archive.WriteToDirectoryAsync(@"C:\output", + await archive.WriteToDirectoryAsync( + @"C:\output", new ExtractionOptions { ExtractFullPath = true, Overwrite = true }, - cts.Token); + cancellationToken: cts.Token + ); } } catch (OperationCanceledException) From 1b4cedfa13188fcd7896b1a03fc1902d3907cfb3 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 16:21:20 +0000 Subject: [PATCH 08/36] misc fixes --- SharpCompress.sln | 2 -- src/SharpCompress/Archives/AbstractArchive.cs | 6 ++++-- src/SharpCompress/Archives/IArchive.cs | 5 ++++- src/SharpCompress/Archives/IArchiveExtensions.cs | 2 +- src/SharpCompress/Archives/IAsyncArchive.cs | 7 ++++++- src/SharpCompress/Archives/IAsyncArchiveExtensions.cs | 2 +- src/SharpCompress/Archives/IWritableArchive.cs | 10 ++++++++++ 7 files changed, 26 insertions(+), 8 deletions(-) diff --git a/SharpCompress.sln b/SharpCompress.sln index ba04063b..353e73d7 100644 --- a/SharpCompress.sln +++ b/SharpCompress.sln @@ -21,9 +21,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Config", "Config", "{CDB425 Directory.Packages.props = Directory.Packages.props NuGet.config = NuGet.config .github\workflows\nuget-release.yml = .github\workflows\nuget-release.yml - USAGE.md = USAGE.md README.md = README.md - FORMATS.md = FORMATS.md AGENTS.md = AGENTS.md EndProjectSection EndProject diff --git a/src/SharpCompress/Archives/AbstractArchive.cs b/src/SharpCompress/Archives/AbstractArchive.cs index 844035cd..63e67e69 100644 --- a/src/SharpCompress/Archives/AbstractArchive.cs +++ b/src/SharpCompress/Archives/AbstractArchive.cs @@ -68,7 +68,7 @@ public abstract class AbstractArchive : IArchive, IAsyncArchive /// /// The total size of the files as uncompressed in the archive. /// - public virtual long TotalUncompressSize => + public virtual long TotalUncompressedSize => Entries.Aggregate(0L, (total, cf) => total + cf.Size); protected abstract IEnumerable LoadVolumes(SourceStream sourceStream); @@ -231,8 +231,10 @@ public abstract class AbstractArchive : IArchive, IAsyncArchive public async ValueTask TotalSizeAsync() => await EntriesAsync.AggregateAsync(0L, (total, cf) => total + cf.CompressedSize); - public async ValueTask TotalUncompressSizeAsync() => + public async ValueTask TotalUncompressedSizeAsync() => await EntriesAsync.AggregateAsync(0L, (total, cf) => total + cf.Size); + public ValueTask IsEncryptedAsync() => new(IsEncrypted); + #endregion } diff --git a/src/SharpCompress/Archives/IArchive.cs b/src/SharpCompress/Archives/IArchive.cs index 0abb2f1b..6016214b 100644 --- a/src/SharpCompress/Archives/IArchive.cs +++ b/src/SharpCompress/Archives/IArchive.cs @@ -38,7 +38,10 @@ public interface IArchive : IDisposable /// /// The total size of the files as uncompressed in the archive. /// - long TotalUncompressSize { get; } + long TotalUncompressedSize { get; } + /// + /// Returns whether the archive is encrypted. + /// bool IsEncrypted { get; } } diff --git a/src/SharpCompress/Archives/IArchiveExtensions.cs b/src/SharpCompress/Archives/IArchiveExtensions.cs index c1d2ac98..f02dfc38 100644 --- a/src/SharpCompress/Archives/IArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IArchiveExtensions.cs @@ -43,7 +43,7 @@ public static class IArchiveExtensions ) { // Prepare for progress reporting - var totalBytes = archive.TotalUncompressSize; + var totalBytes = archive.TotalUncompressedSize; var bytesRead = 0L; // Tracking for created directories. diff --git a/src/SharpCompress/Archives/IAsyncArchive.cs b/src/SharpCompress/Archives/IAsyncArchive.cs index bd3f290e..2994e8b8 100644 --- a/src/SharpCompress/Archives/IAsyncArchive.cs +++ b/src/SharpCompress/Archives/IAsyncArchive.cs @@ -39,5 +39,10 @@ public interface IAsyncArchive : IAsyncDisposable /// /// The total size of the files as uncompressed in the archive. /// - ValueTask TotalUncompressSizeAsync(); + ValueTask TotalUncompressedSizeAsync(); + + /// + /// Returns whether the archive is encrypted. + /// + ValueTask IsEncryptedAsync(); } diff --git a/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs b/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs index b6b0cad1..128258b3 100644 --- a/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs @@ -53,7 +53,7 @@ public static class IAsyncArchiveExtensions ) { // Prepare for progress reporting - var totalBytes = await archive.TotalUncompressSizeAsync(); + var totalBytes = await archive.TotalUncompressedSizeAsync(); var bytesRead = 0L; // Tracking for created directories. diff --git a/src/SharpCompress/Archives/IWritableArchive.cs b/src/SharpCompress/Archives/IWritableArchive.cs index 7c10910c..28496b48 100644 --- a/src/SharpCompress/Archives/IWritableArchive.cs +++ b/src/SharpCompress/Archives/IWritableArchive.cs @@ -13,6 +13,10 @@ public interface IWritableArchiveCommon /// /// IDisposeable to resume entry rebuilding IDisposable PauseEntryRebuilding(); + + /// + /// Removes the specified entry from the archive. + /// void RemoveEntry(IArchiveEntry entry); IArchiveEntry AddEntry( @@ -28,11 +32,17 @@ public interface IWritableArchiveCommon public interface IWritableArchive : IArchive, IWritableArchiveCommon { + /// + /// Saves the archive to the specified stream using the given writer options. + /// void SaveTo(Stream stream, WriterOptions options); } public interface IWritableAsyncArchive : IAsyncArchive, IWritableArchiveCommon { + /// + /// Asynchronously saves the archive to the specified stream using the given writer options. + /// ValueTask SaveToAsync( Stream stream, WriterOptions options, From 4df952db1b6be812c4cfcb2ee8f072be234f0ca8 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Mon, 12 Jan 2026 16:32:26 +0000 Subject: [PATCH 09/36] split out factories for archive --- .../Archives/ArchiveFactory.Open.cs | 347 +++++++++++++++ src/SharpCompress/Archives/ArchiveFactory.cs | 416 +----------------- .../Archives/GZip/GZipArchive.Factory.cs | 171 +++++++ .../Archives/GZip/GZipArchive.cs | 218 +-------- .../Archives/Rar/RarArchive.Factory.cs | 148 +++++++ src/SharpCompress/Archives/Rar/RarArchive.cs | 194 +------- .../SevenZip/SevenZipArchive.Factory.cs | 151 +++++++ .../Archives/SevenZip/SevenZipArchive.cs | 224 +--------- .../Archives/Tar/TarArchive.Factory.cs | 162 +++++++ src/SharpCompress/Archives/Tar/TarArchive.cs | 203 +-------- .../Archives/Zip/ZipArchive.Factory.cs | 317 +++++++++++++ src/SharpCompress/Archives/Zip/ZipArchive.cs | 373 +--------------- 12 files changed, 1322 insertions(+), 1602 deletions(-) create mode 100644 src/SharpCompress/Archives/ArchiveFactory.Open.cs create mode 100644 src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs create mode 100644 src/SharpCompress/Archives/Rar/RarArchive.Factory.cs create mode 100644 src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs create mode 100644 src/SharpCompress/Archives/Tar/TarArchive.Factory.cs create mode 100644 src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs diff --git a/src/SharpCompress/Archives/ArchiveFactory.Open.cs b/src/SharpCompress/Archives/ArchiveFactory.Open.cs new file mode 100644 index 00000000..25a104ec --- /dev/null +++ b/src/SharpCompress/Archives/ArchiveFactory.Open.cs @@ -0,0 +1,347 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Common; +using SharpCompress.Factories; +using SharpCompress.IO; +using SharpCompress.Readers; + +namespace SharpCompress.Archives; + +public static partial class ArchiveFactory +{ + public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + readerOptions ??= new ReaderOptions(); + stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); + return FindFactory(stream).Open(stream, readerOptions); + } + + public static async ValueTask OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + readerOptions ??= new ReaderOptions(); + stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); + var factory = await FindFactoryAsync(stream, cancellationToken); + return factory.OpenAsync(stream, readerOptions); + } + + public static IWritableArchive Create(ArchiveType type) + { + var factory = Factory + .Factories.OfType() + .FirstOrDefault(item => item.KnownArchiveType == type); + + if (factory != null) + { + return factory.CreateWriteableArchive(); + } + + throw new NotSupportedException("Cannot create Archives of type: " + type); + } + + public static IArchive Open(string filePath, ReaderOptions? options = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), options); + } + + public static ValueTask OpenAsync( + string filePath, + ReaderOptions? options = null, + CancellationToken cancellationToken = default + ) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return OpenAsync(new FileInfo(filePath), options, cancellationToken); + } + + public static IArchive Open(FileInfo fileInfo, ReaderOptions? options = null) + { + options ??= new ReaderOptions { LeaveStreamOpen = false }; + + return FindFactory(fileInfo).Open(fileInfo, options); + } + + public static async ValueTask OpenAsync( + FileInfo fileInfo, + ReaderOptions? options = null, + CancellationToken cancellationToken = default + ) + { + options ??= new ReaderOptions { LeaveStreamOpen = false }; + + var factory = await FindFactoryAsync(fileInfo, cancellationToken); + return factory.OpenAsync(fileInfo, options, cancellationToken); + } + + public static IArchive Open(IEnumerable fileInfos, ReaderOptions? options = null) + { + fileInfos.NotNull(nameof(fileInfos)); + var filesArray = fileInfos.ToArray(); + if (filesArray.Length == 0) + { + throw new InvalidOperationException("No files to open"); + } + + var fileInfo = filesArray[0]; + if (filesArray.Length == 1) + { + return Open(fileInfo, options); + } + + fileInfo.NotNull(nameof(fileInfo)); + options ??= new ReaderOptions { LeaveStreamOpen = false }; + + return FindFactory(fileInfo).Open(filesArray, options); + } + + public static async ValueTask OpenAsync( + IEnumerable fileInfos, + ReaderOptions? options = null, + CancellationToken cancellationToken = default + ) + { + fileInfos.NotNull(nameof(fileInfos)); + var filesArray = fileInfos.ToArray(); + if (filesArray.Length == 0) + { + throw new InvalidOperationException("No files to open"); + } + + var fileInfo = filesArray[0]; + if (filesArray.Length == 1) + { + return await OpenAsync(fileInfo, options, cancellationToken); + } + + fileInfo.NotNull(nameof(fileInfo)); + options ??= new ReaderOptions { LeaveStreamOpen = false }; + + var factory = await FindFactoryAsync(fileInfo, cancellationToken); + return factory.OpenAsync(filesArray, options, cancellationToken); + } + + public static IArchive Open(IEnumerable streams, ReaderOptions? options = null) + { + streams.NotNull(nameof(streams)); + var streamsArray = streams.ToArray(); + if (streamsArray.Length == 0) + { + throw new InvalidOperationException("No streams"); + } + + var firstStream = streamsArray[0]; + if (streamsArray.Length == 1) + { + return Open(firstStream, options); + } + + firstStream.NotNull(nameof(firstStream)); + options ??= new ReaderOptions(); + + return FindFactory(firstStream).Open(streamsArray, options); + } + + public static async ValueTask OpenAsync( + IEnumerable streams, + ReaderOptions? options = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + streams.NotNull(nameof(streams)); + var streamsArray = streams.ToArray(); + if (streamsArray.Length == 0) + { + throw new InvalidOperationException("No streams"); + } + + var firstStream = streamsArray[0]; + if (streamsArray.Length == 1) + { + return await OpenAsync(firstStream, options, cancellationToken); + } + + firstStream.NotNull(nameof(firstStream)); + options ??= new ReaderOptions(); + + var factory = FindFactory(firstStream); + return factory.OpenAsync(streamsArray, options); + } + + public static void WriteToDirectory( + string sourceArchive, + string destinationDirectory, + ExtractionOptions? options = null + ) + { + using var archive = Open(sourceArchive); + archive.WriteToDirectory(destinationDirectory, options); + } + + private static T FindFactory(FileInfo finfo) + where T : IFactory + { + finfo.NotNull(nameof(finfo)); + using Stream stream = finfo.OpenRead(); + return FindFactory(stream); + } + + private static T FindFactory(Stream stream) + where T : IFactory + { + stream.NotNull(nameof(stream)); + if (!stream.CanRead || !stream.CanSeek) + { + throw new ArgumentException("Stream should be readable and seekable"); + } + + var factories = Factory.Factories.OfType(); + + var startPosition = stream.Position; + + foreach (var factory in factories) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + if (factory.IsArchive(stream)) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + return factory; + } + } + + var extensions = string.Join(", ", factories.Select(item => item.Name)); + + throw new InvalidOperationException( + $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" + ); + } + + private static async ValueTask FindFactoryAsync( + FileInfo finfo, + CancellationToken cancellationToken + ) + where T : IFactory + { + finfo.NotNull(nameof(finfo)); + using Stream stream = finfo.OpenRead(); + return await FindFactoryAsync(stream, cancellationToken); + } + + private static async ValueTask FindFactoryAsync( + Stream stream, + CancellationToken cancellationToken + ) + where T : IFactory + { + stream.NotNull(nameof(stream)); + if (!stream.CanRead || !stream.CanSeek) + { + throw new ArgumentException("Stream should be readable and seekable"); + } + + var factories = Factory.Factories.OfType(); + + var startPosition = stream.Position; + + foreach (var factory in factories) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + if (await factory.IsArchiveAsync(stream, cancellationToken: cancellationToken)) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + return factory; + } + } + + var extensions = string.Join(", ", factories.Select(item => item.Name)); + + throw new InvalidOperationException( + $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" + ); + } + + public static bool IsArchive( + string filePath, + out ArchiveType? type, + int bufferSize = ReaderOptions.DefaultBufferSize + ) + { + filePath.NotNullOrEmpty(nameof(filePath)); + using Stream s = File.OpenRead(filePath); + return IsArchive(s, out type, bufferSize); + } + + public static bool IsArchive( + Stream stream, + out ArchiveType? type, + int bufferSize = ReaderOptions.DefaultBufferSize + ) + { + type = null; + stream.NotNull(nameof(stream)); + + if (!stream.CanRead || !stream.CanSeek) + { + throw new ArgumentException("Stream should be readable and seekable"); + } + + var startPosition = stream.Position; + + foreach (var factory in Factory.Factories) + { + var isArchive = factory.IsArchive(stream); + stream.Position = startPosition; + + if (isArchive) + { + type = factory.KnownArchiveType; + return true; + } + } + + return false; + } + + public static IEnumerable GetFileParts(string part1) + { + part1.NotNullOrEmpty(nameof(part1)); + return GetFileParts(new FileInfo(part1)).Select(a => a.FullName); + } + + public static IEnumerable GetFileParts(FileInfo part1) + { + part1.NotNull(nameof(part1)); + yield return part1; + + foreach (var factory in Factory.Factories.OfType()) + { + var i = 1; + var part = factory.GetFilePart(i++, part1); + + if (part != null) + { + yield return part; + while ((part = factory.GetFilePart(i++, part1)) != null) + { + yield return part; + } + + yield break; + } + } + } + + public static IArchiveFactory AutoFactory { get; } = new AutoArchiveFactory(); +} diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index fb1addc0..a6073240 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -1,417 +1,3 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using SharpCompress.Common; -using SharpCompress.Factories; -using SharpCompress.IO; -using SharpCompress.Readers; - namespace SharpCompress.Archives; -public static class ArchiveFactory -{ - /// - /// Opens an Archive for random access - /// - /// - /// - /// - public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) - { - readerOptions ??= new ReaderOptions(); - stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); - return FindFactory(stream).Open(stream, readerOptions); - } - - /// - /// Opens an Archive for random access asynchronously - /// - /// - /// - /// - /// - public static async ValueTask OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - readerOptions ??= new ReaderOptions(); - stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); - var factory = await FindFactoryAsync(stream, cancellationToken); - return factory.OpenAsync(stream, readerOptions); - } - - public static IWritableArchive Create(ArchiveType type) - { - var factory = Factory - .Factories.OfType() - .FirstOrDefault(item => item.KnownArchiveType == type); - - if (factory != null) - { - return factory.CreateWriteableArchive(); - } - - throw new NotSupportedException("Cannot create Archives of type: " + type); - } - - /// - /// Constructor expects a filepath to an existing file. - /// - /// - /// - public static IArchive Open(string filePath, ReaderOptions? options = null) - { - filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), options); - } - - /// - /// Opens an Archive from a filepath asynchronously. - /// - /// - /// - /// - public static ValueTask OpenAsync( - string filePath, - ReaderOptions? options = null, - CancellationToken cancellationToken = default - ) - { - filePath.NotNullOrEmpty(nameof(filePath)); - return OpenAsync(new FileInfo(filePath), options, cancellationToken); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - /// - public static IArchive Open(FileInfo fileInfo, ReaderOptions? options = null) - { - options ??= new ReaderOptions { LeaveStreamOpen = false }; - - return FindFactory(fileInfo).Open(fileInfo, options); - } - - /// - /// Opens an Archive from a FileInfo object asynchronously. - /// - /// - /// - /// - public static async ValueTask OpenAsync( - FileInfo fileInfo, - ReaderOptions? options = null, - CancellationToken cancellationToken = default - ) - { - options ??= new ReaderOptions { LeaveStreamOpen = false }; - - var factory = await FindFactoryAsync(fileInfo, cancellationToken); - return factory.OpenAsync(fileInfo, options, cancellationToken); - } - - /// - /// Constructor with IEnumerable FileInfo objects, multi and split support. - /// - /// - /// - public static IArchive Open(IEnumerable fileInfos, ReaderOptions? options = null) - { - fileInfos.NotNull(nameof(fileInfos)); - var filesArray = fileInfos.ToArray(); - if (filesArray.Length == 0) - { - throw new InvalidOperationException("No files to open"); - } - - var fileInfo = filesArray[0]; - if (filesArray.Length == 1) - { - return Open(fileInfo, options); - } - - fileInfo.NotNull(nameof(fileInfo)); - options ??= new ReaderOptions { LeaveStreamOpen = false }; - - return FindFactory(fileInfo).Open(filesArray, options); - } - - /// - /// Opens a multi-part archive from files asynchronously. - /// - /// - /// - /// - public static async ValueTask OpenAsync( - IEnumerable fileInfos, - ReaderOptions? options = null, - CancellationToken cancellationToken = default - ) - { - fileInfos.NotNull(nameof(fileInfos)); - var filesArray = fileInfos.ToArray(); - if (filesArray.Length == 0) - { - throw new InvalidOperationException("No files to open"); - } - - var fileInfo = filesArray[0]; - if (filesArray.Length == 1) - { - return await OpenAsync(fileInfo, options, cancellationToken); - } - - fileInfo.NotNull(nameof(fileInfo)); - options ??= new ReaderOptions { LeaveStreamOpen = false }; - - var factory = await FindFactoryAsync(fileInfo, cancellationToken); - return factory.OpenAsync(filesArray, options, cancellationToken); - } - - /// - /// Constructor with IEnumerable FileInfo objects, multi and split support. - /// - /// - /// - public static IArchive Open(IEnumerable streams, ReaderOptions? options = null) - { - streams.NotNull(nameof(streams)); - var streamsArray = streams.ToArray(); - if (streamsArray.Length == 0) - { - throw new InvalidOperationException("No streams"); - } - - var firstStream = streamsArray[0]; - if (streamsArray.Length == 1) - { - return Open(firstStream, options); - } - - firstStream.NotNull(nameof(firstStream)); - options ??= new ReaderOptions(); - - return FindFactory(firstStream).Open(streamsArray, options); - } - - /// - /// Opens a multi-part archive from streams asynchronously. - /// - /// - /// - /// - public static async ValueTask OpenAsync( - IEnumerable streams, - ReaderOptions? options = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - streams.NotNull(nameof(streams)); - var streamsArray = streams.ToArray(); - if (streamsArray.Length == 0) - { - throw new InvalidOperationException("No streams"); - } - - var firstStream = streamsArray[0]; - if (streamsArray.Length == 1) - { - return await OpenAsync(firstStream, options, cancellationToken); - } - - firstStream.NotNull(nameof(firstStream)); - options ??= new ReaderOptions(); - - var factory = FindFactory(firstStream); - return factory.OpenAsync(streamsArray, options); - } - - /// - /// Extract to specific directory, retaining filename - /// - public static void WriteToDirectory( - string sourceArchive, - string destinationDirectory, - ExtractionOptions? options = null - ) - { - using var archive = Open(sourceArchive); - archive.WriteToDirectory(destinationDirectory, options); - } - - private static T FindFactory(FileInfo finfo) - where T : IFactory - { - finfo.NotNull(nameof(finfo)); - using Stream stream = finfo.OpenRead(); - return FindFactory(stream); - } - - private static T FindFactory(Stream stream) - where T : IFactory - { - stream.NotNull(nameof(stream)); - if (!stream.CanRead || !stream.CanSeek) - { - throw new ArgumentException("Stream should be readable and seekable"); - } - - var factories = Factory.Factories.OfType(); - - var startPosition = stream.Position; - - foreach (var factory in factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - if (factory.IsArchive(stream)) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - return factory; - } - } - - var extensions = string.Join(", ", factories.Select(item => item.Name)); - - throw new InvalidOperationException( - $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" - ); - } - - private static async ValueTask FindFactoryAsync( - FileInfo finfo, - CancellationToken cancellationToken - ) - where T : IFactory - { - finfo.NotNull(nameof(finfo)); - using Stream stream = finfo.OpenRead(); - return await FindFactoryAsync(stream, cancellationToken); - } - - private static async ValueTask FindFactoryAsync( - Stream stream, - CancellationToken cancellationToken - ) - where T : IFactory - { - stream.NotNull(nameof(stream)); - if (!stream.CanRead || !stream.CanSeek) - { - throw new ArgumentException("Stream should be readable and seekable"); - } - - var factories = Factory.Factories.OfType(); - - var startPosition = stream.Position; - - foreach (var factory in factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - if (await factory.IsArchiveAsync(stream, cancellationToken: cancellationToken)) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - return factory; - } - } - - var extensions = string.Join(", ", factories.Select(item => item.Name)); - - throw new InvalidOperationException( - $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" - ); - } - - public static bool IsArchive( - string filePath, - out ArchiveType? type, - int bufferSize = ReaderOptions.DefaultBufferSize - ) - { - filePath.NotNullOrEmpty(nameof(filePath)); - using Stream s = File.OpenRead(filePath); - return IsArchive(s, out type, bufferSize); - } - - public static bool IsArchive( - Stream stream, - out ArchiveType? type, - int bufferSize = ReaderOptions.DefaultBufferSize - ) - { - type = null; - stream.NotNull(nameof(stream)); - - if (!stream.CanRead || !stream.CanSeek) - { - throw new ArgumentException("Stream should be readable and seekable"); - } - - var startPosition = stream.Position; - - foreach (var factory in Factory.Factories) - { - var isArchive = factory.IsArchive(stream); - stream.Position = startPosition; - - if (isArchive) - { - type = factory.KnownArchiveType; - return true; - } - } - - return false; - } - - /// - /// From a passed in archive (zip, rar, 7z, 001), return all parts. - /// - /// - /// - public static IEnumerable GetFileParts(string part1) - { - part1.NotNullOrEmpty(nameof(part1)); - return GetFileParts(new FileInfo(part1)).Select(a => a.FullName); - } - - /// - /// From a passed in archive (zip, rar, 7z, 001), return all parts. - /// - /// - /// - public static IEnumerable GetFileParts(FileInfo part1) - { - part1.NotNull(nameof(part1)); - yield return part1; - - foreach (var factory in Factory.Factories.OfType()) - { - var i = 1; - var part = factory.GetFilePart(i++, part1); - - if (part != null) - { - yield return part; - while ((part = factory.GetFilePart(i++, part1)) != null) //tests split too - { - yield return part; - } - - yield break; - } - } - } - - public static IArchiveFactory AutoFactory { get; } = new AutoArchiveFactory(); -} +public static partial class ArchiveFactory { } diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs new file mode 100644 index 00000000..ca6cd6a3 --- /dev/null +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs @@ -0,0 +1,171 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Common; +using SharpCompress.Common.GZip; +using SharpCompress.IO; +using SharpCompress.Readers; +using SharpCompress.Readers.GZip; +using SharpCompress.Writers; +using SharpCompress.Writers.GZip; + +namespace SharpCompress.Archives.GZip; + +public partial class GZipArchive +{ + public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); + } + + public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return new GZipArchive( + new SourceStream( + fileInfo, + i => ArchiveVolumeFactory.GetFilePart(i, fileInfo), + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IArchive Open( + IEnumerable fileInfos, + ReaderOptions? readerOptions = null + ) + { + fileInfos.NotNull(nameof(fileInfos)); + var files = fileInfos.ToArray(); + return new GZipArchive( + new SourceStream( + files[0], + i => i < files.Length ? files[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + { + streams.NotNull(nameof(streams)); + var strms = streams.ToArray(); + return new GZipArchive( + new SourceStream( + strms[0], + i => i < strms.Length ? strms[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + stream.NotNull(nameof(stream)); + + if (stream is not { CanSeek: true }) + { + throw new ArgumentException("Stream must be seekable", nameof(stream)); + } + + return new GZipArchive( + new SourceStream(stream, _ => null, readerOptions ?? new ReaderOptions()) + ); + } + + public static IWritableAsyncArchive OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(stream, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(fileInfo, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + IReadOnlyList streams, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(streams, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(fileInfos, readerOptions); + } + + public static GZipArchive Create() => new(); + + public static bool IsGZipFile(string filePath) => IsGZipFile(new FileInfo(filePath)); + + public static bool IsGZipFile(FileInfo fileInfo) + { + if (!fileInfo.Exists) + { + return false; + } + + using Stream stream = fileInfo.OpenRead(); + return IsGZipFile(stream); + } + + public static bool IsGZipFile(Stream stream) + { + Span header = stackalloc byte[10]; + + if (!stream.ReadFully(header)) + { + return false; + } + + if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8) + { + return false; + } + + return true; + } + + public static async ValueTask IsGZipFileAsync( + Stream stream, + CancellationToken cancellationToken = default + ) + { + byte[] header = new byte[10]; + + if (!await stream.ReadFullyAsync(header, cancellationToken).ConfigureAwait(false)) + { + return false; + } + + if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8) + { + return false; + } + + return true; + } +} diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index b4c1b86f..03503fb8 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -14,186 +14,20 @@ using SharpCompress.Writers.GZip; namespace SharpCompress.Archives.GZip; -public class GZipArchive : AbstractWritableArchive +public partial class GZipArchive : AbstractWritableArchive { - /// - /// Constructor expects a filepath to an existing file. - /// - /// - /// - public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) - { - filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - /// - public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) - { - fileInfo.NotNull(nameof(fileInfo)); - return new GZipArchive( - new SourceStream( - fileInfo, - i => ArchiveVolumeFactory.GetFilePart(i, fileInfo), - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all file parts passed in - /// - /// - /// - public static IArchive Open( - IEnumerable fileInfos, - ReaderOptions? readerOptions = null - ) - { - fileInfos.NotNull(nameof(fileInfos)); - var files = fileInfos.ToArray(); - return new GZipArchive( - new SourceStream( - files[0], - i => i < files.Length ? files[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all stream parts passed in - /// - /// - /// - public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) - { - streams.NotNull(nameof(streams)); - var strms = streams.ToArray(); - return new GZipArchive( - new SourceStream( - strms[0], - i => i < strms.Length ? strms[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) - { - stream.NotNull(nameof(stream)); - - if (stream is not { CanSeek: true }) - { - throw new ArgumentException("Stream must be seekable", nameof(stream)); - } - - return new GZipArchive( - new SourceStream(stream, _ => null, readerOptions ?? new ReaderOptions()) - ); - } - - /// - /// Opens a GZipArchive asynchronously from a stream. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(stream, readerOptions); - } - - /// - /// Opens a GZipArchive asynchronously from a FileInfo. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - FileInfo fileInfo, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfo, readerOptions); - } - - /// - /// Opens a GZipArchive asynchronously from multiple streams. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(streams, readerOptions); - } - - /// - /// Opens a GZipArchive asynchronously from multiple FileInfo objects. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - IReadOnlyList fileInfos, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfos, readerOptions); - } - - public static GZipArchive Create() => new(); - - /// - /// Constructor with a SourceStream able to handle FileInfo and Streams. - /// - /// private GZipArchive(SourceStream sourceStream) : base(ArchiveType.GZip, sourceStream) { } + internal GZipArchive() + : base(ArchiveType.GZip) { } + protected override IEnumerable LoadVolumes(SourceStream sourceStream) { sourceStream.LoadAllParts(); return sourceStream.Streams.Select(a => new GZipVolume(a, ReaderOptions, 0)); } - public static bool IsGZipFile(string filePath) => IsGZipFile(new FileInfo(filePath)); - - public static bool IsGZipFile(FileInfo fileInfo) - { - if (!fileInfo.Exists) - { - return false; - } - - using Stream stream = fileInfo.OpenRead(); - return IsGZipFile(stream); - } - public void SaveTo(string filePath) => SaveTo(new FileInfo(filePath)); public void SaveTo(FileInfo fileInfo) @@ -215,50 +49,6 @@ public class GZipArchive : AbstractWritableArchive .ConfigureAwait(false); } - public static bool IsGZipFile(Stream stream) - { - // read the header on the first read - Span header = stackalloc byte[10]; - - // workitem 8501: handle edge case (decompress empty stream) - if (!stream.ReadFully(header)) - { - return false; - } - - if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8) - { - return false; - } - - return true; - } - - public static async ValueTask IsGZipFileAsync( - Stream stream, - CancellationToken cancellationToken = default - ) - { - // read the header on the first read - byte[] header = new byte[10]; - - // workitem 8501: handle edge case (decompress empty stream) - if (!await stream.ReadFullyAsync(header, cancellationToken).ConfigureAwait(false)) - { - return false; - } - - if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8) - { - return false; - } - - return true; - } - - internal GZipArchive() - : base(ArchiveType.GZip) { } - protected override GZipArchiveEntry CreateEntryInternal( string filePath, Stream source, diff --git a/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs b/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs new file mode 100644 index 00000000..f5dc8383 --- /dev/null +++ b/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs @@ -0,0 +1,148 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using SharpCompress.Common; +using SharpCompress.Common.Rar; +using SharpCompress.Common.Rar.Headers; +using SharpCompress.Compressors.Rar; +using SharpCompress.IO; +using SharpCompress.Readers; +using SharpCompress.Readers.Rar; + +namespace SharpCompress.Archives.Rar; + +public partial class RarArchive +{ + public static IRarArchive Open(string filePath, ReaderOptions? options = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + var fileInfo = new FileInfo(filePath); + return new RarArchive( + new SourceStream( + fileInfo, + i => RarArchiveVolumeFactory.GetFilePart(i, fileInfo), + options ?? new ReaderOptions() + ) + ); + } + + public static IRarArchive Open(FileInfo fileInfo, ReaderOptions? options = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return new RarArchive( + new SourceStream( + fileInfo, + i => RarArchiveVolumeFactory.GetFilePart(i, fileInfo), + options ?? new ReaderOptions() + ) + ); + } + + public static IRarArchive Open(Stream stream, ReaderOptions? options = null) + { + stream.NotNull(nameof(stream)); + + if (stream is not { CanSeek: true }) + { + throw new ArgumentException("Stream must be seekable", nameof(stream)); + } + + return new RarArchive(new SourceStream(stream, _ => null, options ?? new ReaderOptions())); + } + + public static IRarArchive Open( + IEnumerable fileInfos, + ReaderOptions? readerOptions = null + ) + { + fileInfos.NotNull(nameof(fileInfos)); + var files = fileInfos.ToArray(); + return new RarArchive( + new SourceStream( + files[0], + i => i < files.Length ? files[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IRarArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + { + streams.NotNull(nameof(streams)); + var strms = streams.ToArray(); + return new RarArchive( + new SourceStream( + strms[0], + i => i < strms.Length ? strms[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IRarAsyncArchive OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IRarAsyncArchive)Open(stream, readerOptions); + } + + public static IRarAsyncArchive OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IRarAsyncArchive)Open(fileInfo, readerOptions); + } + + public static IRarAsyncArchive OpenAsync( + IReadOnlyList streams, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IRarAsyncArchive)Open(streams, readerOptions); + } + + public static IRarAsyncArchive OpenAsync( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IRarAsyncArchive)Open(fileInfos, readerOptions); + } + + public static bool IsRarFile(string filePath) => IsRarFile(new FileInfo(filePath)); + + public static bool IsRarFile(FileInfo fileInfo) + { + if (!fileInfo.Exists) + { + return false; + } + using Stream stream = fileInfo.OpenRead(); + return IsRarFile(stream); + } + + public static bool IsRarFile(Stream stream, ReaderOptions? options = null) + { + try + { + MarkHeader.Read(stream, true, false); + return true; + } + catch + { + return false; + } + } +} diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index 05b41494..d67dd15c 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -24,17 +24,13 @@ public interface IRarArchive : IArchive, IRarArchiveCommon { } public interface IRarAsyncArchive : IAsyncArchive, IRarArchiveCommon { } -public class RarArchive : AbstractArchive, IRarArchive +public partial class RarArchive : AbstractArchive, IRarArchive { private bool _disposed; internal Lazy UnpackV2017 { get; } = new(() => new Compressors.Rar.UnpackV2017.Unpack()); internal Lazy UnpackV1 { get; } = new(() => new Compressors.Rar.UnpackV1.Unpack()); - /// - /// Constructor with a SourceStream able to handle FileInfo and Streams. - /// - /// private RarArchive(SourceStream sourceStream) : base(ArchiveType.Rar, sourceStream) { } @@ -57,10 +53,10 @@ public class RarArchive : AbstractArchive, IRarArchi protected override IEnumerable LoadVolumes(SourceStream sourceStream) { - sourceStream.LoadAllParts(); //request all streams + sourceStream.LoadAllParts(); var streams = sourceStream.Streams.ToArray(); var i = 0; - if (streams.Length > 1 && IsRarFile(streams[1], ReaderOptions)) //test part 2 - true = multipart not split + if (streams.Length > 1 && IsRarFile(streams[1], ReaderOptions)) { sourceStream.IsVolumes = true; streams[1].Position = 0; @@ -73,7 +69,6 @@ public class RarArchive : AbstractArchive, IRarArchi )); } - //split mode or single file return new StreamRarArchiveVolume(sourceStream, ReaderOptions, i++).AsEnumerable(); } @@ -106,187 +101,4 @@ public class RarArchive : AbstractArchive, IRarArchi public virtual int MinVersion => Volumes.First().MinVersion; public virtual int MaxVersion => Volumes.First().MaxVersion; - - #region Creation - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - /// - public static IRarArchive Open(string filePath, ReaderOptions? options = null) - { - filePath.NotNullOrEmpty(nameof(filePath)); - var fileInfo = new FileInfo(filePath); - return new RarArchive( - new SourceStream( - fileInfo, - i => RarArchiveVolumeFactory.GetFilePart(i, fileInfo), - options ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - /// - public static IRarArchive Open(FileInfo fileInfo, ReaderOptions? options = null) - { - fileInfo.NotNull(nameof(fileInfo)); - return new RarArchive( - new SourceStream( - fileInfo, - i => RarArchiveVolumeFactory.GetFilePart(i, fileInfo), - options ?? new ReaderOptions() - ) - ); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - public static IRarArchive Open(Stream stream, ReaderOptions? options = null) - { - stream.NotNull(nameof(stream)); - - if (stream is not { CanSeek: true }) - { - throw new ArgumentException("Stream must be seekable", nameof(stream)); - } - - return new RarArchive(new SourceStream(stream, _ => null, options ?? new ReaderOptions())); - } - - /// - /// Constructor with all file parts passed in - /// - /// - /// - public static IRarArchive Open( - IEnumerable fileInfos, - ReaderOptions? readerOptions = null - ) - { - fileInfos.NotNull(nameof(fileInfos)); - var files = fileInfos.ToArray(); - return new RarArchive( - new SourceStream( - files[0], - i => i < files.Length ? files[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all stream parts passed in - /// - /// - /// - public static IRarArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) - { - streams.NotNull(nameof(streams)); - var strms = streams.ToArray(); - return new RarArchive( - new SourceStream( - strms[0], - i => i < strms.Length ? strms[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Opens a RarArchive asynchronously from a stream. - /// - /// - /// - /// - public static IRarAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IRarAsyncArchive)Open(stream, readerOptions); - } - - /// - /// Opens a RarArchive asynchronously from a FileInfo. - /// - /// - /// - /// - public static IRarAsyncArchive OpenAsync( - FileInfo fileInfo, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IRarAsyncArchive)Open(fileInfo, readerOptions); - } - - /// - /// Opens a RarArchive asynchronously from multiple streams. - /// - /// - /// - /// - public static IRarAsyncArchive OpenAsync( - IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IRarAsyncArchive)Open(streams, readerOptions); - } - - /// - /// Opens a RarArchive asynchronously from multiple FileInfo objects. - /// - /// - /// - /// - public static IRarAsyncArchive OpenAsync( - IReadOnlyList fileInfos, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IRarAsyncArchive)Open(fileInfos, readerOptions); - } - - public static bool IsRarFile(string filePath) => IsRarFile(new FileInfo(filePath)); - - public static bool IsRarFile(FileInfo fileInfo) - { - if (!fileInfo.Exists) - { - return false; - } - using Stream stream = fileInfo.OpenRead(); - return IsRarFile(stream); - } - - public static bool IsRarFile(Stream stream, ReaderOptions? options = null) - { - try - { - MarkHeader.Read(stream, true, false); - return true; - } - catch - { - return false; - } - } - - #endregion } diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs new file mode 100644 index 00000000..9aaf1ae5 --- /dev/null +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs @@ -0,0 +1,151 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Common; +using SharpCompress.Common.SevenZip; +using SharpCompress.Compressors.LZMA.Utilites; +using SharpCompress.IO; +using SharpCompress.Readers; + +namespace SharpCompress.Archives.SevenZip; + +public partial class SevenZipArchive +{ + public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty("filePath"); + return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); + } + + public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull("fileInfo"); + return new SevenZipArchive( + new SourceStream( + fileInfo, + i => ArchiveVolumeFactory.GetFilePart(i, fileInfo), + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IArchive Open( + IEnumerable fileInfos, + ReaderOptions? readerOptions = null + ) + { + fileInfos.NotNull(nameof(fileInfos)); + var files = fileInfos.ToArray(); + return new SevenZipArchive( + new SourceStream( + files[0], + i => i < files.Length ? files[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + { + streams.NotNull(nameof(streams)); + var strms = streams.ToArray(); + return new SevenZipArchive( + new SourceStream( + strms[0], + i => i < strms.Length ? strms[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + stream.NotNull("stream"); + + if (stream is not { CanSeek: true }) + { + throw new ArgumentException("Stream must be seekable", nameof(stream)); + } + + return new SevenZipArchive( + new SourceStream(stream, _ => null, readerOptions ?? new ReaderOptions()) + ); + } + + public static IAsyncArchive OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(stream, readerOptions); + } + + public static IAsyncArchive OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfo, readerOptions); + } + + public static IAsyncArchive OpenAsync( + IReadOnlyList streams, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(streams, readerOptions); + } + + public static IAsyncArchive OpenAsync( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncArchive)Open(fileInfos, readerOptions); + } + + public static bool IsSevenZipFile(string filePath) => IsSevenZipFile(new FileInfo(filePath)); + + public static bool IsSevenZipFile(FileInfo fileInfo) + { + if (!fileInfo.Exists) + { + return false; + } + using Stream stream = fileInfo.OpenRead(); + return IsSevenZipFile(stream); + } + + public static bool IsSevenZipFile(Stream stream) + { + try + { + return SignatureMatch(stream); + } + catch + { + return false; + } + } + + private static ReadOnlySpan Signature => + new byte[] { (byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C }; + + private static bool SignatureMatch(Stream stream) + { + var reader = new BinaryReader(stream); + ReadOnlySpan signatureBytes = reader.ReadBytes(6); + return signatureBytes.SequenceEqual(Signature); + } +} diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs index 8532e837..e6b511d8 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.cs @@ -12,188 +12,22 @@ using SharpCompress.Readers; namespace SharpCompress.Archives.SevenZip; -public class SevenZipArchive : AbstractArchive +public partial class SevenZipArchive : AbstractArchive { private ArchiveDatabase? _database; - /// - /// Constructor expects a filepath to an existing file. - /// - /// - /// - public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) - { - filePath.NotNullOrEmpty("filePath"); - return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - /// - public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) - { - fileInfo.NotNull("fileInfo"); - return new SevenZipArchive( - new SourceStream( - fileInfo, - i => ArchiveVolumeFactory.GetFilePart(i, fileInfo), - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all file parts passed in - /// - /// - /// - public static IArchive Open( - IEnumerable fileInfos, - ReaderOptions? readerOptions = null - ) - { - fileInfos.NotNull(nameof(fileInfos)); - var files = fileInfos.ToArray(); - return new SevenZipArchive( - new SourceStream( - files[0], - i => i < files.Length ? files[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all stream parts passed in - /// - /// - /// - public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) - { - streams.NotNull(nameof(streams)); - var strms = streams.ToArray(); - return new SevenZipArchive( - new SourceStream( - strms[0], - i => i < strms.Length ? strms[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) - { - stream.NotNull("stream"); - - if (stream is not { CanSeek: true }) - { - throw new ArgumentException("Stream must be seekable", nameof(stream)); - } - - return new SevenZipArchive( - new SourceStream(stream, _ => null, readerOptions ?? new ReaderOptions()) - ); - } - - /// - /// Opens a SevenZipArchive asynchronously from a stream. - /// - /// - /// - /// - public static IAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(stream, readerOptions); - } - - /// - /// Opens a SevenZipArchive asynchronously from a FileInfo. - /// - /// - /// - /// - public static IAsyncArchive OpenAsync( - FileInfo fileInfo, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); - } - - /// - /// Opens a SevenZipArchive asynchronously from multiple streams. - /// - /// - /// - /// - public static IAsyncArchive OpenAsync( - IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(streams, readerOptions); - } - - /// - /// Opens a SevenZipArchive asynchronously from multiple FileInfo objects. - /// - /// - /// - /// - public static IAsyncArchive OpenAsync( - IReadOnlyList fileInfos, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); - } - - /// - /// Constructor with a SourceStream able to handle FileInfo and Streams. - /// - /// private SevenZipArchive(SourceStream sourceStream) : base(ArchiveType.SevenZip, sourceStream) { } - protected override IEnumerable LoadVolumes(SourceStream sourceStream) - { - sourceStream.NotNull("SourceStream is null").LoadAllParts(); //request all streams - return new SevenZipVolume(sourceStream, ReaderOptions, 0).AsEnumerable(); //simple single volume or split, multivolume not supported - } - - public static bool IsSevenZipFile(string filePath) => IsSevenZipFile(new FileInfo(filePath)); - - public static bool IsSevenZipFile(FileInfo fileInfo) - { - if (!fileInfo.Exists) - { - return false; - } - using Stream stream = fileInfo.OpenRead(); - return IsSevenZipFile(stream); - } - internal SevenZipArchive() : base(ArchiveType.SevenZip) { } + protected override IEnumerable LoadVolumes(SourceStream sourceStream) + { + sourceStream.NotNull("SourceStream is null").LoadAllParts(); + return new SevenZipVolume(sourceStream, ReaderOptions, 0).AsEnumerable(); + } + protected override IEnumerable LoadEntries( IEnumerable volumes ) @@ -219,7 +53,7 @@ public class SevenZipArchive : AbstractArchive Signature => - new byte[] { (byte)'7', (byte)'z', 0xBC, 0xAF, 0x27, 0x1C }; - - private static bool SignatureMatch(Stream stream) - { - var reader = new BinaryReader(stream); - ReadOnlySpan signatureBytes = reader.ReadBytes(6); - return signatureBytes.SequenceEqual(Signature); - } - protected override IReader CreateReaderForSolidExtraction() => new SevenZipReader(ReaderOptions, this); @@ -295,9 +107,6 @@ public class SevenZipArchive : AbstractArchive !x.IsDirectory)) { _currentEntry = entry; @@ -307,13 +116,6 @@ public class SevenZipArchive : AbstractArchive - /// WORKAROUND: Forces async operations to use synchronous equivalents. - /// This is necessary because the LZMA decoder has bugs in its async implementation - /// that cause state corruption (IndexOutOfRangeException, DataErrorException). - /// - /// The proper fix would be to repair the LZMA decoder's async methods - /// (LzmaStream.ReadAsync, Decoder.CodeAsync, OutWindow async operations), - /// but that requires deep changes to the decoder state machine. - /// private sealed class SyncOnlyStream : Stream { private readonly Stream _baseStream; @@ -361,7 +154,6 @@ public class SevenZipArchive : AbstractArchive _baseStream.Write(buffer, offset, count); - // Force async operations to use sync equivalents to avoid LZMA decoder bugs public override Task ReadAsync( byte[] buffer, int offset, diff --git a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs new file mode 100644 index 00000000..528c0954 --- /dev/null +++ b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs @@ -0,0 +1,162 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Common; +using SharpCompress.Common.Tar; +using SharpCompress.Common.Tar.Headers; +using SharpCompress.IO; +using SharpCompress.Readers; +using SharpCompress.Writers; +using SharpCompress.Writers.Tar; + +namespace SharpCompress.Archives.Tar; + +public partial class TarArchive +{ + public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); + } + + public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return new TarArchive( + new SourceStream( + fileInfo, + i => ArchiveVolumeFactory.GetFilePart(i, fileInfo), + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IWritableArchive Open( + IEnumerable fileInfos, + ReaderOptions? readerOptions = null + ) + { + fileInfos.NotNull(nameof(fileInfos)); + var files = fileInfos.ToArray(); + return new TarArchive( + new SourceStream( + files[0], + i => i < files.Length ? files[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IWritableArchive Open( + IEnumerable streams, + ReaderOptions? readerOptions = null + ) + { + streams.NotNull(nameof(streams)); + var strms = streams.ToArray(); + return new TarArchive( + new SourceStream( + strms[0], + i => i < strms.Length ? strms[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + stream.NotNull(nameof(stream)); + + if (stream is not { CanSeek: true }) + { + throw new ArgumentException("Stream must be seekable", nameof(stream)); + } + + return new TarArchive( + new SourceStream(stream, i => null, readerOptions ?? new ReaderOptions()) + ); + } + + public static IWritableAsyncArchive OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(stream, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + string file, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(new FileInfo(file), readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(fileInfo, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + IReadOnlyList streams, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(streams, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(fileInfos, readerOptions); + } + + public static bool IsTarFile(string filePath) => IsTarFile(new FileInfo(filePath)); + + public static bool IsTarFile(FileInfo fileInfo) + { + if (!fileInfo.Exists) + { + return false; + } + using Stream stream = fileInfo.OpenRead(); + return IsTarFile(stream); + } + + public static bool IsTarFile(Stream stream) + { + try + { + var tarHeader = new TarHeader(new ArchiveEncoding()); + var readSucceeded = tarHeader.Read(new BinaryReader(stream)); + var isEmptyArchive = + tarHeader.Name?.Length == 0 + && tarHeader.Size == 0 + && Enum.IsDefined(typeof(EntryType), tarHeader.EntryType); + return readSucceeded || isEmptyArchive; + } + catch { } + return false; + } + + public static TarArchive Create() => new(); +} diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index f1c0254f..af06e47d 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -15,209 +15,14 @@ using SharpCompress.Writers.Tar; namespace SharpCompress.Archives.Tar; -public class TarArchive : AbstractWritableArchive +public partial class TarArchive : AbstractWritableArchive { - /// - /// Constructor expects a filepath to an existing file. - /// - /// - /// - public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) - { - filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - /// - public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) - { - fileInfo.NotNull(nameof(fileInfo)); - return new TarArchive( - new SourceStream( - fileInfo, - i => ArchiveVolumeFactory.GetFilePart(i, fileInfo), - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all file parts passed in - /// - /// - /// - public static IWritableArchive Open( - IEnumerable fileInfos, - ReaderOptions? readerOptions = null - ) - { - fileInfos.NotNull(nameof(fileInfos)); - var files = fileInfos.ToArray(); - return new TarArchive( - new SourceStream( - files[0], - i => i < files.Length ? files[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all stream parts passed in - /// - /// - /// - public static IWritableArchive Open( - IEnumerable streams, - ReaderOptions? readerOptions = null - ) - { - streams.NotNull(nameof(streams)); - var strms = streams.ToArray(); - return new TarArchive( - new SourceStream( - strms[0], - i => i < strms.Length ? strms[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) - { - stream.NotNull(nameof(stream)); - - if (stream is not { CanSeek: true }) - { - throw new ArgumentException("Stream must be seekable", nameof(stream)); - } - - return new TarArchive( - new SourceStream(stream, i => null, readerOptions ?? new ReaderOptions()) - ); - } - - /// - /// Opens a TarArchive asynchronously from a stream. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(stream, readerOptions); - } - - public static IWritableAsyncArchive OpenAsync( - string file, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(new FileInfo(file), readerOptions); - } - - /// - /// Opens a TarArchive asynchronously from a FileInfo. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - FileInfo fileInfo, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfo, readerOptions); - } - - /// - /// Opens a TarArchive asynchronously from multiple streams. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(streams, readerOptions); - } - - /// - /// Opens a TarArchive asynchronously from multiple FileInfo objects. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - IReadOnlyList fileInfos, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfos, readerOptions); - } - - public static bool IsTarFile(string filePath) => IsTarFile(new FileInfo(filePath)); - - public static bool IsTarFile(FileInfo fileInfo) - { - if (!fileInfo.Exists) - { - return false; - } - using Stream stream = fileInfo.OpenRead(); - return IsTarFile(stream); - } - - public static bool IsTarFile(Stream stream) - { - try - { - var tarHeader = new TarHeader(new ArchiveEncoding()); - var readSucceeded = tarHeader.Read(new BinaryReader(stream)); - var isEmptyArchive = - tarHeader.Name?.Length == 0 - && tarHeader.Size == 0 - && Enum.IsDefined(typeof(EntryType), tarHeader.EntryType); - return readSucceeded || isEmptyArchive; - } - catch { } - return false; - } - protected override IEnumerable LoadVolumes(SourceStream sourceStream) { - sourceStream.NotNull("SourceStream is null").LoadAllParts(); //request all streams - return new TarVolume(sourceStream, ReaderOptions, 1).AsEnumerable(); //simple single volume or split, multivolume not supported + sourceStream.NotNull("SourceStream is null").LoadAllParts(); + return new TarVolume(sourceStream, ReaderOptions, 1).AsEnumerable(); } - /// - /// Constructor with a SourceStream able to handle FileInfo and Streams. - /// - /// private TarArchive(SourceStream sourceStream) : base(ArchiveType.Tar, sourceStream) { } @@ -282,8 +87,6 @@ public class TarArchive : AbstractWritableArchive } } - public static TarArchive Create() => new(); - protected override TarArchiveEntry CreateEntryInternal( string filePath, Stream source, diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs new file mode 100644 index 00000000..9fb47187 --- /dev/null +++ b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs @@ -0,0 +1,317 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Common; +using SharpCompress.Common.Zip; +using SharpCompress.Common.Zip.Headers; +using SharpCompress.IO; +using SharpCompress.Readers; +using SharpCompress.Writers; +using SharpCompress.Writers.Zip; + +namespace SharpCompress.Archives.Zip; + +public partial class ZipArchive +{ + public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); + } + + public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return new ZipArchive( + new SourceStream( + fileInfo, + i => ZipArchiveVolumeFactory.GetFilePart(i, fileInfo), + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IWritableArchive Open( + IEnumerable fileInfos, + ReaderOptions? readerOptions = null + ) + { + fileInfos.NotNull(nameof(fileInfos)); + var files = fileInfos.ToArray(); + return new ZipArchive( + new SourceStream( + files[0], + i => i < files.Length ? files[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IWritableArchive Open( + IEnumerable streams, + ReaderOptions? readerOptions = null + ) + { + streams.NotNull(nameof(streams)); + var strms = streams.ToArray(); + return new ZipArchive( + new SourceStream( + strms[0], + i => i < strms.Length ? strms[i] : null, + readerOptions ?? new ReaderOptions() + ) + ); + } + + public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + stream.NotNull(nameof(stream)); + + if (stream is not { CanSeek: true }) + { + throw new ArgumentException("Stream must be seekable", nameof(stream)); + } + + return new ZipArchive( + new SourceStream(stream, i => null, readerOptions ?? new ReaderOptions()) + ); + } + + public static IWritableAsyncArchive OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(path, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(stream, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(fileInfo, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + IReadOnlyList streams, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(streams, readerOptions); + } + + public static IWritableAsyncArchive OpenAsync( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IWritableAsyncArchive)Open(fileInfos, readerOptions); + } + + public static bool IsZipFile( + string filePath, + string? password = null, + int bufferSize = ReaderOptions.DefaultBufferSize + ) => IsZipFile(new FileInfo(filePath), password, bufferSize); + + public static bool IsZipFile( + FileInfo fileInfo, + string? password = null, + int bufferSize = ReaderOptions.DefaultBufferSize + ) + { + if (!fileInfo.Exists) + { + return false; + } + using Stream stream = fileInfo.OpenRead(); + return IsZipFile(stream, password, bufferSize); + } + + public static bool IsZipFile( + Stream stream, + string? password = null, + int bufferSize = ReaderOptions.DefaultBufferSize + ) + { + var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); + try + { + if (stream is not SharpCompressStream) + { + stream = new SharpCompressStream(stream, bufferSize: bufferSize); + } + + var header = headerFactory + .ReadStreamHeader(stream) + .FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split); + if (header is null) + { + return false; + } + return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType); + } + catch (CryptographicException) + { + return true; + } + catch + { + return false; + } + } + + public static bool IsZipMulti( + Stream stream, + string? password = null, + int bufferSize = ReaderOptions.DefaultBufferSize + ) + { + var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); + try + { + if (stream is not SharpCompressStream) + { + stream = new SharpCompressStream(stream, bufferSize: bufferSize); + } + + var header = headerFactory + .ReadStreamHeader(stream) + .FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split); + if (header is null) + { + if (stream.CanSeek) + { + var z = new SeekableZipHeaderFactory(password, new ArchiveEncoding()); + var x = z.ReadSeekableHeader(stream, useSync: true).FirstOrDefault(); + return x?.ZipHeaderType == ZipHeaderType.DirectoryEntry; + } + else + { + return false; + } + } + return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType); + } + catch (CryptographicException) + { + return true; + } + catch + { + return false; + } + } + + public static async ValueTask IsZipFileAsync( + Stream stream, + string? password = null, + int bufferSize = ReaderOptions.DefaultBufferSize, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); + try + { + if (stream is not SharpCompressStream) + { + stream = new SharpCompressStream(stream, bufferSize: bufferSize); + } + + var header = await headerFactory + .ReadStreamHeaderAsync(stream) + .WhereAsync(x => x.ZipHeaderType != ZipHeaderType.Split) + .FirstOrDefaultAsync(); + if (header is null) + { + return false; + } + return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType); + } + catch (CryptographicException) + { + return true; + } + catch + { + return false; + } + } + + public static ZipArchive Create() => new(); + + public static async ValueTask IsZipMultiAsync( + Stream stream, + string? password = null, + int bufferSize = ReaderOptions.DefaultBufferSize, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); + try + { + if (stream is not SharpCompressStream) + { + stream = new SharpCompressStream(stream, bufferSize: bufferSize); + } + + var header = headerFactory + .ReadStreamHeader(stream) + .FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split); + if (header is null) + { + if (stream.CanSeek) + { + var z = new SeekableZipHeaderFactory(password, new ArchiveEncoding()); + ZipHeader? x = null; + await foreach ( + var h in z.ReadSeekableHeaderAsync(stream) + .WithCancellation(cancellationToken) + ) + { + x = h; + break; + } + return x?.ZipHeaderType == ZipHeaderType.DirectoryEntry; + } + else + { + return false; + } + } + return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType); + } + catch (CryptographicException) + { + return true; + } + catch + { + return false; + } + } +} diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index 159cc6f4..5f1c84d8 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -16,21 +16,12 @@ using SharpCompress.Writers.Zip; namespace SharpCompress.Archives.Zip; -public class ZipArchive : AbstractWritableArchive +public partial class ZipArchive : AbstractWritableArchive { private readonly SeekableZipHeaderFactory? headerFactory; - /// - /// Gets or sets the compression level applied to files added to the archive, - /// if the compression method is set to deflate - /// public CompressionLevel DeflateCompressionLevel { get; set; } - /// - /// Constructor with a SourceStream able to handle FileInfo and Streams. - /// - /// - /// internal ZipArchive(SourceStream sourceStream) : base(ArchiveType.Zip, sourceStream) => headerFactory = new SeekableZipHeaderFactory( @@ -38,384 +29,36 @@ public class ZipArchive : AbstractWritableArchive sourceStream.ReaderOptions.ArchiveEncoding ); - /// - /// Constructor expects a filepath to an existing file. - /// - /// - /// - public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) - { - filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); - } - - /// - /// Constructor with a FileInfo object to an existing file. - /// - /// - /// - public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) - { - fileInfo.NotNull(nameof(fileInfo)); - return new ZipArchive( - new SourceStream( - fileInfo, - i => ZipArchiveVolumeFactory.GetFilePart(i, fileInfo), - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all file parts passed in - /// - /// - /// - public static IWritableArchive Open( - IEnumerable fileInfos, - ReaderOptions? readerOptions = null - ) - { - fileInfos.NotNull(nameof(fileInfos)); - var files = fileInfos.ToArray(); - return new ZipArchive( - new SourceStream( - files[0], - i => i < files.Length ? files[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Constructor with all stream parts passed in - /// - /// - /// - public static IWritableArchive Open( - IEnumerable streams, - ReaderOptions? readerOptions = null - ) - { - streams.NotNull(nameof(streams)); - var strms = streams.ToArray(); - return new ZipArchive( - new SourceStream( - strms[0], - i => i < strms.Length ? strms[i] : null, - readerOptions ?? new ReaderOptions() - ) - ); - } - - /// - /// Takes a seekable Stream as a source - /// - /// - /// - public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) - { - stream.NotNull(nameof(stream)); - - if (stream is not { CanSeek: true }) - { - throw new ArgumentException("Stream must be seekable", nameof(stream)); - } - - return new ZipArchive( - new SourceStream(stream, i => null, readerOptions ?? new ReaderOptions()) - ); - } - - public static IWritableAsyncArchive OpenAsync( - string path, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(path, readerOptions); - } - - /// - /// Opens a ZipArchive asynchronously from a stream. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(stream, readerOptions); - } - - /// - /// Opens a ZipArchive asynchronously from a FileInfo. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - FileInfo fileInfo, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfo, readerOptions); - } - - /// - /// Opens a ZipArchive asynchronously from multiple streams. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - IReadOnlyList streams, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(streams, readerOptions); - } - - /// - /// Opens a ZipArchive asynchronously from multiple FileInfo objects. - /// - /// - /// - /// - public static IWritableAsyncArchive OpenAsync( - IReadOnlyList fileInfos, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfos, readerOptions); - } - - public static bool IsZipFile( - string filePath, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) => IsZipFile(new FileInfo(filePath), password, bufferSize); - - public static bool IsZipFile( - FileInfo fileInfo, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) - { - if (!fileInfo.Exists) - { - return false; - } - using Stream stream = fileInfo.OpenRead(); - return IsZipFile(stream, password, bufferSize); - } - - public static bool IsZipFile( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) - { - var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); - try - { - if (stream is not SharpCompressStream) - { - stream = new SharpCompressStream(stream, bufferSize: bufferSize); - } - - var header = headerFactory - .ReadStreamHeader(stream) - .FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split); - if (header is null) - { - return false; - } - return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType); - } - catch (CryptographicException) - { - return true; - } - catch - { - return false; - } - } - - public static bool IsZipMulti( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) - { - var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); - try - { - if (stream is not SharpCompressStream) - { - stream = new SharpCompressStream(stream, bufferSize: bufferSize); - } - - var header = headerFactory - .ReadStreamHeader(stream) - .FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split); - if (header is null) - { - if (stream.CanSeek) //could be multipart. Test for central directory - might not be z64 safe - { - var z = new SeekableZipHeaderFactory(password, new ArchiveEncoding()); - var x = z.ReadSeekableHeader(stream, useSync: true).FirstOrDefault(); - return x?.ZipHeaderType == ZipHeaderType.DirectoryEntry; - } - else - { - return false; - } - } - return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType); - } - catch (CryptographicException) - { - return true; - } - catch - { - return false; - } - } - - public static async ValueTask IsZipFileAsync( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); - try - { - if (stream is not SharpCompressStream) - { - stream = new SharpCompressStream(stream, bufferSize: bufferSize); - } - - var header = await headerFactory - .ReadStreamHeaderAsync(stream) - .WhereAsync(x => x.ZipHeaderType != ZipHeaderType.Split) - .FirstOrDefaultAsync(); - if (header is null) - { - return false; - } - return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType); - } - catch (CryptographicException) - { - return true; - } - catch - { - return false; - } - } - - public static async ValueTask IsZipMultiAsync( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - var headerFactory = new StreamingZipHeaderFactory(password, new ArchiveEncoding(), null); - try - { - if (stream is not SharpCompressStream) - { - stream = new SharpCompressStream(stream, bufferSize: bufferSize); - } - - var header = headerFactory - .ReadStreamHeader(stream) - .FirstOrDefault(x => x.ZipHeaderType != ZipHeaderType.Split); - if (header is null) - { - if (stream.CanSeek) //could be multipart. Test for central directory - might not be z64 safe - { - var z = new SeekableZipHeaderFactory(password, new ArchiveEncoding()); - ZipHeader? x = null; - await foreach ( - var h in z.ReadSeekableHeaderAsync(stream) - .WithCancellation(cancellationToken) - ) - { - x = h; - break; - } - return x?.ZipHeaderType == ZipHeaderType.DirectoryEntry; - } - else - { - return false; - } - } - return Enum.IsDefined(typeof(ZipHeaderType), header.ZipHeaderType); - } - catch (CryptographicException) - { - return true; - } - catch - { - return false; - } - } + internal ZipArchive() + : base(ArchiveType.Zip) { } protected override IEnumerable LoadVolumes(SourceStream stream) { - stream.LoadAllParts(); //request all streams + stream.LoadAllParts(); stream.Position = 0; var streams = stream.Streams.ToList(); var idx = 0; - if (streams.Count() > 1) //test part 2 - true = multipart not split + if (streams.Count() > 1) { - streams[1].Position += 4; //skip the POST_DATA_DESCRIPTOR to prevent an exception + streams[1].Position += 4; var isZip = IsZipFile(streams[1], ReaderOptions.Password, ReaderOptions.BufferSize); streams[1].Position -= 4; if (isZip) { stream.IsVolumes = true; - var tmp = streams[0]; //arcs as zip, z01 ... swap the zip the end + var tmp = streams[0]; streams.RemoveAt(0); streams.Add(tmp); - //streams[0].Position = 4; //skip the POST_DATA_DESCRIPTOR to prevent an exception return streams.Select(a => new ZipVolume(a, ReaderOptions, idx++)); } } - //split mode or single file return new ZipVolume(stream, ReaderOptions, idx++).AsEnumerable(); } - internal ZipArchive() - : base(ArchiveType.Zip) { } - protected override IEnumerable LoadEntries(IEnumerable volumes) { var vols = volumes.ToArray(); @@ -597,8 +240,6 @@ public class ZipArchive : AbstractWritableArchive DateTime? modified ) => new ZipWritableArchiveEntry(this, directoryPath, modified); - public static ZipArchive Create() => new(); - protected override IReader CreateReaderForSolidExtraction() { var stream = Volumes.Single().Stream; From 9a4e864f5e2d6655f4347c5b34b80ef3cc65e1cd Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 13:49:35 +0000 Subject: [PATCH 10/36] fix usage of ArchiveFactory --- .../Archives/ArchiveFactory.Open.cs | 347 ------------------ src/SharpCompress/Archives/ArchiveFactory.cs | 346 ++++++++++++++++- 2 files changed, 345 insertions(+), 348 deletions(-) delete mode 100644 src/SharpCompress/Archives/ArchiveFactory.Open.cs diff --git a/src/SharpCompress/Archives/ArchiveFactory.Open.cs b/src/SharpCompress/Archives/ArchiveFactory.Open.cs deleted file mode 100644 index 25a104ec..00000000 --- a/src/SharpCompress/Archives/ArchiveFactory.Open.cs +++ /dev/null @@ -1,347 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using SharpCompress.Common; -using SharpCompress.Factories; -using SharpCompress.IO; -using SharpCompress.Readers; - -namespace SharpCompress.Archives; - -public static partial class ArchiveFactory -{ - public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) - { - readerOptions ??= new ReaderOptions(); - stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); - return FindFactory(stream).Open(stream, readerOptions); - } - - public static async ValueTask OpenAsync( - Stream stream, - ReaderOptions? readerOptions = null, - CancellationToken cancellationToken = default - ) - { - readerOptions ??= new ReaderOptions(); - stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); - var factory = await FindFactoryAsync(stream, cancellationToken); - return factory.OpenAsync(stream, readerOptions); - } - - public static IWritableArchive Create(ArchiveType type) - { - var factory = Factory - .Factories.OfType() - .FirstOrDefault(item => item.KnownArchiveType == type); - - if (factory != null) - { - return factory.CreateWriteableArchive(); - } - - throw new NotSupportedException("Cannot create Archives of type: " + type); - } - - public static IArchive Open(string filePath, ReaderOptions? options = null) - { - filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), options); - } - - public static ValueTask OpenAsync( - string filePath, - ReaderOptions? options = null, - CancellationToken cancellationToken = default - ) - { - filePath.NotNullOrEmpty(nameof(filePath)); - return OpenAsync(new FileInfo(filePath), options, cancellationToken); - } - - public static IArchive Open(FileInfo fileInfo, ReaderOptions? options = null) - { - options ??= new ReaderOptions { LeaveStreamOpen = false }; - - return FindFactory(fileInfo).Open(fileInfo, options); - } - - public static async ValueTask OpenAsync( - FileInfo fileInfo, - ReaderOptions? options = null, - CancellationToken cancellationToken = default - ) - { - options ??= new ReaderOptions { LeaveStreamOpen = false }; - - var factory = await FindFactoryAsync(fileInfo, cancellationToken); - return factory.OpenAsync(fileInfo, options, cancellationToken); - } - - public static IArchive Open(IEnumerable fileInfos, ReaderOptions? options = null) - { - fileInfos.NotNull(nameof(fileInfos)); - var filesArray = fileInfos.ToArray(); - if (filesArray.Length == 0) - { - throw new InvalidOperationException("No files to open"); - } - - var fileInfo = filesArray[0]; - if (filesArray.Length == 1) - { - return Open(fileInfo, options); - } - - fileInfo.NotNull(nameof(fileInfo)); - options ??= new ReaderOptions { LeaveStreamOpen = false }; - - return FindFactory(fileInfo).Open(filesArray, options); - } - - public static async ValueTask OpenAsync( - IEnumerable fileInfos, - ReaderOptions? options = null, - CancellationToken cancellationToken = default - ) - { - fileInfos.NotNull(nameof(fileInfos)); - var filesArray = fileInfos.ToArray(); - if (filesArray.Length == 0) - { - throw new InvalidOperationException("No files to open"); - } - - var fileInfo = filesArray[0]; - if (filesArray.Length == 1) - { - return await OpenAsync(fileInfo, options, cancellationToken); - } - - fileInfo.NotNull(nameof(fileInfo)); - options ??= new ReaderOptions { LeaveStreamOpen = false }; - - var factory = await FindFactoryAsync(fileInfo, cancellationToken); - return factory.OpenAsync(filesArray, options, cancellationToken); - } - - public static IArchive Open(IEnumerable streams, ReaderOptions? options = null) - { - streams.NotNull(nameof(streams)); - var streamsArray = streams.ToArray(); - if (streamsArray.Length == 0) - { - throw new InvalidOperationException("No streams"); - } - - var firstStream = streamsArray[0]; - if (streamsArray.Length == 1) - { - return Open(firstStream, options); - } - - firstStream.NotNull(nameof(firstStream)); - options ??= new ReaderOptions(); - - return FindFactory(firstStream).Open(streamsArray, options); - } - - public static async ValueTask OpenAsync( - IEnumerable streams, - ReaderOptions? options = null, - CancellationToken cancellationToken = default - ) - { - cancellationToken.ThrowIfCancellationRequested(); - streams.NotNull(nameof(streams)); - var streamsArray = streams.ToArray(); - if (streamsArray.Length == 0) - { - throw new InvalidOperationException("No streams"); - } - - var firstStream = streamsArray[0]; - if (streamsArray.Length == 1) - { - return await OpenAsync(firstStream, options, cancellationToken); - } - - firstStream.NotNull(nameof(firstStream)); - options ??= new ReaderOptions(); - - var factory = FindFactory(firstStream); - return factory.OpenAsync(streamsArray, options); - } - - public static void WriteToDirectory( - string sourceArchive, - string destinationDirectory, - ExtractionOptions? options = null - ) - { - using var archive = Open(sourceArchive); - archive.WriteToDirectory(destinationDirectory, options); - } - - private static T FindFactory(FileInfo finfo) - where T : IFactory - { - finfo.NotNull(nameof(finfo)); - using Stream stream = finfo.OpenRead(); - return FindFactory(stream); - } - - private static T FindFactory(Stream stream) - where T : IFactory - { - stream.NotNull(nameof(stream)); - if (!stream.CanRead || !stream.CanSeek) - { - throw new ArgumentException("Stream should be readable and seekable"); - } - - var factories = Factory.Factories.OfType(); - - var startPosition = stream.Position; - - foreach (var factory in factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - if (factory.IsArchive(stream)) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - return factory; - } - } - - var extensions = string.Join(", ", factories.Select(item => item.Name)); - - throw new InvalidOperationException( - $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" - ); - } - - private static async ValueTask FindFactoryAsync( - FileInfo finfo, - CancellationToken cancellationToken - ) - where T : IFactory - { - finfo.NotNull(nameof(finfo)); - using Stream stream = finfo.OpenRead(); - return await FindFactoryAsync(stream, cancellationToken); - } - - private static async ValueTask FindFactoryAsync( - Stream stream, - CancellationToken cancellationToken - ) - where T : IFactory - { - stream.NotNull(nameof(stream)); - if (!stream.CanRead || !stream.CanSeek) - { - throw new ArgumentException("Stream should be readable and seekable"); - } - - var factories = Factory.Factories.OfType(); - - var startPosition = stream.Position; - - foreach (var factory in factories) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - if (await factory.IsArchiveAsync(stream, cancellationToken: cancellationToken)) - { - stream.Seek(startPosition, SeekOrigin.Begin); - - return factory; - } - } - - var extensions = string.Join(", ", factories.Select(item => item.Name)); - - throw new InvalidOperationException( - $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" - ); - } - - public static bool IsArchive( - string filePath, - out ArchiveType? type, - int bufferSize = ReaderOptions.DefaultBufferSize - ) - { - filePath.NotNullOrEmpty(nameof(filePath)); - using Stream s = File.OpenRead(filePath); - return IsArchive(s, out type, bufferSize); - } - - public static bool IsArchive( - Stream stream, - out ArchiveType? type, - int bufferSize = ReaderOptions.DefaultBufferSize - ) - { - type = null; - stream.NotNull(nameof(stream)); - - if (!stream.CanRead || !stream.CanSeek) - { - throw new ArgumentException("Stream should be readable and seekable"); - } - - var startPosition = stream.Position; - - foreach (var factory in Factory.Factories) - { - var isArchive = factory.IsArchive(stream); - stream.Position = startPosition; - - if (isArchive) - { - type = factory.KnownArchiveType; - return true; - } - } - - return false; - } - - public static IEnumerable GetFileParts(string part1) - { - part1.NotNullOrEmpty(nameof(part1)); - return GetFileParts(new FileInfo(part1)).Select(a => a.FullName); - } - - public static IEnumerable GetFileParts(FileInfo part1) - { - part1.NotNull(nameof(part1)); - yield return part1; - - foreach (var factory in Factory.Factories.OfType()) - { - var i = 1; - var part = factory.GetFilePart(i++, part1); - - if (part != null) - { - yield return part; - while ((part = factory.GetFilePart(i++, part1)) != null) - { - yield return part; - } - - yield break; - } - } - } - - public static IArchiveFactory AutoFactory { get; } = new AutoArchiveFactory(); -} diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index a6073240..8d4b9c27 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -1,3 +1,347 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Common; +using SharpCompress.Factories; +using SharpCompress.IO; +using SharpCompress.Readers; + namespace SharpCompress.Archives; -public static partial class ArchiveFactory { } +public static class ArchiveFactory +{ + public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + { + readerOptions ??= new ReaderOptions(); + stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); + return FindFactory(stream).Open(stream, readerOptions); + } + + public static async ValueTask OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + readerOptions ??= new ReaderOptions(); + stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); + var factory = await FindFactoryAsync(stream, cancellationToken); + return factory.OpenAsync(stream, readerOptions); + } + + public static IWritableArchive Create(ArchiveType type) + { + var factory = Factory + .Factories.OfType() + .FirstOrDefault(item => item.KnownArchiveType == type); + + if (factory != null) + { + return factory.CreateWriteableArchive(); + } + + throw new NotSupportedException("Cannot create Archives of type: " + type); + } + + public static IArchive Open(string filePath, ReaderOptions? options = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), options); + } + + public static ValueTask OpenAsync( + string filePath, + ReaderOptions? options = null, + CancellationToken cancellationToken = default + ) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return OpenAsync(new FileInfo(filePath), options, cancellationToken); + } + + public static IArchive Open(FileInfo fileInfo, ReaderOptions? options = null) + { + options ??= new ReaderOptions { LeaveStreamOpen = false }; + + return FindFactory(fileInfo).Open(fileInfo, options); + } + + public static async ValueTask OpenAsync( + FileInfo fileInfo, + ReaderOptions? options = null, + CancellationToken cancellationToken = default + ) + { + options ??= new ReaderOptions { LeaveStreamOpen = false }; + + var factory = await FindFactoryAsync(fileInfo, cancellationToken); + return factory.OpenAsync(fileInfo, options, cancellationToken); + } + + public static IArchive Open(IEnumerable fileInfos, ReaderOptions? options = null) + { + fileInfos.NotNull(nameof(fileInfos)); + var filesArray = fileInfos.ToArray(); + if (filesArray.Length == 0) + { + throw new InvalidOperationException("No files to open"); + } + + var fileInfo = filesArray[0]; + if (filesArray.Length == 1) + { + return Open(fileInfo, options); + } + + fileInfo.NotNull(nameof(fileInfo)); + options ??= new ReaderOptions { LeaveStreamOpen = false }; + + return FindFactory(fileInfo).Open(filesArray, options); + } + + public static async ValueTask OpenAsync( + IEnumerable fileInfos, + ReaderOptions? options = null, + CancellationToken cancellationToken = default + ) + { + fileInfos.NotNull(nameof(fileInfos)); + var filesArray = fileInfos.ToArray(); + if (filesArray.Length == 0) + { + throw new InvalidOperationException("No files to open"); + } + + var fileInfo = filesArray[0]; + if (filesArray.Length == 1) + { + return await OpenAsync(fileInfo, options, cancellationToken); + } + + fileInfo.NotNull(nameof(fileInfo)); + options ??= new ReaderOptions { LeaveStreamOpen = false }; + + var factory = await FindFactoryAsync(fileInfo, cancellationToken); + return factory.OpenAsync(filesArray, options, cancellationToken); + } + + public static IArchive Open(IEnumerable streams, ReaderOptions? options = null) + { + streams.NotNull(nameof(streams)); + var streamsArray = streams.ToArray(); + if (streamsArray.Length == 0) + { + throw new InvalidOperationException("No streams"); + } + + var firstStream = streamsArray[0]; + if (streamsArray.Length == 1) + { + return Open(firstStream, options); + } + + firstStream.NotNull(nameof(firstStream)); + options ??= new ReaderOptions(); + + return FindFactory(firstStream).Open(streamsArray, options); + } + + public static async ValueTask OpenAsync( + IEnumerable streams, + ReaderOptions? options = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + streams.NotNull(nameof(streams)); + var streamsArray = streams.ToArray(); + if (streamsArray.Length == 0) + { + throw new InvalidOperationException("No streams"); + } + + var firstStream = streamsArray[0]; + if (streamsArray.Length == 1) + { + return await OpenAsync(firstStream, options, cancellationToken); + } + + firstStream.NotNull(nameof(firstStream)); + options ??= new ReaderOptions(); + + var factory = FindFactory(firstStream); + return factory.OpenAsync(streamsArray, options); + } + + public static void WriteToDirectory( + string sourceArchive, + string destinationDirectory, + ExtractionOptions? options = null + ) + { + using var archive = Open(sourceArchive); + archive.WriteToDirectory(destinationDirectory, options); + } + + private static T FindFactory(FileInfo finfo) + where T : IFactory + { + finfo.NotNull(nameof(finfo)); + using Stream stream = finfo.OpenRead(); + return FindFactory(stream); + } + + private static T FindFactory(Stream stream) + where T : IFactory + { + stream.NotNull(nameof(stream)); + if (!stream.CanRead || !stream.CanSeek) + { + throw new ArgumentException("Stream should be readable and seekable"); + } + + var factories = Factory.Factories.OfType(); + + var startPosition = stream.Position; + + foreach (var factory in factories) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + if (factory.IsArchive(stream)) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + return factory; + } + } + + var extensions = string.Join(", ", factories.Select(item => item.Name)); + + throw new InvalidOperationException( + $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" + ); + } + + private static async ValueTask FindFactoryAsync( + FileInfo finfo, + CancellationToken cancellationToken + ) + where T : IFactory + { + finfo.NotNull(nameof(finfo)); + using Stream stream = finfo.OpenRead(); + return await FindFactoryAsync(stream, cancellationToken); + } + + private static async ValueTask FindFactoryAsync( + Stream stream, + CancellationToken cancellationToken + ) + where T : IFactory + { + stream.NotNull(nameof(stream)); + if (!stream.CanRead || !stream.CanSeek) + { + throw new ArgumentException("Stream should be readable and seekable"); + } + + var factories = Factory.Factories.OfType(); + + var startPosition = stream.Position; + + foreach (var factory in factories) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + if (await factory.IsArchiveAsync(stream, cancellationToken: cancellationToken)) + { + stream.Seek(startPosition, SeekOrigin.Begin); + + return factory; + } + } + + var extensions = string.Join(", ", factories.Select(item => item.Name)); + + throw new InvalidOperationException( + $"Cannot determine compressed stream type. Supported Archive Formats: {extensions}" + ); + } + + public static bool IsArchive( + string filePath, + out ArchiveType? type, + int bufferSize = ReaderOptions.DefaultBufferSize + ) + { + filePath.NotNullOrEmpty(nameof(filePath)); + using Stream s = File.OpenRead(filePath); + return IsArchive(s, out type, bufferSize); + } + + public static bool IsArchive( + Stream stream, + out ArchiveType? type, + int bufferSize = ReaderOptions.DefaultBufferSize + ) + { + type = null; + stream.NotNull(nameof(stream)); + + if (!stream.CanRead || !stream.CanSeek) + { + throw new ArgumentException("Stream should be readable and seekable"); + } + + var startPosition = stream.Position; + + foreach (var factory in Factory.Factories) + { + var isArchive = factory.IsArchive(stream); + stream.Position = startPosition; + + if (isArchive) + { + type = factory.KnownArchiveType; + return true; + } + } + + return false; + } + + public static IEnumerable GetFileParts(string part1) + { + part1.NotNullOrEmpty(nameof(part1)); + return GetFileParts(new FileInfo(part1)).Select(a => a.FullName); + } + + public static IEnumerable GetFileParts(FileInfo part1) + { + part1.NotNull(nameof(part1)); + yield return part1; + + foreach (var factory in Factory.Factories.OfType()) + { + var i = 1; + var part = factory.GetFilePart(i++, part1); + + if (part != null) + { + yield return part; + while ((part = factory.GetFilePart(i++, part1)) != null) + { + yield return part; + } + + yield break; + } + } + } + + public static IArchiveFactory AutoFactory { get; } = new AutoArchiveFactory(); +} From 9cf2b3129cf5c040f755b1f35e9c060a612805d2 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 13:54:15 +0000 Subject: [PATCH 11/36] fixed up async writer --- src/SharpCompress/Factories/GZipFactory.cs | 4 +- src/SharpCompress/Factories/TarFactory.cs | 4 +- src/SharpCompress/Factories/ZipFactory.cs | 4 +- src/SharpCompress/Writers/AbstractWriter.cs | 2 +- src/SharpCompress/Writers/IWriter.cs | 7 +- .../Writers/IWriterExtensions.cs | 212 +++++++++--------- src/SharpCompress/Writers/IWriterFactory.cs | 2 +- src/SharpCompress/Writers/WriterFactory.cs | 2 +- tests/SharpCompress.Test/ArchiveTests.cs | 14 ++ .../GZip/GZipWriterAsyncTests.cs | 2 +- .../Tar/TarArchiveAsyncTests.cs | 4 +- tests/SharpCompress.Test/WriterTests.cs | 2 +- .../Zip/ZipMemoryArchiveWithCrcAsyncTests.cs | 6 +- 13 files changed, 138 insertions(+), 127 deletions(-) diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index f8eada3f..f222fc31 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -182,7 +182,7 @@ public class GZipFactory } /// - public IWriter OpenAsync( + public IAsyncWriter OpenAsync( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default @@ -193,7 +193,7 @@ public class GZipFactory { throw new InvalidFormatException("GZip archives only support GZip compression type."); } - return Open(stream, writerOptions); + return (IAsyncWriter)Open(stream, writerOptions); } #endregion diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index 85832bab..a664b243 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -294,14 +294,14 @@ public class TarFactory new TarWriter(stream, new TarWriterOptions(writerOptions)); /// - public IWriter OpenAsync( + public IAsyncWriter OpenAsync( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return Open(stream, writerOptions); + return (IAsyncWriter)Open(stream, writerOptions); } #endregion diff --git a/src/SharpCompress/Factories/ZipFactory.cs b/src/SharpCompress/Factories/ZipFactory.cs index d0774a74..bdf8fedd 100644 --- a/src/SharpCompress/Factories/ZipFactory.cs +++ b/src/SharpCompress/Factories/ZipFactory.cs @@ -218,14 +218,14 @@ public class ZipFactory new ZipWriter(stream, new ZipWriterOptions(writerOptions)); /// - public IWriter OpenAsync( + public IAsyncWriter OpenAsync( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return Open(stream, writerOptions); + return (IAsyncWriter)Open(stream, writerOptions); } #endregion diff --git a/src/SharpCompress/Writers/AbstractWriter.cs b/src/SharpCompress/Writers/AbstractWriter.cs index 7dce6297..fec2f56f 100644 --- a/src/SharpCompress/Writers/AbstractWriter.cs +++ b/src/SharpCompress/Writers/AbstractWriter.cs @@ -8,7 +8,7 @@ using SharpCompress.IO; namespace SharpCompress.Writers; #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. -public abstract class AbstractWriter(ArchiveType type, WriterOptions writerOptions) : IWriter +public abstract class AbstractWriter(ArchiveType type, WriterOptions writerOptions) : IWriter, IAsyncWriter { private bool _isDisposed; diff --git a/src/SharpCompress/Writers/IWriter.cs b/src/SharpCompress/Writers/IWriter.cs index b51b4972..c33a3bf0 100644 --- a/src/SharpCompress/Writers/IWriter.cs +++ b/src/SharpCompress/Writers/IWriter.cs @@ -10,13 +10,18 @@ public interface IWriter : IDisposable { ArchiveType WriterType { get; } void Write(string filename, Stream source, DateTime? modificationTime); + void WriteDirectory(string directoryName, DateTime? modificationTime); +} + +public interface IAsyncWriter : IDisposable +{ + ArchiveType WriterType { get; } ValueTask WriteAsync( string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default ); - void WriteDirectory(string directoryName, DateTime? modificationTime); ValueTask WriteDirectoryAsync( string directoryName, DateTime? modificationTime, diff --git a/src/SharpCompress/Writers/IWriterExtensions.cs b/src/SharpCompress/Writers/IWriterExtensions.cs index 9b8fb67b..e2c66e8e 100644 --- a/src/SharpCompress/Writers/IWriterExtensions.cs +++ b/src/SharpCompress/Writers/IWriterExtensions.cs @@ -8,126 +8,118 @@ namespace SharpCompress.Writers; public static class IWriterExtensions { - public static void Write(this IWriter writer, string entryPath, Stream source) => - writer.Write(entryPath, source, null); - - public static void Write(this IWriter writer, string entryPath, FileInfo source) + extension(IWriter writer) { - if (!source.Exists) + public void Write(string entryPath, Stream source) => + writer.Write(entryPath, source, null); + + public void Write(string entryPath, FileInfo source) { - throw new ArgumentException("Source does not exist: " + source.FullName); - } - using var stream = source.OpenRead(); - writer.Write(entryPath, stream, source.LastWriteTime); - } + if (!source.Exists) + { + throw new ArgumentException("Source does not exist: " + source.FullName); + } - public static void Write(this IWriter writer, string entryPath, string source) => - writer.Write(entryPath, new FileInfo(source)); - - public static void WriteAll( - this IWriter writer, - string directory, - string searchPattern = "*", - SearchOption option = SearchOption.TopDirectoryOnly - ) => writer.WriteAll(directory, searchPattern, null, option); - - public static void WriteAll( - this IWriter writer, - string directory, - string searchPattern = "*", - Func? fileSearchFunc = null, - SearchOption option = SearchOption.TopDirectoryOnly - ) - { - if (!Directory.Exists(directory)) - { - throw new ArgumentException("Directory does not exist: " + directory); + using var stream = source.OpenRead(); + writer.Write(entryPath, stream, source.LastWriteTime); } - fileSearchFunc ??= n => true; - foreach ( - var file in Directory - .EnumerateFiles(directory, searchPattern, option) - .Where(fileSearchFunc) + public void Write(string entryPath, string source) => + writer.Write(entryPath, new FileInfo(source)); + + public void WriteAll(string directory, + string searchPattern = "*", + SearchOption option = SearchOption.TopDirectoryOnly + ) => writer.WriteAll(directory, searchPattern, null, option); + + public void WriteAll(string directory, + string searchPattern = "*", + Func? fileSearchFunc = null, + SearchOption option = SearchOption.TopDirectoryOnly ) { - writer.Write(file.Substring(directory.Length), file); + if (!Directory.Exists(directory)) + { + throw new ArgumentException("Directory does not exist: " + directory); + } + + fileSearchFunc ??= n => true; + foreach ( + var file in Directory + .EnumerateFiles(directory, searchPattern, option) + .Where(fileSearchFunc) + ) + { + writer.Write(file.Substring(directory.Length), file); + } } + + public void WriteDirectory(string directoryName) => + writer.WriteDirectory(directoryName, null); } - public static void WriteDirectory(this IWriter writer, string directoryName) => - writer.WriteDirectory(directoryName, null); + extension(IAsyncWriter writer) + { + public ValueTask WriteAsync(string entryPath, + Stream source, + CancellationToken cancellationToken = default + ) => writer.WriteAsync(entryPath, source, null, cancellationToken); + + public async ValueTask WriteAsync(string entryPath, + FileInfo source, + CancellationToken cancellationToken = default + ) + { + if (!source.Exists) + { + throw new ArgumentException("Source does not exist: " + source.FullName); + } + using var stream = source.OpenRead(); + await writer + .WriteAsync(entryPath, stream, source.LastWriteTime, cancellationToken) + .ConfigureAwait(false); + } + + public ValueTask WriteAsync(string entryPath, + string source, + CancellationToken cancellationToken = default + ) => writer.WriteAsync(entryPath, new FileInfo(source), cancellationToken); + + public ValueTask WriteAllAsync(string directory, + string searchPattern = "*", + SearchOption option = SearchOption.TopDirectoryOnly, + CancellationToken cancellationToken = default + ) => writer.WriteAllAsync(directory, searchPattern, null, option, cancellationToken); + + public async ValueTask WriteAllAsync(string directory, + string searchPattern = "*", + Func? fileSearchFunc = null, + SearchOption option = SearchOption.TopDirectoryOnly, + CancellationToken cancellationToken = default + ) + { + if (!Directory.Exists(directory)) + { + throw new ArgumentException("Directory does not exist: " + directory); + } + + fileSearchFunc ??= n => true; + foreach ( + var file in Directory + .EnumerateFiles(directory, searchPattern, option) + .Where(fileSearchFunc) + ) + { + await writer + .WriteAsync(file.Substring(directory.Length), file, cancellationToken) + .ConfigureAwait(false); + } + } + + public ValueTask WriteDirectoryAsync(string directoryName, + CancellationToken cancellationToken = default + ) => writer.WriteDirectoryAsync(directoryName, null, cancellationToken); + } // Async extensions - public static ValueTask WriteAsync( - this IWriter writer, - string entryPath, - Stream source, - CancellationToken cancellationToken = default - ) => writer.WriteAsync(entryPath, source, null, cancellationToken); - - public static async ValueTask WriteAsync( - this IWriter writer, - string entryPath, - FileInfo source, - CancellationToken cancellationToken = default - ) - { - if (!source.Exists) - { - throw new ArgumentException("Source does not exist: " + source.FullName); - } - using var stream = source.OpenRead(); - await writer - .WriteAsync(entryPath, stream, source.LastWriteTime, cancellationToken) - .ConfigureAwait(false); - } - - public static ValueTask WriteAsync( - this IWriter writer, - string entryPath, - string source, - CancellationToken cancellationToken = default - ) => writer.WriteAsync(entryPath, new FileInfo(source), cancellationToken); - - public static ValueTask WriteAllAsync( - this IWriter writer, - string directory, - string searchPattern = "*", - SearchOption option = SearchOption.TopDirectoryOnly, - CancellationToken cancellationToken = default - ) => writer.WriteAllAsync(directory, searchPattern, null, option, cancellationToken); - - public static async ValueTask WriteAllAsync( - this IWriter writer, - string directory, - string searchPattern = "*", - Func? fileSearchFunc = null, - SearchOption option = SearchOption.TopDirectoryOnly, - CancellationToken cancellationToken = default - ) - { - if (!Directory.Exists(directory)) - { - throw new ArgumentException("Directory does not exist: " + directory); - } - - fileSearchFunc ??= n => true; - foreach ( - var file in Directory - .EnumerateFiles(directory, searchPattern, option) - .Where(fileSearchFunc) - ) - { - await writer - .WriteAsync(file.Substring(directory.Length), file, cancellationToken) - .ConfigureAwait(false); - } - } - - public static ValueTask WriteDirectoryAsync( - this IWriter writer, - string directoryName, - CancellationToken cancellationToken = default - ) => writer.WriteDirectoryAsync(directoryName, null, cancellationToken); } diff --git a/src/SharpCompress/Writers/IWriterFactory.cs b/src/SharpCompress/Writers/IWriterFactory.cs index 2c68c249..9615f078 100644 --- a/src/SharpCompress/Writers/IWriterFactory.cs +++ b/src/SharpCompress/Writers/IWriterFactory.cs @@ -8,7 +8,7 @@ public interface IWriterFactory : IFactory { IWriter Open(Stream stream, WriterOptions writerOptions); - IWriter OpenAsync( + IAsyncWriter OpenAsync( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index 52b62e7d..be2a7780 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -31,7 +31,7 @@ public static class WriterFactory /// Writer options. /// Cancellation token. /// A task that returns an IWriter. - public static IWriter OpenAsync( + public static IAsyncWriter OpenAsync( Stream stream, ArchiveType archiveType, WriterOptions writerOptions, diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index 5853e82c..ee291145 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -380,6 +380,20 @@ public class ArchiveTests : ReaderTests return WriterFactory.Open(stream, ArchiveType.Zip, writerOptions); } + protected static IAsyncWriter CreateWriterWithLevelAsync( + Stream stream, + CompressionType compressionType, + int? compressionLevel = null + ) + { + var writerOptions = new ZipWriterOptions(compressionType); + if (compressionLevel.HasValue) + { + writerOptions.CompressionLevel = compressionLevel.Value; + } + return WriterFactory.OpenAsync(stream, ArchiveType.Zip, writerOptions); + } + /// /// Verifies archive content against expected files with CRC32 validation /// diff --git a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs index 3ada3c52..ff246dc2 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs @@ -22,7 +22,7 @@ public class GZipWriterAsyncTests : WriterTests FileAccess.Write ) ) - using (var writer = WriterFactory.Open(stream, ArchiveType.GZip, CompressionType.GZip)) + using (var writer = WriterFactory.OpenAsync(stream, ArchiveType.GZip, CompressionType.GZip)) { await writer.WriteAsync("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); } diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index d140c788..f29d4553 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -32,7 +32,7 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 1: create a tar file containing a file with the test name using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.None)) + using (var writer = WriterFactory.OpenAsync(stream, ArchiveType.Tar, CompressionType.None)) using (Stream inputStream = new MemoryStream()) { var sw = new StreamWriter(inputStream); @@ -76,7 +76,7 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 1: create a tar file containing a file with a long name using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.None)) + using (var writer = WriterFactory.OpenAsync(stream, ArchiveType.Tar, CompressionType.None)) using (Stream inputStream = new MemoryStream()) { var sw = new StreamWriter(inputStream); diff --git a/tests/SharpCompress.Test/WriterTests.cs b/tests/SharpCompress.Test/WriterTests.cs index 9a6a43de..95d82da3 100644 --- a/tests/SharpCompress.Test/WriterTests.cs +++ b/tests/SharpCompress.Test/WriterTests.cs @@ -73,7 +73,7 @@ public class WriterTests : TestBase writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using var writer = WriterFactory.Open(stream, _type, writerOptions); + using var writer = WriterFactory.OpenAsync(stream, _type, writerOptions); await writer.WriteAllAsync( ORIGINAL_FILES_PATH, "*", diff --git a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs index c04c9e93..e7c80a8e 100644 --- a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs @@ -60,7 +60,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests // Create zip archive in memory using var zipStream = new MemoryStream(); - using (var writer = CreateWriterWithLevel(zipStream, compressionType, compressionLevel)) + using (var writer = CreateWriterWithLevelAsync(zipStream, compressionType, compressionLevel)) { await writer.WriteAsync($"file1_{sizeMb}MiB.txt", new MemoryStream(file1Data)); await writer.WriteAsync($"data/file2_{sizeMb * 2}MiB.txt", new MemoryStream(file2Data)); @@ -129,7 +129,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests CompressionLevel = compressionLevel, }; - using (var writer = WriterFactory.Open(zipStream, ArchiveType.Zip, writerOptions)) + using (var writer = WriterFactory.OpenAsync(zipStream, ArchiveType.Zip, writerOptions)) { await writer.WriteAsync( $"{compressionType}_level_{compressionLevel}_{sizeMb}MiB.txt", @@ -191,7 +191,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests // Create archive with specified compression and level using var zipStream = new MemoryStream(); - using (var writer = CreateWriterWithLevel(zipStream, compressionType, compressionLevel)) + using (var writer = CreateWriterWithLevelAsync(zipStream, compressionType, compressionLevel)) { await writer.WriteAsync( $"{compressionType}_{compressionLevel}_{sizeMb}MiB.txt", From 1c0183ef118005a624229fd11568f653edffd16e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 13:56:56 +0000 Subject: [PATCH 12/36] force async tests --- tests/SharpCompress.Test/ArchiveTests.cs | 2 +- tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs | 9 +++++---- tests/SharpCompress.Test/Tar/TarWriterAsyncTests.cs | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index ee291145..45a1ef98 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -391,7 +391,7 @@ public class ArchiveTests : ReaderTests { writerOptions.CompressionLevel = compressionLevel.Value; } - return WriterFactory.OpenAsync(stream, ArchiveType.Zip, writerOptions); + return WriterFactory.OpenAsync(new AsyncOnlyStream(stream), ArchiveType.Zip, writerOptions); } /// diff --git a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs index ff246dc2..6057e1aa 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs @@ -1,6 +1,7 @@ using System.IO; using System.Threading.Tasks; using SharpCompress.Common; +using SharpCompress.Test.Mocks; using SharpCompress.Writers; using SharpCompress.Writers.GZip; using Xunit; @@ -22,7 +23,7 @@ public class GZipWriterAsyncTests : WriterTests FileAccess.Write ) ) - using (var writer = WriterFactory.OpenAsync(stream, ArchiveType.GZip, CompressionType.GZip)) + using (var writer = WriterFactory.OpenAsync(new AsyncOnlyStream(stream), ArchiveType.GZip, CompressionType.GZip)) { await writer.WriteAsync("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); } @@ -42,7 +43,7 @@ public class GZipWriterAsyncTests : WriterTests FileAccess.Write ) ) - using (var writer = new GZipWriter(stream)) + using (var writer = new GZipWriter(new AsyncOnlyStream(stream))) { await writer.WriteAsync("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); } @@ -57,7 +58,7 @@ public class GZipWriterAsyncTests : WriterTests Assert.Throws(() => { using Stream stream = File.OpenWrite(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); - using var writer = WriterFactory.Open(stream, ArchiveType.GZip, CompressionType.BZip2); + using var writer = WriterFactory.Open(new AsyncOnlyStream(stream), ArchiveType.GZip, CompressionType.BZip2); }); [Fact] @@ -70,7 +71,7 @@ public class GZipWriterAsyncTests : WriterTests FileAccess.Write ) ) - using (var writer = new GZipWriter(stream)) + using (var writer = new GZipWriter(new AsyncOnlyStream(stream))) { var path = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"); await writer.WriteAsync(path, path); diff --git a/tests/SharpCompress.Test/Tar/TarWriterAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarWriterAsyncTests.cs index 290a01ed..d9884543 100644 --- a/tests/SharpCompress.Test/Tar/TarWriterAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarWriterAsyncTests.cs @@ -2,6 +2,7 @@ using System.IO; using System.Text; using System.Threading.Tasks; using SharpCompress.Common; +using SharpCompress.Test.Mocks; using SharpCompress.Writers.Tar; using Xunit; @@ -66,7 +67,7 @@ public class TarWriterAsyncTests : WriterTests using Stream content = File.OpenRead(Path.Combine(ORIGINAL_FILES_PATH, "jpg", "test.jpg")); using ( var writer = new TarWriter( - stream, + new AsyncOnlyStream(stream), new TarWriterOptions(CompressionType.None, finalizeArchive) ) ) From ec7d2e357daaa4ffa061c9a0783e22624965176f Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 13:58:03 +0000 Subject: [PATCH 13/36] fix lock? --- Directory.Build.props | 1 + src/SharpCompress/packages.lock.json | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index b3d0b595..f03c2d6e 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -12,5 +12,6 @@ False true true + true diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index 41325333..032c15c4 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -216,9 +216,9 @@ "net10.0": { "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[10.0.0, )", - "resolved": "10.0.0", - "contentHash": "kICGrGYEzCNI3wPzfEXcwNHgTvlvVn9yJDhSdRK+oZQy4jvYH529u7O0xf5ocQKzOMjfS07+3z9PKRIjrFMJDA==" + "requested": "[10.0.1, )", + "resolved": "10.0.1", + "contentHash": "ISahzLHsHY7vrwqr2p1YWZ+gsxoBRtH7gWRDK8fDUst9pp2He0GiesaqEfeX0V8QMCJM3eNEHGGpnIcPjFo2NQ==" }, "Microsoft.NETFramework.ReferenceAssemblies": { "type": "Direct", From cdca909d84c767ba5ecb338b3a1c2ccb3b102cf6 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 13:58:31 +0000 Subject: [PATCH 14/36] fmt --- src/SharpCompress/Writers/AbstractWriter.cs | 4 +- .../Writers/IWriterExtensions.cs | 81 ++++++++++--------- .../GZip/GZipWriterAsyncTests.cs | 14 +++- .../Zip/ZipMemoryArchiveWithCrcAsyncTests.cs | 8 +- 4 files changed, 65 insertions(+), 42 deletions(-) diff --git a/src/SharpCompress/Writers/AbstractWriter.cs b/src/SharpCompress/Writers/AbstractWriter.cs index fec2f56f..63511afa 100644 --- a/src/SharpCompress/Writers/AbstractWriter.cs +++ b/src/SharpCompress/Writers/AbstractWriter.cs @@ -8,7 +8,9 @@ using SharpCompress.IO; namespace SharpCompress.Writers; #pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. -public abstract class AbstractWriter(ArchiveType type, WriterOptions writerOptions) : IWriter, IAsyncWriter +public abstract class AbstractWriter(ArchiveType type, WriterOptions writerOptions) + : IWriter, + IAsyncWriter { private bool _isDisposed; diff --git a/src/SharpCompress/Writers/IWriterExtensions.cs b/src/SharpCompress/Writers/IWriterExtensions.cs index e2c66e8e..706baf0e 100644 --- a/src/SharpCompress/Writers/IWriterExtensions.cs +++ b/src/SharpCompress/Writers/IWriterExtensions.cs @@ -10,8 +10,7 @@ public static class IWriterExtensions { extension(IWriter writer) { - public void Write(string entryPath, Stream source) => - writer.Write(entryPath, source, null); + public void Write(string entryPath, Stream source) => writer.Write(entryPath, source, null); public void Write(string entryPath, FileInfo source) { @@ -27,15 +26,17 @@ public static class IWriterExtensions public void Write(string entryPath, string source) => writer.Write(entryPath, new FileInfo(source)); - public void WriteAll(string directory, - string searchPattern = "*", - SearchOption option = SearchOption.TopDirectoryOnly + public void WriteAll( + string directory, + string searchPattern = "*", + SearchOption option = SearchOption.TopDirectoryOnly ) => writer.WriteAll(directory, searchPattern, null, option); - public void WriteAll(string directory, - string searchPattern = "*", - Func? fileSearchFunc = null, - SearchOption option = SearchOption.TopDirectoryOnly + public void WriteAll( + string directory, + string searchPattern = "*", + Func? fileSearchFunc = null, + SearchOption option = SearchOption.TopDirectoryOnly ) { if (!Directory.Exists(directory)) @@ -46,8 +47,8 @@ public static class IWriterExtensions fileSearchFunc ??= n => true; foreach ( var file in Directory - .EnumerateFiles(directory, searchPattern, option) - .Where(fileSearchFunc) + .EnumerateFiles(directory, searchPattern, option) + .Where(fileSearchFunc) ) { writer.Write(file.Substring(directory.Length), file); @@ -60,14 +61,16 @@ public static class IWriterExtensions extension(IAsyncWriter writer) { - public ValueTask WriteAsync(string entryPath, - Stream source, - CancellationToken cancellationToken = default + public ValueTask WriteAsync( + string entryPath, + Stream source, + CancellationToken cancellationToken = default ) => writer.WriteAsync(entryPath, source, null, cancellationToken); - public async ValueTask WriteAsync(string entryPath, - FileInfo source, - CancellationToken cancellationToken = default + public async ValueTask WriteAsync( + string entryPath, + FileInfo source, + CancellationToken cancellationToken = default ) { if (!source.Exists) @@ -76,26 +79,29 @@ public static class IWriterExtensions } using var stream = source.OpenRead(); await writer - .WriteAsync(entryPath, stream, source.LastWriteTime, cancellationToken) - .ConfigureAwait(false); + .WriteAsync(entryPath, stream, source.LastWriteTime, cancellationToken) + .ConfigureAwait(false); } - public ValueTask WriteAsync(string entryPath, - string source, - CancellationToken cancellationToken = default + public ValueTask WriteAsync( + string entryPath, + string source, + CancellationToken cancellationToken = default ) => writer.WriteAsync(entryPath, new FileInfo(source), cancellationToken); - public ValueTask WriteAllAsync(string directory, - string searchPattern = "*", - SearchOption option = SearchOption.TopDirectoryOnly, - CancellationToken cancellationToken = default + public ValueTask WriteAllAsync( + string directory, + string searchPattern = "*", + SearchOption option = SearchOption.TopDirectoryOnly, + CancellationToken cancellationToken = default ) => writer.WriteAllAsync(directory, searchPattern, null, option, cancellationToken); - public async ValueTask WriteAllAsync(string directory, - string searchPattern = "*", - Func? fileSearchFunc = null, - SearchOption option = SearchOption.TopDirectoryOnly, - CancellationToken cancellationToken = default + public async ValueTask WriteAllAsync( + string directory, + string searchPattern = "*", + Func? fileSearchFunc = null, + SearchOption option = SearchOption.TopDirectoryOnly, + CancellationToken cancellationToken = default ) { if (!Directory.Exists(directory)) @@ -106,18 +112,19 @@ public static class IWriterExtensions fileSearchFunc ??= n => true; foreach ( var file in Directory - .EnumerateFiles(directory, searchPattern, option) - .Where(fileSearchFunc) + .EnumerateFiles(directory, searchPattern, option) + .Where(fileSearchFunc) ) { await writer - .WriteAsync(file.Substring(directory.Length), file, cancellationToken) - .ConfigureAwait(false); + .WriteAsync(file.Substring(directory.Length), file, cancellationToken) + .ConfigureAwait(false); } } - public ValueTask WriteDirectoryAsync(string directoryName, - CancellationToken cancellationToken = default + public ValueTask WriteDirectoryAsync( + string directoryName, + CancellationToken cancellationToken = default ) => writer.WriteDirectoryAsync(directoryName, null, cancellationToken); } diff --git a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs index 6057e1aa..c93a14c9 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs @@ -23,7 +23,13 @@ public class GZipWriterAsyncTests : WriterTests FileAccess.Write ) ) - using (var writer = WriterFactory.OpenAsync(new AsyncOnlyStream(stream), ArchiveType.GZip, CompressionType.GZip)) + using ( + var writer = WriterFactory.OpenAsync( + new AsyncOnlyStream(stream), + ArchiveType.GZip, + CompressionType.GZip + ) + ) { await writer.WriteAsync("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); } @@ -58,7 +64,11 @@ public class GZipWriterAsyncTests : WriterTests Assert.Throws(() => { using Stream stream = File.OpenWrite(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); - using var writer = WriterFactory.Open(new AsyncOnlyStream(stream), ArchiveType.GZip, CompressionType.BZip2); + using var writer = WriterFactory.Open( + new AsyncOnlyStream(stream), + ArchiveType.GZip, + CompressionType.BZip2 + ); }); [Fact] diff --git a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs index e7c80a8e..471e8e31 100644 --- a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs @@ -60,7 +60,9 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests // Create zip archive in memory using var zipStream = new MemoryStream(); - using (var writer = CreateWriterWithLevelAsync(zipStream, compressionType, compressionLevel)) + using ( + var writer = CreateWriterWithLevelAsync(zipStream, compressionType, compressionLevel) + ) { await writer.WriteAsync($"file1_{sizeMb}MiB.txt", new MemoryStream(file1Data)); await writer.WriteAsync($"data/file2_{sizeMb * 2}MiB.txt", new MemoryStream(file2Data)); @@ -191,7 +193,9 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests // Create archive with specified compression and level using var zipStream = new MemoryStream(); - using (var writer = CreateWriterWithLevelAsync(zipStream, compressionType, compressionLevel)) + using ( + var writer = CreateWriterWithLevelAsync(zipStream, compressionType, compressionLevel) + ) { await writer.WriteAsync( $"{compressionType}_{compressionLevel}_{sizeMb}MiB.txt", From 0b5371d986b303d38602e4af478be4dde07ae234 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 14:06:14 +0000 Subject: [PATCH 15/36] more async fixing --- .../GZip/GZipArchiveAsyncTests.cs | 13 +++---- .../Mocks/AsyncOnlyStream.cs | 35 ++++++++++++------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index 5eabdcb0..b572dc32 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -5,6 +5,7 @@ using SharpCompress.Archives; using SharpCompress.Archives.GZip; using SharpCompress.Archives.Tar; using SharpCompress.Common; +using SharpCompress.Test.Mocks; using Xunit; namespace SharpCompress.Test.GZip; @@ -21,7 +22,7 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) #endif - using (var archive = ArchiveFactory.Open(stream)) + using (var archive = ArchiveFactory.Open(new AsyncOnlyStream(stream))) { var entry = archive.Entries.First(); await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); @@ -48,7 +49,7 @@ public class GZipArchiveAsyncTests : ArchiveTests await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) #endif { - await using (var archive = GZipArchive.OpenAsync(stream)) + await using (var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream))) { var entry = await archive.EntriesAsync.FirstAsync(); await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); @@ -76,7 +77,7 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); #endif - await using (var archive = GZipArchive.OpenAsync(stream)) + await using (var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream))) { Assert.Throws(() => archive.AddEntry("jpg\\test.jpg", jpg)); await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); @@ -97,7 +98,7 @@ public class GZipArchiveAsyncTests : ArchiveTests inputStream.Position = 0; } - using var archive = GZipArchive.Open(inputStream); + using var archive = GZipArchive.Open(new AsyncOnlyStream(inputStream)); var archiveEntry = archive.Entries.First(); MemoryStream tarStream; @@ -147,7 +148,7 @@ public class GZipArchiveAsyncTests : ArchiveTests public void TestGzCrcWithMostSignificantBitNotNegative_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - using var archive = GZipArchive.Open(stream); + using var archive = GZipArchive.Open(new AsyncOnlyStream(stream)); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { Assert.InRange(entry.Crc, 0L, 0xFFFFFFFFL); @@ -158,7 +159,7 @@ public class GZipArchiveAsyncTests : ArchiveTests public void TestGzArchiveTypeGzip_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - using var archive = GZipArchive.Open(stream); + using var archive = GZipArchive.Open(new AsyncOnlyStream(stream)); Assert.Equal(archive.Type, ArchiveType.GZip); } } diff --git a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs index d0b363e0..1c35e9b7 100644 --- a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs +++ b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs @@ -27,35 +27,44 @@ public class AsyncOnlyStream : Stream public override void Flush() => _stream.Flush(); - public override int Read(byte[] buffer, int offset, int count) - { - throw new NotSupportedException("Synchronous Read is not supported"); - } + public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException("Synchronous Read is not supported"); public override Task ReadAsync( byte[] buffer, int offset, int count, CancellationToken cancellationToken - ) - { - return _stream.ReadAsync(buffer, offset, count, cancellationToken); - } + ) => + _stream.ReadAsync(buffer, offset, count, cancellationToken); -#if !NETFRAMEWORK && !NETSTANDARD2_0 +#if NET8_0_OR_GREATER public override ValueTask ReadAsync( Memory buffer, CancellationToken cancellationToken = default - ) - { - return _stream.ReadAsync(buffer, cancellationToken); - } + ) => + _stream.ReadAsync(buffer, cancellationToken); #endif public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin); public override void SetLength(long value) => _stream.SetLength(value); + public override Task WriteAsync( + byte[] buffer, + int offset, + int count, + CancellationToken cancellationToken + ) => + throw new NotSupportedException("Synchronous Read is not supported"); + +#if NET8_0_OR_GREATER + public override ValueTask WriteAsync( + ReadOnlyMemory buffer, + CancellationToken cancellationToken = default + ) => + _stream.WriteAsync(buffer, cancellationToken); +#endif + public override void Write(byte[] buffer, int offset, int count) => _stream.Write(buffer, offset, count); From 0b8081f320efc8da40f19c1f9629dd8c685e7847 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 14:24:36 +0000 Subject: [PATCH 16/36] gzip fixes --- .../Archives/GZip/GZipArchive.cs | 13 +- src/SharpCompress/Common/GZip/GZipEntry.cs | 2 +- src/SharpCompress/Common/GZip/GZipFilePart.cs | 138 ++++++++++++++++-- .../Polyfills/AsyncEnumerableExtensions.cs | 30 +++- tests/SharpCompress.Test/GZip/AsyncTests.cs | 6 +- .../GZip/GZipArchiveAsyncTests.cs | 10 +- 6 files changed, 178 insertions(+), 21 deletions(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 03503fb8..253af836 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -119,7 +119,18 @@ public partial class GZipArchive : AbstractWritableArchive LoadEntriesAsync( + IAsyncEnumerable volumes + ) + { + var stream = (await volumes.SingleAsync()).Stream; + yield return new GZipArchiveEntry( + this, + await GZipFilePart.CreateAsync(stream, ReaderOptions.ArchiveEncoding) ); } diff --git a/src/SharpCompress/Common/GZip/GZipEntry.cs b/src/SharpCompress/Common/GZip/GZipEntry.cs index 9a551d07..c0cc6b10 100644 --- a/src/SharpCompress/Common/GZip/GZipEntry.cs +++ b/src/SharpCompress/Common/GZip/GZipEntry.cs @@ -40,6 +40,6 @@ public class GZipEntry : Entry internal static IEnumerable GetEntries(Stream stream, OptionsBase options) { - yield return new GZipEntry(new GZipFilePart(stream, options.ArchiveEncoding)); + yield return new GZipEntry(GZipFilePart.Create(stream, options.ArchiveEncoding)); } } diff --git a/src/SharpCompress/Common/GZip/GZipFilePart.cs b/src/SharpCompress/Common/GZip/GZipFilePart.cs index f1d0eaf1..ed7e9ed5 100644 --- a/src/SharpCompress/Common/GZip/GZipFilePart.cs +++ b/src/SharpCompress/Common/GZip/GZipFilePart.cs @@ -2,6 +2,8 @@ using System; using System.Buffers.Binary; using System.Collections.Generic; using System.IO; +using System.Threading; +using System.Threading.Tasks; using SharpCompress.Common.Tar.Headers; using SharpCompress.Compressors; using SharpCompress.Compressors.Deflate; @@ -13,28 +15,56 @@ internal sealed class GZipFilePart : FilePart private string? _name; private readonly Stream _stream; - internal GZipFilePart(Stream stream, IArchiveEncoding archiveEncoding) - : base(archiveEncoding) + internal static GZipFilePart Create(Stream stream, IArchiveEncoding archiveEncoding) { - _stream = stream; - ReadAndValidateGzipHeader(); + var part = new GZipFilePart(stream, archiveEncoding); + + part.ReadAndValidateGzipHeader(); if (stream.CanSeek) { var position = stream.Position; stream.Position = stream.Length - 8; - ReadTrailer(); + part.ReadTrailer(); stream.Position = position; - EntryStartPosition = position; + part.EntryStartPosition = position; } else { // For non-seekable streams, we can't read the trailer or track position. // Set to 0 since the stream will be read sequentially from its current position. - EntryStartPosition = 0; + part.EntryStartPosition = 0; } + return part; } - internal long EntryStartPosition { get; } + internal static async ValueTask CreateAsync(Stream stream, IArchiveEncoding archiveEncoding, CancellationToken cancellationToken = default) + { + var part = new GZipFilePart(stream, archiveEncoding); + + await part.ReadAndValidateGzipHeaderAsync(cancellationToken); + if (stream.CanSeek) + { + var position = stream.Position; + stream.Position = stream.Length - 8; + await part.ReadTrailerAsync(cancellationToken); + stream.Position = position; + part.EntryStartPosition = position; + } + else + { + // For non-seekable streams, we can't read the trailer or track position. + // Set to 0 since the stream will be read sequentially from its current position. + part.EntryStartPosition = 0; + } + return part; + } + + + private GZipFilePart(Stream stream, IArchiveEncoding archiveEncoding) + : base(archiveEncoding) => + _stream = stream; + + internal long EntryStartPosition { get; private set; } internal DateTime? DateModified { get; private set; } internal uint? Crc { get; private set; } @@ -51,12 +81,22 @@ internal sealed class GZipFilePart : FilePart { // Read and potentially verify the GZIP trailer: CRC32 and size mod 2^32 Span trailer = stackalloc byte[8]; - var n = _stream.Read(trailer); + _stream.ReadFully(trailer); Crc = BinaryPrimitives.ReadUInt32LittleEndian(trailer); UncompressedSize = BinaryPrimitives.ReadUInt32LittleEndian(trailer.Slice(4)); } + private async ValueTask ReadTrailerAsync(CancellationToken cancellationToken = default) + { + // Read and potentially verify the GZIP trailer: CRC32 and size mod 2^32 + var trailer = new byte[8]; + _ = await _stream.ReadFullyAsync(trailer, 0, 8, cancellationToken); + + Crc = BinaryPrimitives.ReadUInt32LittleEndian(trailer); + UncompressedSize = BinaryPrimitives.ReadUInt32LittleEndian(trailer.AsSpan().Slice(4)); + } + private void ReadAndValidateGzipHeader() { // read the header on the first read @@ -109,6 +149,59 @@ internal sealed class GZipFilePart : FilePart } } + private async ValueTask ReadAndValidateGzipHeaderAsync(CancellationToken cancellationToken = default) + { + // read the header on the first read + var header = new byte[10]; + var n = await _stream.ReadAsync(header, 0, 10, cancellationToken); + + // workitem 8501: handle edge case (decompress empty stream) + if (n == 0) + { + return; + } + + if (n != 10) + { + throw new ZlibException("Not a valid GZIP stream."); + } + + if (header[0] != 0x1F || header[1] != 0x8B || header[2] != 8) + { + throw new ZlibException("Bad GZIP header."); + } + + var timet = BinaryPrimitives.ReadInt32LittleEndian(header.AsSpan().Slice(4)); + DateModified = TarHeader.EPOCH.AddSeconds(timet); + if ((header[3] & 0x04) == 0x04) + { + // read and discard extra field + var lengthField = new byte[2]; + _ = await _stream.ReadAsync(lengthField, 0, 2, cancellationToken); + + var extraLength = (short)(lengthField[0] + (lengthField[1] * 256)); + var extra = new byte[extraLength]; + + if (!await _stream.ReadFullyAsync(extra, cancellationToken)) + { + throw new ZlibException("Unexpected end-of-file reading GZIP header."); + } + } + if ((header[3] & 0x08) == 0x08) + { + _name = await ReadZeroTerminatedStringAsync(_stream, cancellationToken); + } + if ((header[3] & 0x10) == 0x010) + { + await ReadZeroTerminatedStringAsync(_stream, cancellationToken); + } + if ((header[3] & 0x02) == 0x02) + { + var buf = new byte[1]; + _ = await _stream.ReadAsync(buf, 0, 1, cancellationToken); // CRC16, ignore + } + } + private string ReadZeroTerminatedString(Stream stream) { Span buf1 = stackalloc byte[1]; @@ -134,4 +227,31 @@ internal sealed class GZipFilePart : FilePart var buffer = list.ToArray(); return ArchiveEncoding.Decode(buffer); } + + private async ValueTask ReadZeroTerminatedStringAsync(Stream stream, CancellationToken cancellationToken = default) + { + var buf1 = new byte[1]; + var list = new List(); + var done = false; + do + { + // workitem 7740 + var n = await stream.ReadAsync(buf1, 0, 1, cancellationToken); + if (n != 1) + { + throw new ZlibException("Unexpected EOF reading GZIP header."); + } + if (buf1[0] == 0) + { + done = true; + } + else + { + list.Add(buf1[0]); + } + } while (!done); + var buffer = list.ToArray(); + return ArchiveEncoding.Decode(buffer); + } + } diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index e7cea151..a1280e69 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; +using System.Threading; using System.Threading.Tasks; namespace SharpCompress; @@ -29,6 +31,21 @@ public static class EnumerableExtensions public static class AsyncEnumerableExtensions { + public static async IAsyncEnumerable TakeAsync( + this IAsyncEnumerable source, + int count) + { + await foreach (var element in source) + { + yield return element; + + if (--count == 0) + { + break; + } + } + } + public static async ValueTask> ToListAsync(this IAsyncEnumerable source) { var list = new List(); @@ -118,10 +135,19 @@ public static class AsyncEnumerableExtensions public static async ValueTask SingleAsync( this IAsyncEnumerable source, - Func predicate + Func? predicate = null ) { - var enumerator = source.WhereAsync(predicate).GetAsyncEnumerator(); + IAsyncEnumerator enumerator; + if (predicate is null) + { + enumerator = source.GetAsyncEnumerator(); + } + else + { + enumerator = source.WhereAsync(predicate).GetAsyncEnumerator(); + } + if (!await enumerator.MoveNextAsync()) { throw new InvalidOperationException("The source sequence is empty."); diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index fabdafc0..037f1b38 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -74,9 +74,9 @@ public class AsyncTests : TestBase public async ValueTask Archive_Entry_Async_Open_Stream() { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"); - using var archive = ArchiveFactory.Open(testArchive); + await using var archive = await ArchiveFactory.OpenAsync(new AsyncOnlyStream(File.OpenRead(testArchive))); - foreach (var entry in archive.Entries.Where(e => !e.IsDirectory).Take(1)) + await foreach (var entry in archive.EntriesAsync.WhereAsync(e => !e.IsDirectory).TakeAsync(1)) { #if NETFRAMEWORK using var entryStream = await entry.OpenEntryStreamAsync(); @@ -104,7 +104,7 @@ public class AsyncTests : TestBase await using (var stream = File.Create(outputPath)) #endif using ( - var writer = WriterFactory.OpenAsync(stream, ArchiveType.Zip, CompressionType.Deflate) + var writer = WriterFactory.OpenAsync(new AsyncOnlyStream(stream), ArchiveType.Zip, CompressionType.Deflate) ) { var testFile = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"); diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index b572dc32..da02db29 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -145,21 +145,21 @@ public class GZipArchiveAsyncTests : ArchiveTests } [Fact] - public void TestGzCrcWithMostSignificantBitNotNegative_Async() + public async Task TestGzCrcWithMostSignificantBitNotNegative_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - using var archive = GZipArchive.Open(new AsyncOnlyStream(stream)); - foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) + await using var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + await foreach (var entry in archive.EntriesAsync.Where(entry => !entry.IsDirectory)) { Assert.InRange(entry.Crc, 0L, 0xFFFFFFFFL); } } [Fact] - public void TestGzArchiveTypeGzip_Async() + public async Task TestGzArchiveTypeGzip_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - using var archive = GZipArchive.Open(new AsyncOnlyStream(stream)); + await using var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream)); Assert.Equal(archive.Type, ArchiveType.GZip); } } From fc85f1fa2cdb79be4ef102dbc8230c1166ab5316 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 14:28:45 +0000 Subject: [PATCH 17/36] more tar async fixes --- .../Polyfills/AsyncEnumerableExtensions.cs | 24 ++++++++++++++++++ tests/SharpCompress.Test/ArchiveTests.cs | 12 ++++----- .../Tar/TarArchiveAsyncTests.cs | 25 ++++++++++--------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index a1280e69..5f219211 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -31,6 +31,30 @@ public static class EnumerableExtensions public static class AsyncEnumerableExtensions { + public static async IAsyncEnumerable Select( + this IAsyncEnumerable source, + Func selector) + { + await foreach (var element in source) + { + yield return selector(element); + } + } + public static async ValueTask CountAsync( + this IAsyncEnumerable source, + CancellationToken cancellationToken = default) + { + await using var e = source.GetAsyncEnumerator(cancellationToken); + + var count = 0; + while (await e.MoveNextAsync()) + { + checked { count++; } + } + + return count; + } + public static async IAsyncEnumerable TakeAsync( this IAsyncEnumerable source, int count) diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index 45a1ef98..302c4d45 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -643,7 +643,7 @@ public class ArchiveTests : ReaderTests } [Fact] - public void ArchiveFactory_Open_WithPreWrappedStream() + public async Task ArchiveFactory_Open_WithPreWrappedStream() { // Test that ArchiveFactory.Open works correctly with a stream that's already wrapped // This addresses the issue where ZIP files fail to open on Linux @@ -652,25 +652,25 @@ public class ArchiveTests : ReaderTests // Open with a pre-wrapped stream using (var fileStream = File.OpenRead(testArchive)) using (var wrappedStream = SharpCompressStream.Create(fileStream, bufferSize: 32768)) - using (var archive = ArchiveFactory.Open(wrappedStream)) + await using (var archive = await ArchiveFactory.OpenAsync(new AsyncOnlyStream(wrappedStream))) { Assert.Equal(ArchiveType.Zip, archive.Type); - Assert.Equal(3, archive.Entries.Count()); + Assert.Equal(3, await archive.EntriesAsync.CountAsync()); } } [Fact] - public void ArchiveFactory_Open_WithRawFileStream() + public async Task ArchiveFactory_Open_WithRawFileStream() { // Test that ArchiveFactory.Open works correctly with a raw FileStream // This is the common use case reported in the issue var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.noEmptyDirs.zip"); using (var stream = File.OpenRead(testArchive)) - using (var archive = ArchiveFactory.Open(stream)) + await using (var archive = await ArchiveFactory.OpenAsync(new AsyncOnlyStream(stream))) { Assert.Equal(ArchiveType.Zip, archive.Type); - Assert.Equal(3, archive.Entries.Count()); + Assert.Equal(3, await archive.EntriesAsync.CountAsync()); } } } diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index f29d4553..4481cdaa 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -8,6 +8,7 @@ using SharpCompress.Archives.Tar; using SharpCompress.Common; using SharpCompress.Readers; using SharpCompress.Readers.Tar; +using SharpCompress.Test.Mocks; using SharpCompress.Writers; using SharpCompress.Writers.Tar; using Xunit; @@ -45,12 +46,12 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); - using (var archive2 = TarArchive.Open(unmodified)) + await using (var archive2 = TarArchive.OpenAsync(new AsyncOnlyStream(File.OpenRead(unmodified)))) { - Assert.Equal(1, archive2.Entries.Count()); - Assert.Contains(filename, archive2.Entries.Select(entry => entry.Key)); + Assert.Equal(1, await archive2.EntriesAsync.CountAsync()); + Assert.Contains(filename, await archive2.EntriesAsync.Select(entry => entry.Key).ToListAsync()); - foreach (var entry in archive2.Entries) + await foreach (var entry in archive2.EntriesAsync) { Assert.Equal( "dummy filecontent", @@ -89,12 +90,12 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); - using (var archive2 = TarArchive.Open(unmodified)) + await using (var archive2 = TarArchive.OpenAsync(new AsyncOnlyStream(File.OpenRead(unmodified)))) { - Assert.Equal(1, archive2.Entries.Count()); - Assert.Contains(longFilename, archive2.Entries.Select(entry => entry.Key)); + Assert.Equal(1, await archive2.EntriesAsync.CountAsync()); + Assert.Contains(longFilename, await archive2.EntriesAsync.Select(entry => entry.Key).ToListAsync()); - foreach (var entry in archive2.Entries) + await foreach (var entry in archive2.EntriesAsync) { Assert.Equal( "dummy filecontent", @@ -172,9 +173,9 @@ public class TarArchiveAsyncTests : ArchiveTests using (var inputMemory = new MemoryStream(mstm.ToArray())) { var tropt = new ReaderOptions { ArchiveEncoding = enc }; - using (var tr = TarReader.Open(inputMemory, tropt)) + await using (var tr = ReaderFactory.OpenAsync(inputMemory, tropt)) { - while (tr.MoveToNextEntry()) + while (await tr.MoveToNextEntryAsync()) { Assert.Equal(fname, tr.Entry.Key); } @@ -205,9 +206,9 @@ public class TarArchiveAsyncTests : ArchiveTests var numberOfEntries = 0; - using (var archiveFactory = TarArchive.Open(memoryStream)) +await using (var archiveFactory = TarArchive.OpenAsync(new AsyncOnlyStream(memoryStream))) { - foreach (var entry in archiveFactory.Entries) + await foreach (var entry in archiveFactory.EntriesAsync) { ++numberOfEntries; From a1a86cdde8ca1fc6125873123b4767f74519ca9a Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 14:29:10 +0000 Subject: [PATCH 18/36] fmt --- .../Archives/GZip/GZipArchive.cs | 2 +- src/SharpCompress/Common/GZip/GZipFilePart.cs | 20 ++++++++++------ .../Polyfills/AsyncEnumerableExtensions.cs | 24 +++++++++++-------- tests/SharpCompress.Test/ArchiveTests.cs | 4 +++- tests/SharpCompress.Test/GZip/AsyncTests.cs | 14 ++++++++--- .../GZip/GZipArchiveAsyncTests.cs | 2 +- .../Mocks/AsyncOnlyStream.cs | 15 +++++------- .../Tar/TarArchiveAsyncTests.cs | 22 ++++++++++++----- 8 files changed, 65 insertions(+), 38 deletions(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 253af836..757c1bc2 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -119,7 +119,7 @@ public partial class GZipArchive : AbstractWritableArchive CreateAsync(Stream stream, IArchiveEncoding archiveEncoding, CancellationToken cancellationToken = default) + internal static async ValueTask CreateAsync( + Stream stream, + IArchiveEncoding archiveEncoding, + CancellationToken cancellationToken = default + ) { var part = new GZipFilePart(stream, archiveEncoding); @@ -59,10 +63,8 @@ internal sealed class GZipFilePart : FilePart return part; } - private GZipFilePart(Stream stream, IArchiveEncoding archiveEncoding) - : base(archiveEncoding) => - _stream = stream; + : base(archiveEncoding) => _stream = stream; internal long EntryStartPosition { get; private set; } @@ -149,7 +151,9 @@ internal sealed class GZipFilePart : FilePart } } - private async ValueTask ReadAndValidateGzipHeaderAsync(CancellationToken cancellationToken = default) + private async ValueTask ReadAndValidateGzipHeaderAsync( + CancellationToken cancellationToken = default + ) { // read the header on the first read var header = new byte[10]; @@ -228,7 +232,10 @@ internal sealed class GZipFilePart : FilePart return ArchiveEncoding.Decode(buffer); } - private async ValueTask ReadZeroTerminatedStringAsync(Stream stream, CancellationToken cancellationToken = default) + private async ValueTask ReadZeroTerminatedStringAsync( + Stream stream, + CancellationToken cancellationToken = default + ) { var buf1 = new byte[1]; var list = new List(); @@ -253,5 +260,4 @@ internal sealed class GZipFilePart : FilePart var buffer = list.ToArray(); return ArchiveEncoding.Decode(buffer); } - } diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index 5f219211..8cc3c1d1 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -33,31 +33,35 @@ public static class AsyncEnumerableExtensions { public static async IAsyncEnumerable Select( this IAsyncEnumerable source, - Func selector) + Func selector + ) { - await foreach (var element in source) - { - yield return selector(element); - } + await foreach (var element in source) + { + yield return selector(element); + } } + public static async ValueTask CountAsync( this IAsyncEnumerable source, - CancellationToken cancellationToken = default) + CancellationToken cancellationToken = default + ) { await using var e = source.GetAsyncEnumerator(cancellationToken); var count = 0; while (await e.MoveNextAsync()) { - checked { count++; } + checked + { + count++; + } } return count; } - public static async IAsyncEnumerable TakeAsync( - this IAsyncEnumerable source, - int count) + public static async IAsyncEnumerable TakeAsync(this IAsyncEnumerable source, int count) { await foreach (var element in source) { diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index 302c4d45..1fe1b024 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -652,7 +652,9 @@ public class ArchiveTests : ReaderTests // Open with a pre-wrapped stream using (var fileStream = File.OpenRead(testArchive)) using (var wrappedStream = SharpCompressStream.Create(fileStream, bufferSize: 32768)) - await using (var archive = await ArchiveFactory.OpenAsync(new AsyncOnlyStream(wrappedStream))) + await using ( + var archive = await ArchiveFactory.OpenAsync(new AsyncOnlyStream(wrappedStream)) + ) { Assert.Equal(ArchiveType.Zip, archive.Type); Assert.Equal(3, await archive.EntriesAsync.CountAsync()); diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index 037f1b38..cfe5b559 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -74,9 +74,13 @@ public class AsyncTests : TestBase public async ValueTask Archive_Entry_Async_Open_Stream() { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"); - await using var archive = await ArchiveFactory.OpenAsync(new AsyncOnlyStream(File.OpenRead(testArchive))); + await using var archive = await ArchiveFactory.OpenAsync( + new AsyncOnlyStream(File.OpenRead(testArchive)) + ); - await foreach (var entry in archive.EntriesAsync.WhereAsync(e => !e.IsDirectory).TakeAsync(1)) + await foreach ( + var entry in archive.EntriesAsync.WhereAsync(e => !e.IsDirectory).TakeAsync(1) + ) { #if NETFRAMEWORK using var entryStream = await entry.OpenEntryStreamAsync(); @@ -104,7 +108,11 @@ public class AsyncTests : TestBase await using (var stream = File.Create(outputPath)) #endif using ( - var writer = WriterFactory.OpenAsync(new AsyncOnlyStream(stream), ArchiveType.Zip, CompressionType.Deflate) + var writer = WriterFactory.OpenAsync( + new AsyncOnlyStream(stream), + ArchiveType.Zip, + CompressionType.Deflate + ) ) { var testFile = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"); diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index da02db29..98bcf41a 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -156,7 +156,7 @@ public class GZipArchiveAsyncTests : ArchiveTests } [Fact] - public async Task TestGzArchiveTypeGzip_Async() + public async Task TestGzArchiveTypeGzip_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); await using var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream)); diff --git a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs index 1c35e9b7..a7fb32eb 100644 --- a/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs +++ b/tests/SharpCompress.Test/Mocks/AsyncOnlyStream.cs @@ -27,22 +27,21 @@ public class AsyncOnlyStream : Stream public override void Flush() => _stream.Flush(); - public override int Read(byte[] buffer, int offset, int count) => throw new NotSupportedException("Synchronous Read is not supported"); + public override int Read(byte[] buffer, int offset, int count) => + throw new NotSupportedException("Synchronous Read is not supported"); public override Task ReadAsync( byte[] buffer, int offset, int count, CancellationToken cancellationToken - ) => - _stream.ReadAsync(buffer, offset, count, cancellationToken); + ) => _stream.ReadAsync(buffer, offset, count, cancellationToken); #if NET8_0_OR_GREATER public override ValueTask ReadAsync( Memory buffer, CancellationToken cancellationToken = default - ) => - _stream.ReadAsync(buffer, cancellationToken); + ) => _stream.ReadAsync(buffer, cancellationToken); #endif public override long Seek(long offset, SeekOrigin origin) => _stream.Seek(offset, origin); @@ -54,15 +53,13 @@ public class AsyncOnlyStream : Stream int offset, int count, CancellationToken cancellationToken - ) => - throw new NotSupportedException("Synchronous Read is not supported"); + ) => throw new NotSupportedException("Synchronous Read is not supported"); #if NET8_0_OR_GREATER public override ValueTask WriteAsync( ReadOnlyMemory buffer, CancellationToken cancellationToken = default - ) => - _stream.WriteAsync(buffer, cancellationToken); + ) => _stream.WriteAsync(buffer, cancellationToken); #endif public override void Write(byte[] buffer, int offset, int count) => diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index 4481cdaa..f7758bb3 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -46,10 +46,15 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); - await using (var archive2 = TarArchive.OpenAsync(new AsyncOnlyStream(File.OpenRead(unmodified)))) + await using ( + var archive2 = TarArchive.OpenAsync(new AsyncOnlyStream(File.OpenRead(unmodified))) + ) { Assert.Equal(1, await archive2.EntriesAsync.CountAsync()); - Assert.Contains(filename, await archive2.EntriesAsync.Select(entry => entry.Key).ToListAsync()); + Assert.Contains( + filename, + await archive2.EntriesAsync.Select(entry => entry.Key).ToListAsync() + ); await foreach (var entry in archive2.EntriesAsync) { @@ -90,10 +95,15 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); - await using (var archive2 = TarArchive.OpenAsync(new AsyncOnlyStream(File.OpenRead(unmodified)))) + await using ( + var archive2 = TarArchive.OpenAsync(new AsyncOnlyStream(File.OpenRead(unmodified))) + ) { Assert.Equal(1, await archive2.EntriesAsync.CountAsync()); - Assert.Contains(longFilename, await archive2.EntriesAsync.Select(entry => entry.Key).ToListAsync()); + Assert.Contains( + longFilename, + await archive2.EntriesAsync.Select(entry => entry.Key).ToListAsync() + ); await foreach (var entry in archive2.EntriesAsync) { @@ -206,9 +216,9 @@ public class TarArchiveAsyncTests : ArchiveTests var numberOfEntries = 0; -await using (var archiveFactory = TarArchive.OpenAsync(new AsyncOnlyStream(memoryStream))) + await using (var archiveFactory = TarArchive.OpenAsync(new AsyncOnlyStream(memoryStream))) { - await foreach (var entry in archiveFactory.EntriesAsync) + await foreach (var entry in archiveFactory.EntriesAsync) { ++numberOfEntries; From ddc08e068e150262890578233388670358b513b3 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 15:16:38 +0000 Subject: [PATCH 19/36] fix async error --- .editorconfig | 1 - src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs | 4 ++-- src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs | 4 ++-- src/SharpCompress/Archives/Tar/TarArchiveEntry.cs | 4 ++-- 4 files changed, 6 insertions(+), 7 deletions(-) diff --git a/.editorconfig b/.editorconfig index 96f2a953..ec46ee72 100644 --- a/.editorconfig +++ b/.editorconfig @@ -307,7 +307,6 @@ dotnet_diagnostic.CS8602.severity = error dotnet_diagnostic.CS8604.severity = error dotnet_diagnostic.CS8618.severity = error dotnet_diagnostic.CS0618.severity = suggestion -dotnet_diagnostic.CS1998.severity = error dotnet_diagnostic.CS4014.severity = error dotnet_diagnostic.CS8600.severity = error dotnet_diagnostic.CS8603.severity = error diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs index 049c7262..9a3f65ab 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs @@ -23,12 +23,12 @@ public class GZipArchiveEntry : GZipEntry, IArchiveEntry return Parts.Single().GetCompressedStream().NotNull(); } - public async ValueTask OpenEntryStreamAsync( + public ValueTask OpenEntryStreamAsync( CancellationToken cancellationToken = default ) { // GZip synchronous implementation is fast enough, just wrap it - return OpenEntryStream(); + return new(OpenEntryStream()); } #region IArchiveEntry Members diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs index a0d4a50d..acd93494 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs @@ -12,9 +12,9 @@ public class SevenZipArchiveEntry : SevenZipEntry, IArchiveEntry public Stream OpenEntryStream() => FilePart.GetCompressedStream(); - public async ValueTask OpenEntryStreamAsync( + public ValueTask OpenEntryStreamAsync( CancellationToken cancellationToken = default - ) => OpenEntryStream(); + ) => new(OpenEntryStream()); public IArchive Archive { get; } diff --git a/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs b/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs index cbea2c71..95b5004b 100644 --- a/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs +++ b/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs @@ -14,9 +14,9 @@ public class TarArchiveEntry : TarEntry, IArchiveEntry public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream().NotNull(); - public async ValueTask OpenEntryStreamAsync( + public ValueTask OpenEntryStreamAsync( CancellationToken cancellationToken = default - ) => OpenEntryStream(); + ) => new(OpenEntryStream()); #region IArchiveEntry Members From 767f3a498577f8bbef562c59b20f713ae4c7946e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 15:26:45 +0000 Subject: [PATCH 20/36] fix up extensions to more like polyfills --- .../Archives/GZip/GZipArchiveEntry.cs | 4 +- .../Archives/SevenZip/SevenZipArchiveEntry.cs | 5 +- .../Archives/Tar/TarArchiveEntry.cs | 5 +- .../Archives/Zip/ZipArchive.Factory.cs | 4 +- .../Polyfills/AsyncEnumerableExtensions.cs | 246 ++++++++---------- .../Polyfills/StreamExtensions.cs | 4 + tests/SharpCompress.Test/GZip/AsyncTests.cs | 6 +- 7 files changed, 121 insertions(+), 153 deletions(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs index 9a3f65ab..a8f7b777 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs @@ -23,9 +23,7 @@ public class GZipArchiveEntry : GZipEntry, IArchiveEntry return Parts.Single().GetCompressedStream().NotNull(); } - public ValueTask OpenEntryStreamAsync( - CancellationToken cancellationToken = default - ) + public ValueTask OpenEntryStreamAsync(CancellationToken cancellationToken = default) { // GZip synchronous implementation is fast enough, just wrap it return new(OpenEntryStream()); diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs index acd93494..5b785e7c 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchiveEntry.cs @@ -12,9 +12,8 @@ public class SevenZipArchiveEntry : SevenZipEntry, IArchiveEntry public Stream OpenEntryStream() => FilePart.GetCompressedStream(); - public ValueTask OpenEntryStreamAsync( - CancellationToken cancellationToken = default - ) => new(OpenEntryStream()); + public ValueTask OpenEntryStreamAsync(CancellationToken cancellationToken = default) => + new(OpenEntryStream()); public IArchive Archive { get; } diff --git a/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs b/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs index 95b5004b..b09876e9 100644 --- a/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs +++ b/src/SharpCompress/Archives/Tar/TarArchiveEntry.cs @@ -14,9 +14,8 @@ public class TarArchiveEntry : TarEntry, IArchiveEntry public virtual Stream OpenEntryStream() => Parts.Single().GetCompressedStream().NotNull(); - public ValueTask OpenEntryStreamAsync( - CancellationToken cancellationToken = default - ) => new(OpenEntryStream()); + public ValueTask OpenEntryStreamAsync(CancellationToken cancellationToken = default) => + new(OpenEntryStream()); #region IArchiveEntry Members diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs index 9fb47187..a4061db1 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs @@ -243,8 +243,8 @@ public partial class ZipArchive var header = await headerFactory .ReadStreamHeaderAsync(stream) - .WhereAsync(x => x.ZipHeaderType != ZipHeaderType.Split) - .FirstOrDefaultAsync(); + .Where(x => x.ZipHeaderType != ZipHeaderType.Split) + .FirstOrDefaultAsync(cancellationToken); if (header is null) { return false; diff --git a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs index 8cc3c1d1..9d0dad0a 100644 --- a/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs +++ b/src/SharpCompress/Polyfills/AsyncEnumerableExtensions.cs @@ -31,58 +31,128 @@ public static class EnumerableExtensions public static class AsyncEnumerableExtensions { - public static async IAsyncEnumerable Select( - this IAsyncEnumerable source, - Func selector - ) +#if !NET10_0_OR_GREATER + extension(IAsyncEnumerable source) { - await foreach (var element in source) + public async IAsyncEnumerable Select(Func selector) { - yield return selector(element); - } - } - - public static async ValueTask CountAsync( - this IAsyncEnumerable source, - CancellationToken cancellationToken = default - ) - { - await using var e = source.GetAsyncEnumerator(cancellationToken); - - var count = 0; - while (await e.MoveNextAsync()) - { - checked + await foreach (var element in source) { - count++; + yield return selector(element); } } - return count; - } - - public static async IAsyncEnumerable TakeAsync(this IAsyncEnumerable source, int count) - { - await foreach (var element in source) + public async ValueTask CountAsync(CancellationToken cancellationToken = default) { - yield return element; + await using var e = source.GetAsyncEnumerator(cancellationToken); - if (--count == 0) + var count = 0; + while (await e.MoveNextAsync()) { - break; + checked + { + count++; + } + } + + return count; + } + + public async IAsyncEnumerable Take(int count) + { + await foreach (var element in source) + { + yield return element; + + if (--count == 0) + { + break; + } } } - } - public static async ValueTask> ToListAsync(this IAsyncEnumerable source) - { - var list = new List(); - await foreach (var item in source) + public async ValueTask> ToListAsync() { - list.Add(item); + var list = new List(); + await foreach (var item in source) + { + list.Add(item); + } + return list; + } + + public async ValueTask AllAsync(Func predicate) + { + await foreach (var item in source) + { + if (!predicate(item)) + { + return false; + } + } + + return true; + } + + public async IAsyncEnumerable Where(Func predicate) + { + await foreach (var item in source) + { + if (predicate(item)) + { + yield return item; + } + } + } + + public async ValueTask SingleAsync(Func? predicate = null) + { + IAsyncEnumerator enumerator; + if (predicate is null) + { + enumerator = source.GetAsyncEnumerator(); + } + else + { + enumerator = source.Where(predicate).GetAsyncEnumerator(); + } + + if (!await enumerator.MoveNextAsync()) + { + throw new InvalidOperationException("The source sequence is empty."); + } + var value = enumerator.Current; + if (await enumerator.MoveNextAsync()) + { + throw new InvalidOperationException( + "The source sequence contains more than one element." + ); + } + return value; + } + + public async ValueTask FirstAsync() + { + await foreach (var item in source) + { + return item; + } + throw new InvalidOperationException("The source sequence is empty."); + } + + public async ValueTask FirstOrDefaultAsync( + CancellationToken cancellationToken = default + ) + { + await foreach (var item in source.WithCancellation(cancellationToken)) + { + return item; + } + + return default; } - return list; } +#endif public static async IAsyncEnumerable CastAsync( this IAsyncEnumerable source @@ -101,111 +171,11 @@ public static class AsyncEnumerableExtensions Func func ) { - TAccumulate result = seed; + var result = seed; await foreach (var element in source) { result = func(result, element); } return result; } - - public static async ValueTask AllAsync( - this IAsyncEnumerable source, - Func predicate - ) - { - await foreach (var item in source) - { - if (!predicate(item)) - { - return false; - } - } - - return true; - } - - public static IAsyncEnumerable Where( - this IAsyncEnumerable source, - Func predicate - ) - { - return WhereIterator(source, predicate); - } - - private static async IAsyncEnumerable WhereIterator( - IAsyncEnumerable source, - Func predicate - ) - { - await foreach (var item in source) - { - if (predicate(item)) - { - yield return item; - } - } - } - - public static async IAsyncEnumerable WhereAsync( - this IAsyncEnumerable source, - Func predicate - ) - { - await foreach (var item in source) - { - if (predicate(item)) - { - yield return item; - } - } - } - - public static async ValueTask SingleAsync( - this IAsyncEnumerable source, - Func? predicate = null - ) - { - IAsyncEnumerator enumerator; - if (predicate is null) - { - enumerator = source.GetAsyncEnumerator(); - } - else - { - enumerator = source.WhereAsync(predicate).GetAsyncEnumerator(); - } - - if (!await enumerator.MoveNextAsync()) - { - throw new InvalidOperationException("The source sequence is empty."); - } - var value = enumerator.Current; - if (await enumerator.MoveNextAsync()) - { - throw new InvalidOperationException( - "The source sequence contains more than one element." - ); - } - return value; - } - - public static async ValueTask FirstAsync(this IAsyncEnumerable source) - { - await foreach (var item in source) - { - return item; - } - throw new InvalidOperationException("The source sequence is empty."); - } - - public static async ValueTask FirstOrDefaultAsync(this IAsyncEnumerable source) - { - await foreach (var item in source) - { - return item; - } - - return default; - } } diff --git a/src/SharpCompress/Polyfills/StreamExtensions.cs b/src/SharpCompress/Polyfills/StreamExtensions.cs index 6316416d..6803e1e0 100644 --- a/src/SharpCompress/Polyfills/StreamExtensions.cs +++ b/src/SharpCompress/Polyfills/StreamExtensions.cs @@ -27,7 +27,11 @@ public static class StreamExtensions public Task SkipAsync(CancellationToken cancellationToken = default) { cancellationToken.ThrowIfCancellationRequested(); +#if NET8_0_OR_GREATER + return stream.CopyToAsync(Stream.Null, cancellationToken); +#else return stream.CopyToAsync(Stream.Null); +#endif } internal int Read(Span buffer) diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index cfe5b559..5d1a7fb2 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -78,9 +78,7 @@ public class AsyncTests : TestBase new AsyncOnlyStream(File.OpenRead(testArchive)) ); - await foreach ( - var entry in archive.EntriesAsync.WhereAsync(e => !e.IsDirectory).TakeAsync(1) - ) + await foreach (var entry in archive.EntriesAsync.Where(e => !e.IsDirectory).Take(1)) { #if NETFRAMEWORK using var entryStream = await entry.OpenEntryStreamAsync(); @@ -128,7 +126,7 @@ public class AsyncTests : TestBase // Verify the archive was created and contains the entry Assert.True(File.Exists(outputPath)); await using var archive = ZipArchive.OpenAsync(outputPath); - Assert.Single(await archive.EntriesAsync.WhereAsync(e => !e.IsDirectory).ToListAsync()); + Assert.Single(await archive.EntriesAsync.Where(e => !e.IsDirectory).ToListAsync()); } [Fact] From 4fa976b4787576e728a47fa002b21614b73a6f64 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 15:29:02 +0000 Subject: [PATCH 21/36] remove unused ref --- AGENTS.md | 1 - Directory.Packages.props | 1 - tests/SharpCompress.Test/SharpCompress.Test.csproj | 3 --- tests/SharpCompress.Test/packages.lock.json | 12 ------------ 4 files changed, 17 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 38ae5692..35e77d70 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -178,5 +178,4 @@ SharpCompress supports multiple archive and compression formats: 2. **Solid archives (Rar, 7Zip)** - Use `ExtractAllEntries()` for best performance, not individual entry extraction 3. **Stream disposal** - Always set `LeaveStreamOpen` explicitly when needed (default is to close) 4. **Tar + non-seekable stream** - Must provide file size or it will throw -5. **Multi-framework differences** - Some features differ between .NET Framework and modern .NET (e.g., Mono.Posix) 6. **Format detection** - Use `ReaderFactory.Open()` for auto-detection, test with actual archive files diff --git a/Directory.Packages.props b/Directory.Packages.props index 9f2e0c86..7d532c3a 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -6,7 +6,6 @@ - diff --git a/tests/SharpCompress.Test/SharpCompress.Test.csproj b/tests/SharpCompress.Test/SharpCompress.Test.csproj index c16a1581..0399d78c 100644 --- a/tests/SharpCompress.Test/SharpCompress.Test.csproj +++ b/tests/SharpCompress.Test/SharpCompress.Test.csproj @@ -24,7 +24,4 @@ - - - diff --git a/tests/SharpCompress.Test/packages.lock.json b/tests/SharpCompress.Test/packages.lock.json index baea090e..3e770308 100644 --- a/tests/SharpCompress.Test/packages.lock.json +++ b/tests/SharpCompress.Test/packages.lock.json @@ -45,12 +45,6 @@ "resolved": "17.14.15", "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" }, - "Mono.Posix.NETStandard": { - "type": "Direct", - "requested": "[1.0.0, )", - "resolved": "1.0.0", - "contentHash": "vSN/L1uaVwKsiLa95bYu2SGkF0iY3xMblTfxc8alSziPuVfJpj3geVqHGAA75J7cZkMuKpFVikz82Lo6y6LLdA==" - }, "xunit": { "type": "Direct", "requested": "[2.9.3, )", @@ -264,12 +258,6 @@ "resolved": "17.14.15", "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" }, - "Mono.Posix.NETStandard": { - "type": "Direct", - "requested": "[1.0.0, )", - "resolved": "1.0.0", - "contentHash": "vSN/L1uaVwKsiLa95bYu2SGkF0iY3xMblTfxc8alSziPuVfJpj3geVqHGAA75J7cZkMuKpFVikz82Lo6y6LLdA==" - }, "xunit": { "type": "Direct", "requested": "[2.9.3, )", From ca4cf25a1f07c54dfdcae8444dd424408fa4eace Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 16:39:55 +0000 Subject: [PATCH 22/36] clean up lazy readonly collections and add tests --- .../LazyAsyncReadOnlyCollection.cs | 40 +- src/SharpCompress/LazyReadOnlyCollection.cs | 46 +-- .../LazyAsyncReadOnlyCollectionTests.cs | 358 ++++++++++++++++ .../LazyReadOnlyCollectionTests.cs | 382 ++++++++++++++++++ 4 files changed, 783 insertions(+), 43 deletions(-) create mode 100644 tests/SharpCompress.Test/LazyAsyncReadOnlyCollectionTests.cs create mode 100644 tests/SharpCompress.Test/LazyReadOnlyCollectionTests.cs diff --git a/src/SharpCompress/LazyAsyncReadOnlyCollection.cs b/src/SharpCompress/LazyAsyncReadOnlyCollection.cs index 85caf611..114bc71a 100644 --- a/src/SharpCompress/LazyAsyncReadOnlyCollection.cs +++ b/src/SharpCompress/LazyAsyncReadOnlyCollection.cs @@ -10,23 +10,23 @@ namespace SharpCompress; internal sealed class LazyAsyncReadOnlyCollection(IAsyncEnumerable source) : IAsyncEnumerable { - private readonly List backing = new(); - private readonly IAsyncEnumerator source = source.GetAsyncEnumerator(); - private bool fullyLoaded; + private readonly List _backing = new(); + private readonly IAsyncEnumerator _source = source.GetAsyncEnumerator(); + private bool _fullyLoaded; private class LazyLoader( LazyAsyncReadOnlyCollection lazyReadOnlyCollection, CancellationToken cancellationToken ) : IAsyncEnumerator { - private bool disposed; - private int index = -1; + private bool _disposed; + private int _index = -1; public ValueTask DisposeAsync() { - if (!disposed) + if (!_disposed) { - disposed = true; + _disposed = true; } return default; } @@ -34,27 +34,27 @@ internal sealed class LazyAsyncReadOnlyCollection(IAsyncEnumerable source) public async ValueTask MoveNextAsync() { cancellationToken.ThrowIfCancellationRequested(); - if (index + 1 < lazyReadOnlyCollection.backing.Count) + if (_index + 1 < lazyReadOnlyCollection._backing.Count) { - index++; + _index++; return true; } if ( - !lazyReadOnlyCollection.fullyLoaded - && await lazyReadOnlyCollection.source.MoveNextAsync() + !lazyReadOnlyCollection._fullyLoaded + && await lazyReadOnlyCollection._source.MoveNextAsync() ) { - lazyReadOnlyCollection.backing.Add(lazyReadOnlyCollection.source.Current); - index++; + lazyReadOnlyCollection._backing.Add(lazyReadOnlyCollection._source.Current); + _index++; return true; } - lazyReadOnlyCollection.fullyLoaded = true; + lazyReadOnlyCollection._fullyLoaded = true; return false; } #region IEnumerator Members - public T Current => lazyReadOnlyCollection.backing[index]; + public T Current => lazyReadOnlyCollection._backing[_index]; #endregion @@ -62,9 +62,9 @@ internal sealed class LazyAsyncReadOnlyCollection(IAsyncEnumerable source) public void Dispose() { - if (!disposed) + if (!_disposed) { - disposed = true; + _disposed = true; } } @@ -73,18 +73,18 @@ internal sealed class LazyAsyncReadOnlyCollection(IAsyncEnumerable source) internal async ValueTask EnsureFullyLoaded() { - if (!fullyLoaded) + if (!_fullyLoaded) { var loader = new LazyLoader(this, CancellationToken.None); while (await loader.MoveNextAsync()) { // Intentionally empty } - fullyLoaded = true; + _fullyLoaded = true; } } - internal IEnumerable GetLoaded() => backing; + internal IEnumerable GetLoaded() => _backing; #region ICollection Members diff --git a/src/SharpCompress/LazyReadOnlyCollection.cs b/src/SharpCompress/LazyReadOnlyCollection.cs index cc9cb3fd..eee60bc7 100644 --- a/src/SharpCompress/LazyReadOnlyCollection.cs +++ b/src/SharpCompress/LazyReadOnlyCollection.cs @@ -8,24 +8,24 @@ namespace SharpCompress; internal sealed class LazyReadOnlyCollection : ICollection { - private readonly List backing = new(); - private readonly IEnumerator source; - private bool fullyLoaded; + private readonly List _backing = new(); + private readonly IEnumerator _source; + private bool _fullyLoaded; - public LazyReadOnlyCollection(IEnumerable source) => this.source = source.GetEnumerator(); + public LazyReadOnlyCollection(IEnumerable source) => _source = source.GetEnumerator(); private class LazyLoader : IEnumerator { - private readonly LazyReadOnlyCollection lazyReadOnlyCollection; - private bool disposed; - private int index = -1; + private readonly LazyReadOnlyCollection _lazyReadOnlyCollection; + private bool _disposed; + private int _index = -1; internal LazyLoader(LazyReadOnlyCollection lazyReadOnlyCollection) => - this.lazyReadOnlyCollection = lazyReadOnlyCollection; + _lazyReadOnlyCollection = lazyReadOnlyCollection; #region IEnumerator Members - public T Current => lazyReadOnlyCollection.backing[index]; + public T Current => _lazyReadOnlyCollection._backing[_index]; #endregion @@ -33,9 +33,9 @@ internal sealed class LazyReadOnlyCollection : ICollection public void Dispose() { - if (!disposed) + if (!_disposed) { - disposed = true; + _disposed = true; } } @@ -47,18 +47,18 @@ internal sealed class LazyReadOnlyCollection : ICollection public bool MoveNext() { - if (index + 1 < lazyReadOnlyCollection.backing.Count) + if (_index + 1 < _lazyReadOnlyCollection._backing.Count) { - index++; + _index++; return true; } - if (!lazyReadOnlyCollection.fullyLoaded && lazyReadOnlyCollection.source.MoveNext()) + if (!_lazyReadOnlyCollection._fullyLoaded && _lazyReadOnlyCollection._source.MoveNext()) { - lazyReadOnlyCollection.backing.Add(lazyReadOnlyCollection.source.Current); - index++; + _lazyReadOnlyCollection._backing.Add(_lazyReadOnlyCollection._source.Current); + _index++; return true; } - lazyReadOnlyCollection.fullyLoaded = true; + _lazyReadOnlyCollection._fullyLoaded = true; return false; } @@ -69,14 +69,14 @@ internal sealed class LazyReadOnlyCollection : ICollection internal void EnsureFullyLoaded() { - if (!fullyLoaded) + if (!_fullyLoaded) { this.ForEach(x => { }); - fullyLoaded = true; + _fullyLoaded = true; } } - internal IEnumerable GetLoaded() => backing; + internal IEnumerable GetLoaded() => _backing; #region ICollection Members @@ -87,13 +87,13 @@ internal sealed class LazyReadOnlyCollection : ICollection public bool Contains(T item) { EnsureFullyLoaded(); - return backing.Contains(item); + return _backing.Contains(item); } public void CopyTo(T[] array, int arrayIndex) { EnsureFullyLoaded(); - backing.CopyTo(array, arrayIndex); + _backing.CopyTo(array, arrayIndex); } public int Count @@ -101,7 +101,7 @@ internal sealed class LazyReadOnlyCollection : ICollection get { EnsureFullyLoaded(); - return backing.Count; + return _backing.Count; } } diff --git a/tests/SharpCompress.Test/LazyAsyncReadOnlyCollectionTests.cs b/tests/SharpCompress.Test/LazyAsyncReadOnlyCollectionTests.cs new file mode 100644 index 00000000..aa9c2af4 --- /dev/null +++ b/tests/SharpCompress.Test/LazyAsyncReadOnlyCollectionTests.cs @@ -0,0 +1,358 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using Xunit; + +namespace SharpCompress.Test; + +public class LazyAsyncReadOnlyCollectionTests +{ + // Helper class to track how many times items are enumerated from the source + private class TrackingAsyncEnumerable : IAsyncEnumerable + { + private readonly List _items; + public int EnumerationCount { get; private set; } + public int ItemsRequestedCount { get; private set; } + + public TrackingAsyncEnumerable(params T[] items) + { + _items = new List(items); + } + + public IAsyncEnumerator GetAsyncEnumerator(CancellationToken cancellationToken = default) + { + EnumerationCount++; + return new TrackingEnumerator(this, cancellationToken); + } + + private class TrackingEnumerator : IAsyncEnumerator + { + private readonly TrackingAsyncEnumerable _parent; + private readonly CancellationToken _cancellationToken; + private int _index = -1; + + public TrackingEnumerator( + TrackingAsyncEnumerable parent, + CancellationToken cancellationToken + ) + { + _parent = parent; + _cancellationToken = cancellationToken; + } + + public T Current => _parent._items[_index]; + + public async ValueTask MoveNextAsync() + { + _cancellationToken.ThrowIfCancellationRequested(); + await Task.Yield(); // Simulate async behavior + _index++; + if (_index < _parent._items.Count) + { + _parent.ItemsRequestedCount++; + return true; + } + return false; + } + + public ValueTask DisposeAsync() => default; + } + } + + [Fact] + public async Task BasicEnumeration_IteratesThroughAllItems() + { + // Arrange + var source = new TrackingAsyncEnumerable(1, 2, 3, 4, 5); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act + var results = new List(); + await foreach (var item in collection) + { + results.Add(item); + } + + // Assert + Assert.Equal(5, results.Count); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, results); + Assert.Equal(1, source.EnumerationCount); + Assert.Equal(5, source.ItemsRequestedCount); + } + + [Fact] + public async Task MultipleEnumerations_UsesCachedBackingList() + { + // Arrange + var source = new TrackingAsyncEnumerable("a", "b", "c"); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act - First enumeration + var firstResults = new List(); + await foreach (var item in collection) + { + firstResults.Add(item); + } + + var itemsRequestedAfterFirst = source.ItemsRequestedCount; + + // Act - Second enumeration + var secondResults = new List(); + await foreach (var item in collection) + { + secondResults.Add(item); + } + + // Assert + Assert.Equal(firstResults, secondResults); + Assert.Equal(new[] { "a", "b", "c" }, secondResults); + + // Source should only be enumerated once + Assert.Equal(1, source.EnumerationCount); + + // Items should only be requested from source during first enumeration + Assert.Equal(itemsRequestedAfterFirst, source.ItemsRequestedCount); + } + + [Fact] + public async Task EnsureFullyLoaded_LoadsAllItemsIntoBackingList() + { + // Arrange + var source = new TrackingAsyncEnumerable(10, 20, 30, 40); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act + await collection.EnsureFullyLoaded(); + var loaded = collection.GetLoaded().ToList(); + + // Assert + Assert.Equal(4, loaded.Count); + Assert.Equal(new[] { 10, 20, 30, 40 }, loaded); + Assert.Equal(4, source.ItemsRequestedCount); + } + + [Fact] + public async Task GetLoaded_ReturnsOnlyLoadedItemsBeforeFullEnumeration() + { + // Arrange + var source = new TrackingAsyncEnumerable(1, 2, 3, 4, 5); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act - Partially enumerate (only first 2 items) + var enumerator = collection.GetAsyncEnumerator(); + await enumerator.MoveNextAsync(); // Load item 1 + await enumerator.MoveNextAsync(); // Load item 2 + + var loadedItems = collection.GetLoaded().ToList(); + + // Continue enumeration + await enumerator.MoveNextAsync(); // Load item 3 + var loadedItemsAfter = collection.GetLoaded().ToList(); + + await enumerator.DisposeAsync(); + + // Assert + Assert.Equal(2, loadedItems.Count); + Assert.Equal(new[] { 1, 2 }, loadedItems); + + Assert.Equal(3, loadedItemsAfter.Count); + Assert.Equal(new[] { 1, 2, 3 }, loadedItemsAfter); + } + + [Fact] + public async Task CancellationToken_PassedToGetAsyncEnumerator_HonorsToken() + { + // Arrange + var cts = new CancellationTokenSource(); + var source = new TrackingAsyncEnumerable(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act & Assert + var results = new List(); + var exception = await Assert.ThrowsAsync(async () => + { + await foreach (var item in collection.WithCancellation(cts.Token)) + { + results.Add(item); + if (item == 3) + { + cts.Cancel(); + } + } + }); + + Assert.Equal(3, results.Count); + Assert.Equal(new[] { 1, 2, 3 }, results); + } + + [Fact] + public async Task CancellationDuringMoveNextAsync_ThrowsOperationCanceledException() + { + // Arrange + var cts = new CancellationTokenSource(); + var source = CreateDelayedAsyncEnumerable( + new[] { 1, 2, 3, 4, 5 }, + TimeSpan.FromMilliseconds(50) + ); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act & Assert + await Assert.ThrowsAsync(async () => + { + var enumerator = collection.GetAsyncEnumerator(cts.Token); + await enumerator.MoveNextAsync(); + await enumerator.MoveNextAsync(); + + cts.Cancel(); + + await enumerator.MoveNextAsync(); // Should throw + }); + } + + [Fact] + public async Task EmptySourceEnumerable_ReturnsNoItems() + { + // Arrange + var source = new TrackingAsyncEnumerable(); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act + var results = new List(); + await foreach (var item in collection) + { + results.Add(item); + } + + // Assert + Assert.Empty(results); + Assert.Equal(1, source.EnumerationCount); + } + + [Fact] + public async Task SingleItemSourceEnumerable_ReturnsSingleItem() + { + // Arrange + var source = new TrackingAsyncEnumerable("only"); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act + var results = new List(); + await foreach (var item in collection) + { + results.Add(item); + } + + // Assert + Assert.Single(results); + Assert.Equal("only", results[0]); + } + + [Fact] + public async Task PartialEnumeration_ThenGetLoaded_ReturnsOnlyEnumeratedItems() + { + // Arrange + var source = new TrackingAsyncEnumerable(10, 20, 30, 40, 50); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act - Enumerate only first 3 items + await using (var enumerator = collection.GetAsyncEnumerator()) + { + var hasMore = await enumerator.MoveNextAsync(); + Assert.True(hasMore); + hasMore = await enumerator.MoveNextAsync(); + Assert.True(hasMore); + hasMore = await enumerator.MoveNextAsync(); + Assert.True(hasMore); + } + + var loadedItems = collection.GetLoaded().ToList(); + + // Assert + Assert.Equal(3, loadedItems.Count); + Assert.Equal(new[] { 10, 20, 30 }, loadedItems); + Assert.Equal(3, source.ItemsRequestedCount); + } + + [Fact] + public async Task ConcurrentEnumerations_ShareBackingList() + { + // Arrange + var source = new TrackingAsyncEnumerable(1, 2, 3, 4, 5); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act - Fully load the collection first, then enumerate from two threads + await collection.EnsureFullyLoaded(); + + var task1 = Task.Run(async () => + { + var results = new List(); + await foreach (var item in collection) + { + results.Add(item); + await Task.Delay(5); + } + return results; + }); + + var task2 = Task.Run(async () => + { + var results = new List(); + await foreach (var item in collection) + { + results.Add(item); + await Task.Delay(5); + } + return results; + }); + + var results1 = await task1; + var results2 = await task2; + + // Assert - Both enumerations should see all items from the shared backing list + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, results1); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, results2); + Assert.Equal(5, source.ItemsRequestedCount); + } + + [Fact] + public async Task DisposeAsync_OnLazyLoader_CompletesSuccessfully() + { + // Arrange + var source = new TrackingAsyncEnumerable(1, 2, 3); + var collection = new LazyAsyncReadOnlyCollection(source); + + // Act + await using (var enumerator = collection.GetAsyncEnumerator()) + { + await enumerator.MoveNextAsync(); + var firstItem = enumerator.Current; + Assert.Equal(1, firstItem); + + // Dispose is called automatically by await using + } + + // Assert - should be able to enumerate again after disposal + var results = new List(); + await foreach (var item in collection) + { + results.Add(item); + } + + Assert.Equal(new[] { 1, 2, 3 }, results); + } + + // Helper method to create an async enumerable with delays + private static async IAsyncEnumerable CreateDelayedAsyncEnumerable( + IEnumerable items, + TimeSpan delay + ) + { + foreach (var item in items) + { + await Task.Delay(delay); + yield return item; + } + } +} diff --git a/tests/SharpCompress.Test/LazyReadOnlyCollectionTests.cs b/tests/SharpCompress.Test/LazyReadOnlyCollectionTests.cs new file mode 100644 index 00000000..e71dc3eb --- /dev/null +++ b/tests/SharpCompress.Test/LazyReadOnlyCollectionTests.cs @@ -0,0 +1,382 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using Xunit; + +namespace SharpCompress.Test; + +public class LazyReadOnlyCollectionTests +{ + // Helper class to track how many times items are enumerated from the source + private class TrackingEnumerable : IEnumerable + { + private readonly List _items; + public int EnumerationCount { get; private set; } + public int ItemsRequestedCount { get; private set; } + + public TrackingEnumerable(params T[] items) + { + _items = new List(items); + } + + public IEnumerator GetEnumerator() + { + EnumerationCount++; + return new TrackingEnumerator(this); + } + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() => + GetEnumerator(); + + private class TrackingEnumerator : IEnumerator + { + private readonly TrackingEnumerable _parent; + private int _index = -1; + + public TrackingEnumerator(TrackingEnumerable parent) + { + _parent = parent; + } + + public T Current => _parent._items[_index]; + + object? System.Collections.IEnumerator.Current => Current; + + public bool MoveNext() + { + _index++; + if (_index < _parent._items.Count) + { + _parent.ItemsRequestedCount++; + return true; + } + return false; + } + + public void Reset() => throw new NotSupportedException(); + + public void Dispose() { } + } + } + + [Fact] + public void BasicEnumeration_IteratesThroughAllItems() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3, 4, 5); + var collection = new LazyReadOnlyCollection(source); + + // Act + var results = new List(); + foreach (var item in collection) + { + results.Add(item); + } + + // Assert + Assert.Equal(5, results.Count); + Assert.Equal(new[] { 1, 2, 3, 4, 5 }, results); + Assert.Equal(1, source.EnumerationCount); + Assert.Equal(5, source.ItemsRequestedCount); + } + + [Fact] + public void MultipleEnumerations_UsesCachedBackingList() + { + // Arrange + var source = new TrackingEnumerable("a", "b", "c"); + var collection = new LazyReadOnlyCollection(source); + + // Act - First enumeration + var firstResults = new List(); + foreach (var item in collection) + { + firstResults.Add(item); + } + + var itemsRequestedAfterFirst = source.ItemsRequestedCount; + + // Act - Second enumeration + var secondResults = new List(); + foreach (var item in collection) + { + secondResults.Add(item); + } + + // Assert + Assert.Equal(firstResults, secondResults); + Assert.Equal(new[] { "a", "b", "c" }, secondResults); + + // Source should only be enumerated once + Assert.Equal(1, source.EnumerationCount); + + // Items should only be requested from source during first enumeration + Assert.Equal(itemsRequestedAfterFirst, source.ItemsRequestedCount); + } + + [Fact] + public void EnsureFullyLoaded_LoadsAllItemsIntoBackingList() + { + // Arrange + var source = new TrackingEnumerable(10, 20, 30, 40); + var collection = new LazyReadOnlyCollection(source); + + // Act + collection.EnsureFullyLoaded(); + var loaded = collection.GetLoaded().ToList(); + + // Assert + Assert.Equal(4, loaded.Count); + Assert.Equal(new[] { 10, 20, 30, 40 }, loaded); + Assert.Equal(4, source.ItemsRequestedCount); + } + + [Fact] + public void GetLoaded_ReturnsOnlyLoadedItemsBeforeFullEnumeration() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3, 4, 5); + var collection = new LazyReadOnlyCollection(source); + + // Act - Partially enumerate (only first 2 items) + using var enumerator = collection.GetEnumerator(); + enumerator.MoveNext(); // Load item 1 + enumerator.MoveNext(); // Load item 2 + + var loadedItems = collection.GetLoaded().ToList(); + + // Continue enumeration + enumerator.MoveNext(); // Load item 3 + var loadedItemsAfter = collection.GetLoaded().ToList(); + + // Assert + Assert.Equal(2, loadedItems.Count); + Assert.Equal(new[] { 1, 2 }, loadedItems); + + Assert.Equal(3, loadedItemsAfter.Count); + Assert.Equal(new[] { 1, 2, 3 }, loadedItemsAfter); + } + + [Fact] + public void Count_TriggersFullLoad() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3, 4, 5); + var collection = new LazyReadOnlyCollection(source); + + // Act + var count = collection.Count; + + // Assert + Assert.Equal(5, count); + Assert.Equal(5, source.ItemsRequestedCount); + Assert.Equal(5, collection.GetLoaded().Count()); + } + + [Fact] + public void Contains_TriggersFullLoadAndSearches() + { + // Arrange + var source = new TrackingEnumerable("apple", "banana", "cherry"); + var collection = new LazyReadOnlyCollection(source); + + // Act + var containsBanana = collection.Contains("banana"); + var containsOrange = collection.Contains("orange"); + + // Assert + Assert.True(containsBanana); + Assert.False(containsOrange); + Assert.Equal(3, source.ItemsRequestedCount); + Assert.Equal(3, collection.GetLoaded().Count()); + } + + [Fact] + public void CopyTo_TriggersFullLoadAndCopiesArray() + { + // Arrange + var source = new TrackingEnumerable(10, 20, 30); + var collection = new LazyReadOnlyCollection(source); + var array = new int[5]; + + // Act + collection.CopyTo(array, 1); + + // Assert + Assert.Equal(0, array[0]); + Assert.Equal(10, array[1]); + Assert.Equal(20, array[2]); + Assert.Equal(30, array[3]); + Assert.Equal(0, array[4]); + Assert.Equal(3, source.ItemsRequestedCount); + } + + [Fact] + public void EmptySourceEnumerable_ReturnsNoItems() + { + // Arrange + var source = new TrackingEnumerable(); + var collection = new LazyReadOnlyCollection(source); + + // Act + var results = new List(); + foreach (var item in collection) + { + results.Add(item); + } + + // Assert + Assert.Empty(results); + Assert.Equal(0, collection.Count); + Assert.Equal(1, source.EnumerationCount); + } + + [Fact] + public void SingleItemSourceEnumerable_ReturnsSingleItem() + { + // Arrange + var source = new TrackingEnumerable("only"); + var collection = new LazyReadOnlyCollection(source); + + // Act + var results = new List(); + foreach (var item in collection) + { + results.Add(item); + } + + // Assert + Assert.Single(results); + Assert.Equal("only", results[0]); + Assert.Equal(1, collection.Count); + } + + [Fact] + public void PartialEnumeration_ThenGetLoaded_ReturnsOnlyEnumeratedItems() + { + // Arrange + var source = new TrackingEnumerable(10, 20, 30, 40, 50); + var collection = new LazyReadOnlyCollection(source); + + // Act - Enumerate only first 3 items + using (var enumerator = collection.GetEnumerator()) + { + var hasMore = enumerator.MoveNext(); + Assert.True(hasMore); + hasMore = enumerator.MoveNext(); + Assert.True(hasMore); + hasMore = enumerator.MoveNext(); + Assert.True(hasMore); + } + + var loadedItems = collection.GetLoaded().ToList(); + + // Assert + Assert.Equal(3, loadedItems.Count); + Assert.Equal(new[] { 10, 20, 30 }, loadedItems); + Assert.Equal(3, source.ItemsRequestedCount); + } + + [Fact] + public void Reset_ThrowsNotSupportedException() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3); + var collection = new LazyReadOnlyCollection(source); + + // Act & Assert + using var enumerator = collection.GetEnumerator(); + enumerator.MoveNext(); + Assert.Throws(() => enumerator.Reset()); + } + + [Fact] + public void Dispose_OnEnumerator_CompletesSuccessfully() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3); + var collection = new LazyReadOnlyCollection(source); + + // Act + using (var enumerator = collection.GetEnumerator()) + { + enumerator.MoveNext(); + var firstItem = enumerator.Current; + Assert.Equal(1, firstItem); + + // Dispose is called automatically by using + } + + // Assert - should be able to enumerate again after disposal + var results = new List(); + foreach (var item in collection) + { + results.Add(item); + } + + Assert.Equal(new[] { 1, 2, 3 }, results); + } + + [Fact] + public void IsReadOnly_ReturnsTrue() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3); + var collection = new LazyReadOnlyCollection(source); + + // Act & Assert + Assert.True(collection.IsReadOnly); + } + + [Fact] + public void Add_ThrowsNotSupportedException() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3); + var collection = new LazyReadOnlyCollection(source); + + // Act & Assert + Assert.Throws(() => collection.Add(4)); + } + + [Fact] + public void Clear_ThrowsNotSupportedException() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3); + var collection = new LazyReadOnlyCollection(source); + + // Act & Assert + Assert.Throws(() => collection.Clear()); + } + + [Fact] + public void Remove_ThrowsNotSupportedException() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3); + var collection = new LazyReadOnlyCollection(source); + + // Act & Assert + Assert.Throws(() => collection.Remove(1)); + } + + [Fact] + public void NonGenericEnumerator_WorksCorrectly() + { + // Arrange + var source = new TrackingEnumerable(1, 2, 3); + var collection = new LazyReadOnlyCollection(source); + + // Act - Use non-generic IEnumerator + var results = new List(); + var enumerable = (System.Collections.IEnumerable)collection; + foreach (var item in enumerable) + { + results.Add((int)item); + } + + // Assert + Assert.Equal(new[] { 1, 2, 3 }, results); + } +} From 8d34f88ca62127993d5507bae20106e63f2eecbb Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 13 Jan 2026 16:42:54 +0000 Subject: [PATCH 23/36] fix up gitignore --- .gitignore | 4 ++-- SharpCompress.sln | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 6c6a863d..17b16704 100644 --- a/.gitignore +++ b/.gitignore @@ -4,8 +4,8 @@ _ReSharper.SharpCompress/ bin/ *.suo *.user -TestArchives/Scratch/ -TestArchives/Scratch2/ +tests/TestArchives/Scratch/ +tests/TestArchives/Scratch2/ TestResults/ *.nupkg packages/*/ diff --git a/SharpCompress.sln b/SharpCompress.sln index 353e73d7..af132f4c 100644 --- a/SharpCompress.sln +++ b/SharpCompress.sln @@ -18,6 +18,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Config", "Config", "{CDB425 Directory.Build.props = Directory.Build.props global.json = global.json .editorconfig = .editorconfig + .gitignore = .gitignore Directory.Packages.props = Directory.Packages.props NuGet.config = NuGet.config .github\workflows\nuget-release.yml = .github\workflows\nuget-release.yml From c32f4b4f2a07c11351aa2537648720c8124ebede Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 14 Jan 2026 08:33:49 +0000 Subject: [PATCH 24/36] fix test reference --- Directory.Packages.props | 2 ++ src/SharpCompress/packages.lock.json | 6 +++--- tests/SharpCompress.Test/SharpCompress.Test.csproj | 3 +++ tests/SharpCompress.Test/packages.lock.json | 12 ++++++++++++ 4 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 7d532c3a..d5994bd7 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -5,7 +5,9 @@ + + diff --git a/src/SharpCompress/packages.lock.json b/src/SharpCompress/packages.lock.json index 032c15c4..41325333 100644 --- a/src/SharpCompress/packages.lock.json +++ b/src/SharpCompress/packages.lock.json @@ -216,9 +216,9 @@ "net10.0": { "Microsoft.NET.ILLink.Tasks": { "type": "Direct", - "requested": "[10.0.1, )", - "resolved": "10.0.1", - "contentHash": "ISahzLHsHY7vrwqr2p1YWZ+gsxoBRtH7gWRDK8fDUst9pp2He0GiesaqEfeX0V8QMCJM3eNEHGGpnIcPjFo2NQ==" + "requested": "[10.0.0, )", + "resolved": "10.0.0", + "contentHash": "kICGrGYEzCNI3wPzfEXcwNHgTvlvVn9yJDhSdRK+oZQy4jvYH529u7O0xf5ocQKzOMjfS07+3z9PKRIjrFMJDA==" }, "Microsoft.NETFramework.ReferenceAssemblies": { "type": "Direct", diff --git a/tests/SharpCompress.Test/SharpCompress.Test.csproj b/tests/SharpCompress.Test/SharpCompress.Test.csproj index 0399d78c..c16a1581 100644 --- a/tests/SharpCompress.Test/SharpCompress.Test.csproj +++ b/tests/SharpCompress.Test/SharpCompress.Test.csproj @@ -24,4 +24,7 @@ + + + diff --git a/tests/SharpCompress.Test/packages.lock.json b/tests/SharpCompress.Test/packages.lock.json index 3e770308..baea090e 100644 --- a/tests/SharpCompress.Test/packages.lock.json +++ b/tests/SharpCompress.Test/packages.lock.json @@ -45,6 +45,12 @@ "resolved": "17.14.15", "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" }, + "Mono.Posix.NETStandard": { + "type": "Direct", + "requested": "[1.0.0, )", + "resolved": "1.0.0", + "contentHash": "vSN/L1uaVwKsiLa95bYu2SGkF0iY3xMblTfxc8alSziPuVfJpj3geVqHGAA75J7cZkMuKpFVikz82Lo6y6LLdA==" + }, "xunit": { "type": "Direct", "requested": "[2.9.3, )", @@ -258,6 +264,12 @@ "resolved": "17.14.15", "contentHash": "mXQPJsbuUD2ydq4/ffd8h8tSOFCXec+2xJOVNCvXjuMOq/+5EKHq3D2m2MC2+nUaXeFMSt66VS/J4HdKBixgcw==" }, + "Mono.Posix.NETStandard": { + "type": "Direct", + "requested": "[1.0.0, )", + "resolved": "1.0.0", + "contentHash": "vSN/L1uaVwKsiLa95bYu2SGkF0iY3xMblTfxc8alSziPuVfJpj3geVqHGAA75J7cZkMuKpFVikz82Lo6y6LLdA==" + }, "xunit": { "type": "Direct", "requested": "[2.9.3, )", From 3677b4b193f6f99fd45631eb0cbf9ec13cbad4ea Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 14 Jan 2026 08:57:12 +0000 Subject: [PATCH 25/36] add default interfaces to enforce consistency --- .../Archives/GZip/GZipArchive.Factory.cs | 29 ++++++++++++--- .../Archives/IArchiveOpenable.cs | 36 +++++++++++++++++++ .../Archives/IMultiArchiveOpenable.cs | 35 ++++++++++++++++++ .../Archives/Rar/RarArchive.Factory.cs | 15 ++++++++ .../SevenZip/SevenZipArchive.Factory.cs | 15 ++++++++ .../Archives/Tar/TarArchive.Factory.cs | 8 +++-- .../Archives/Zip/ZipArchive.Factory.cs | 6 ++-- 7 files changed, 136 insertions(+), 8 deletions(-) create mode 100644 src/SharpCompress/Archives/IArchiveOpenable.cs create mode 100644 src/SharpCompress/Archives/IMultiArchiveOpenable.cs diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs index ca6cd6a3..b2053faa 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs @@ -15,14 +15,32 @@ using SharpCompress.Writers.GZip; namespace SharpCompress.Archives.GZip; public partial class GZipArchive +#if NET8_0_OR_GREATER + : IArchiveOpenable, + IMultiArchiveOpenable +#endif { - public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IWritableAsyncArchive OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IWritableAsyncArchive)Open( + new FileInfo(path), + readerOptions ?? new ReaderOptions() + ); + } + + public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } - public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); return new GZipArchive( @@ -34,7 +52,7 @@ public partial class GZipArchive ); } - public static IArchive Open( + public static IWritableArchive Open( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -50,7 +68,10 @@ public partial class GZipArchive ); } - public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IWritableArchive Open( + IEnumerable streams, + ReaderOptions? readerOptions = null + ) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); diff --git a/src/SharpCompress/Archives/IArchiveOpenable.cs b/src/SharpCompress/Archives/IArchiveOpenable.cs new file mode 100644 index 00000000..cf606a65 --- /dev/null +++ b/src/SharpCompress/Archives/IArchiveOpenable.cs @@ -0,0 +1,36 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Readers; + +namespace SharpCompress.Archives; + +public interface IArchiveOpenable + where TSync : IArchive + where TASync : IAsyncArchive +{ + public static abstract TSync Open(string filePath, ReaderOptions? readerOptions = null); + + public static abstract TSync Open(FileInfo fileInfo, ReaderOptions? readerOptions = null); + + public static abstract TSync Open(Stream stream, ReaderOptions? readerOptions = null); + + public static abstract TASync OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ); + + public static abstract TASync OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ); + + public static abstract TASync OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ); +} +#endif diff --git a/src/SharpCompress/Archives/IMultiArchiveOpenable.cs b/src/SharpCompress/Archives/IMultiArchiveOpenable.cs new file mode 100644 index 00000000..4c1e9d88 --- /dev/null +++ b/src/SharpCompress/Archives/IMultiArchiveOpenable.cs @@ -0,0 +1,35 @@ +#if NET8_0_OR_GREATER +using System.Collections.Generic; +using System.IO; +using System.Threading; +using SharpCompress.Readers; + +namespace SharpCompress.Archives; + +public interface IMultiArchiveOpenable + where TSync : IArchive + where TASync : IAsyncArchive +{ + public static abstract TSync Open( + IEnumerable fileInfos, + ReaderOptions? readerOptions = null + ); + + public static abstract TSync Open( + IEnumerable streams, + ReaderOptions? readerOptions = null + ); + + public static abstract TASync OpenAsync( + IReadOnlyList streams, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ); + + public static abstract TASync OpenAsync( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ); +} +#endif \ No newline at end of file diff --git a/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs b/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs index f5dc8383..6ede4790 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs @@ -14,7 +14,22 @@ using SharpCompress.Readers.Rar; namespace SharpCompress.Archives.Rar; public partial class RarArchive +#if NET8_0_OR_GREATER + : IArchiveOpenable, + IMultiArchiveOpenable +#endif { + public static IRarAsyncArchive OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IRarAsyncArchive)Open(new FileInfo(path), readerOptions); + } + public static IRarArchive Open(string filePath, ReaderOptions? options = null) { filePath.NotNullOrEmpty(nameof(filePath)); diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs index 9aaf1ae5..3b39cff4 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs @@ -13,7 +13,22 @@ using SharpCompress.Readers; namespace SharpCompress.Archives.SevenZip; public partial class SevenZipArchive +#if NET8_0_OR_GREATER + : IArchiveOpenable, + IMultiArchiveOpenable +#endif { + public static IAsyncArchive OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty("path"); + return (IAsyncArchive)Open(new FileInfo(path), readerOptions ?? new ReaderOptions()); + } + public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty("filePath"); diff --git a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs index 528c0954..adf12909 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs @@ -15,6 +15,10 @@ using SharpCompress.Writers.Tar; namespace SharpCompress.Archives.Tar; public partial class TarArchive +#if NET8_0_OR_GREATER + : IArchiveOpenable, + IMultiArchiveOpenable +#endif { public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) { @@ -91,13 +95,13 @@ public partial class TarArchive } public static IWritableAsyncArchive OpenAsync( - string file, + string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(new FileInfo(file), readerOptions); + return (IWritableAsyncArchive)Open(new FileInfo(path), readerOptions); } public static IWritableAsyncArchive OpenAsync( diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs index a4061db1..770fe048 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs @@ -9,12 +9,14 @@ using SharpCompress.Common.Zip; using SharpCompress.Common.Zip.Headers; using SharpCompress.IO; using SharpCompress.Readers; -using SharpCompress.Writers; -using SharpCompress.Writers.Zip; namespace SharpCompress.Archives.Zip; public partial class ZipArchive +#if NET8_0_OR_GREATER + : IArchiveOpenable, + IMultiArchiveOpenable +#endif { public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) { From 775efa1b26e15421fa4182f07ed695dcb7c547f0 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 14 Jan 2026 09:23:18 +0000 Subject: [PATCH 26/36] Reader open factories --- .../Archives/IMultiArchiveOpenable.cs | 2 +- .../Readers/Ace/AceReader.Factory.cs | 53 +++++++++++++++++++ src/SharpCompress/Readers/Ace/AceReader.cs | 2 +- .../Readers/Arc/ArcReader.Factory.cs | 53 +++++++++++++++++++ src/SharpCompress/Readers/Arc/ArcReader.cs | 2 +- .../Readers/Arj/ArjReader.Factory.cs | 53 +++++++++++++++++++ src/SharpCompress/Readers/Arj/ArjReader.cs | 2 +- .../Readers/GZip/GZipReader.Factory.cs | 53 +++++++++++++++++++ src/SharpCompress/Readers/GZip/GZipReader.cs | 2 +- src/SharpCompress/Readers/IReaderOpenable.cs | 33 ++++++++++++ .../Readers/Rar/RarReader.Factory.cs | 41 ++++++++++++++ src/SharpCompress/Readers/Rar/RarReader.cs | 2 +- .../Readers/Tar/TarReader.Factory.cs | 53 +++++++++++++++++++ src/SharpCompress/Readers/Tar/TarReader.cs | 2 +- .../Readers/Zip/ZipReader.Factory.cs | 53 +++++++++++++++++++ src/SharpCompress/Readers/Zip/ZipReader.cs | 2 +- src/SharpCompress/Writers/IWriterOpenable.cs | 6 +++ 17 files changed, 406 insertions(+), 8 deletions(-) create mode 100644 src/SharpCompress/Readers/Ace/AceReader.Factory.cs create mode 100644 src/SharpCompress/Readers/Arc/ArcReader.Factory.cs create mode 100644 src/SharpCompress/Readers/Arj/ArjReader.Factory.cs create mode 100644 src/SharpCompress/Readers/GZip/GZipReader.Factory.cs create mode 100644 src/SharpCompress/Readers/IReaderOpenable.cs create mode 100644 src/SharpCompress/Readers/Rar/RarReader.Factory.cs create mode 100644 src/SharpCompress/Readers/Tar/TarReader.Factory.cs create mode 100644 src/SharpCompress/Readers/Zip/ZipReader.Factory.cs create mode 100644 src/SharpCompress/Writers/IWriterOpenable.cs diff --git a/src/SharpCompress/Archives/IMultiArchiveOpenable.cs b/src/SharpCompress/Archives/IMultiArchiveOpenable.cs index 4c1e9d88..06c5bdd3 100644 --- a/src/SharpCompress/Archives/IMultiArchiveOpenable.cs +++ b/src/SharpCompress/Archives/IMultiArchiveOpenable.cs @@ -32,4 +32,4 @@ public interface IMultiArchiveOpenable CancellationToken cancellationToken = default ); } -#endif \ No newline at end of file +#endif diff --git a/src/SharpCompress/Readers/Ace/AceReader.Factory.cs b/src/SharpCompress/Readers/Ace/AceReader.Factory.cs new file mode 100644 index 00000000..e65c18de --- /dev/null +++ b/src/SharpCompress/Readers/Ace/AceReader.Factory.cs @@ -0,0 +1,53 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Readers.Ace; + +public partial class AceReader : IReaderOpenable +{ + public static IAsyncReader OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IAsyncReader)Open(new FileInfo(path), readerOptions); + } + + public static IAsyncReader OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(stream, readerOptions); + } + + public static IAsyncReader OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(fileInfo, readerOptions); + } + + public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions); + } + + public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return Open(fileInfo.OpenRead(), readerOptions); + } +} +#endif diff --git a/src/SharpCompress/Readers/Ace/AceReader.cs b/src/SharpCompress/Readers/Ace/AceReader.cs index c74ad401..12ad92d3 100644 --- a/src/SharpCompress/Readers/Ace/AceReader.cs +++ b/src/SharpCompress/Readers/Ace/AceReader.cs @@ -23,7 +23,7 @@ namespace SharpCompress.Readers.Ace /// - Recovery record support /// - Additional header flags /// - public abstract class AceReader : AbstractReader + public abstract partial class AceReader : AbstractReader { private readonly IArchiveEncoding _archiveEncoding; diff --git a/src/SharpCompress/Readers/Arc/ArcReader.Factory.cs b/src/SharpCompress/Readers/Arc/ArcReader.Factory.cs new file mode 100644 index 00000000..e8052ddc --- /dev/null +++ b/src/SharpCompress/Readers/Arc/ArcReader.Factory.cs @@ -0,0 +1,53 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Readers.Arc; + +public partial class ArcReader : IReaderOpenable +{ + public static IAsyncReader OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IAsyncReader)Open(new FileInfo(path), readerOptions); + } + + public static IAsyncReader OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(stream, readerOptions); + } + + public static IAsyncReader OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(fileInfo, readerOptions); + } + + public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions); + } + + public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return Open(fileInfo.OpenRead(), readerOptions); + } +} +#endif diff --git a/src/SharpCompress/Readers/Arc/ArcReader.cs b/src/SharpCompress/Readers/Arc/ArcReader.cs index 9f1eb68e..a797ce2d 100644 --- a/src/SharpCompress/Readers/Arc/ArcReader.cs +++ b/src/SharpCompress/Readers/Arc/ArcReader.cs @@ -9,7 +9,7 @@ using SharpCompress.Common.Arc; namespace SharpCompress.Readers.Arc { - public class ArcReader : AbstractReader + public partial class ArcReader : AbstractReader { private ArcReader(Stream stream, ReaderOptions options) : base(options, ArchiveType.Arc) => Volume = new ArcVolume(stream, options, 0); diff --git a/src/SharpCompress/Readers/Arj/ArjReader.Factory.cs b/src/SharpCompress/Readers/Arj/ArjReader.Factory.cs new file mode 100644 index 00000000..9d9cefc0 --- /dev/null +++ b/src/SharpCompress/Readers/Arj/ArjReader.Factory.cs @@ -0,0 +1,53 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Readers.Arj; + +public partial class ArjReader : IReaderOpenable +{ + public static IAsyncReader OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IAsyncReader)Open(new FileInfo(path), readerOptions); + } + + public static IAsyncReader OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(stream, readerOptions); + } + + public static IAsyncReader OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(fileInfo, readerOptions); + } + + public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions); + } + + public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return Open(fileInfo.OpenRead(), readerOptions); + } +} +#endif diff --git a/src/SharpCompress/Readers/Arj/ArjReader.cs b/src/SharpCompress/Readers/Arj/ArjReader.cs index 831daf05..f7d8d6e5 100644 --- a/src/SharpCompress/Readers/Arj/ArjReader.cs +++ b/src/SharpCompress/Readers/Arj/ArjReader.cs @@ -8,7 +8,7 @@ using SharpCompress.Readers.Rar; namespace SharpCompress.Readers.Arj { - public abstract class ArjReader : AbstractReader + public abstract partial class ArjReader : AbstractReader { internal ArjReader(ReaderOptions options) : base(options, ArchiveType.Arj) { } diff --git a/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs b/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs new file mode 100644 index 00000000..02344555 --- /dev/null +++ b/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs @@ -0,0 +1,53 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Readers.GZip; + +public partial class GZipReader : IReaderOpenable +{ + public static IAsyncReader OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IAsyncReader)Open(new FileInfo(path), readerOptions); + } + + public static IAsyncReader OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(stream, readerOptions); + } + + public static IAsyncReader OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(fileInfo, readerOptions); + } + + public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions); + } + + public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return Open(fileInfo.OpenRead(), readerOptions); + } +} +#endif diff --git a/src/SharpCompress/Readers/GZip/GZipReader.cs b/src/SharpCompress/Readers/GZip/GZipReader.cs index c1fd8447..f7c3cf15 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.cs @@ -5,7 +5,7 @@ using SharpCompress.Common.GZip; namespace SharpCompress.Readers.GZip; -public class GZipReader : AbstractReader +public partial class GZipReader : AbstractReader { private GZipReader(Stream stream, ReaderOptions options) : base(options, ArchiveType.GZip) => Volume = new GZipVolume(stream, options, 0); diff --git a/src/SharpCompress/Readers/IReaderOpenable.cs b/src/SharpCompress/Readers/IReaderOpenable.cs new file mode 100644 index 00000000..c55874cb --- /dev/null +++ b/src/SharpCompress/Readers/IReaderOpenable.cs @@ -0,0 +1,33 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; + +namespace SharpCompress.Readers; + +public interface IReaderOpenable +{ + public static abstract IReader Open(string filePath, ReaderOptions? readerOptions = null); + + public static abstract IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null); + + public static abstract IReader Open(Stream stream, ReaderOptions? readerOptions = null); + + public static abstract IAsyncReader OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ); + + public static abstract IAsyncReader OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ); + + public static abstract IAsyncReader OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ); +} +#endif diff --git a/src/SharpCompress/Readers/Rar/RarReader.Factory.cs b/src/SharpCompress/Readers/Rar/RarReader.Factory.cs new file mode 100644 index 00000000..35a1be5d --- /dev/null +++ b/src/SharpCompress/Readers/Rar/RarReader.Factory.cs @@ -0,0 +1,41 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Readers.Rar; + +public partial class RarReader : IReaderOpenable +{ + public static IAsyncReader OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IAsyncReader)Open(new FileInfo(path), readerOptions); + } + + public static IAsyncReader OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(stream, readerOptions); + } + + public static IAsyncReader OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(fileInfo, readerOptions); + } +} +#endif diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index a3976fe5..1a127b69 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -11,7 +11,7 @@ namespace SharpCompress.Readers.Rar; /// /// This class faciliates Reading a Rar Archive in a non-seekable forward-only manner /// -public abstract class RarReader : AbstractReader +public abstract partial class RarReader : AbstractReader { private bool _disposed; private RarVolume? volume; diff --git a/src/SharpCompress/Readers/Tar/TarReader.Factory.cs b/src/SharpCompress/Readers/Tar/TarReader.Factory.cs new file mode 100644 index 00000000..b009afe4 --- /dev/null +++ b/src/SharpCompress/Readers/Tar/TarReader.Factory.cs @@ -0,0 +1,53 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Readers.Tar; + +public partial class TarReader : IReaderOpenable +{ + public static IAsyncReader OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IAsyncReader)Open(new FileInfo(path), readerOptions); + } + + public static IAsyncReader OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(stream, readerOptions); + } + + public static IAsyncReader OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(fileInfo, readerOptions); + } + + public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions); + } + + public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return Open(fileInfo.OpenRead(), readerOptions); + } +} +#endif diff --git a/src/SharpCompress/Readers/Tar/TarReader.cs b/src/SharpCompress/Readers/Tar/TarReader.cs index e1375523..54444954 100644 --- a/src/SharpCompress/Readers/Tar/TarReader.cs +++ b/src/SharpCompress/Readers/Tar/TarReader.cs @@ -16,7 +16,7 @@ using SharpCompress.IO; namespace SharpCompress.Readers.Tar; -public class TarReader : AbstractReader +public partial class TarReader : AbstractReader { private readonly CompressionType compressionType; diff --git a/src/SharpCompress/Readers/Zip/ZipReader.Factory.cs b/src/SharpCompress/Readers/Zip/ZipReader.Factory.cs new file mode 100644 index 00000000..c244e5de --- /dev/null +++ b/src/SharpCompress/Readers/Zip/ZipReader.Factory.cs @@ -0,0 +1,53 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Readers.Zip; + +public partial class ZipReader : IReaderOpenable +{ + public static IAsyncReader OpenAsync( + string path, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + path.NotNullOrEmpty(nameof(path)); + return (IAsyncReader)Open(new FileInfo(path), readerOptions); + } + + public static IAsyncReader OpenAsync( + Stream stream, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(stream, readerOptions); + } + + public static IAsyncReader OpenAsync( + FileInfo fileInfo, + ReaderOptions? readerOptions = null, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncReader)Open(fileInfo, readerOptions); + } + + public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), readerOptions); + } + + public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + { + fileInfo.NotNull(nameof(fileInfo)); + return Open(fileInfo.OpenRead(), readerOptions); + } +} +#endif diff --git a/src/SharpCompress/Readers/Zip/ZipReader.cs b/src/SharpCompress/Readers/Zip/ZipReader.cs index 3d9c7acc..ec5f729b 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.cs @@ -9,7 +9,7 @@ using SharpCompress.Common.Zip.Headers; namespace SharpCompress.Readers.Zip; -public class ZipReader : AbstractReader +public partial class ZipReader : AbstractReader { private readonly StreamingZipHeaderFactory _headerFactory; diff --git a/src/SharpCompress/Writers/IWriterOpenable.cs b/src/SharpCompress/Writers/IWriterOpenable.cs new file mode 100644 index 00000000..f7073d25 --- /dev/null +++ b/src/SharpCompress/Writers/IWriterOpenable.cs @@ -0,0 +1,6 @@ +namespace SharpCompress.Writers; + +public class IWriterOpenable +{ + +} From e63ee57ef0d386e5c4b9b36c032ef5a90e5539a1 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 14 Jan 2026 09:29:44 +0000 Subject: [PATCH 27/36] same for writers --- .../Writers/GZip/GZipWriter.Factory.cs | 58 +++++++++++++++++++ src/SharpCompress/Writers/GZip/GZipWriter.cs | 2 +- src/SharpCompress/Writers/IWriterOpenable.cs | 33 ++++++++++- .../Writers/Tar/TarWriter.Factory.cs | 58 +++++++++++++++++++ src/SharpCompress/Writers/Tar/TarWriter.cs | 2 +- src/SharpCompress/Writers/WriterFactory.cs | 1 - .../Writers/Zip/ZipWriter.Factory.cs | 58 +++++++++++++++++++ src/SharpCompress/Writers/Zip/ZipWriter.cs | 2 +- 8 files changed, 208 insertions(+), 6 deletions(-) create mode 100644 src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs create mode 100644 src/SharpCompress/Writers/Tar/TarWriter.Factory.cs create mode 100644 src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs b/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs new file mode 100644 index 00000000..57a22b54 --- /dev/null +++ b/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs @@ -0,0 +1,58 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Writers.GZip; + +public partial class GZipWriter : IWriterOpenable +{ + public static IWriter Open(string filePath, GZipWriterOptions writerOptions) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), writerOptions); + } + + public static IWriter Open(FileInfo fileInfo, GZipWriterOptions writerOptions) + { + fileInfo.NotNull(nameof(fileInfo)); + return new GZipWriter(fileInfo.OpenWrite(), writerOptions); + } + + public static IWriter Open(Stream stream, GZipWriterOptions writerOptions) + { + stream.NotNull(nameof(stream)); + return new GZipWriter(stream, writerOptions); + } + + public static IAsyncWriter OpenAsync( + string path, + GZipWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(path, writerOptions); + } + + public static IAsyncWriter OpenAsync( + Stream stream, + GZipWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(stream, writerOptions); + } + + public static IAsyncWriter OpenAsync( + FileInfo fileInfo, + GZipWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(fileInfo, writerOptions); + } +} +#endif diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.cs b/src/SharpCompress/Writers/GZip/GZipWriter.cs index ad1d59d7..95ebd253 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriter.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriter.cs @@ -7,7 +7,7 @@ using SharpCompress.IO; namespace SharpCompress.Writers.GZip; -public sealed class GZipWriter : AbstractWriter +public sealed partial class GZipWriter : AbstractWriter { private bool _wroteToStream; diff --git a/src/SharpCompress/Writers/IWriterOpenable.cs b/src/SharpCompress/Writers/IWriterOpenable.cs index f7073d25..5935bbe9 100644 --- a/src/SharpCompress/Writers/IWriterOpenable.cs +++ b/src/SharpCompress/Writers/IWriterOpenable.cs @@ -1,6 +1,35 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; + namespace SharpCompress.Writers; -public class IWriterOpenable +public interface IWriterOpenable + where TWriterOptions : WriterOptions { - + public static abstract IWriter Open(string filePath, TWriterOptions writerOptions); + + public static abstract IWriter Open(FileInfo fileInfo, TWriterOptions writerOptions); + public static abstract IWriter Open(Stream stream, TWriterOptions writerOptions); + + /// + /// Opens a Writer asynchronously. + /// + /// The stream to write to. + /// The archive type. + /// Writer options. + /// Cancellation token. + /// A task that returns an IWriter. + public static abstract IAsyncWriter OpenAsync( + Stream stream, + TWriterOptions writerOptions, + CancellationToken cancellationToken = default + ); + + public static abstract IAsyncWriter OpenAsync(string filePath, TWriterOptions writerOptions, + CancellationToken cancellationToken = default); + + public static abstract IAsyncWriter OpenAsync(FileInfo fileInfo, TWriterOptions writerOptions, + CancellationToken cancellationToken = default); } +#endif diff --git a/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs b/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs new file mode 100644 index 00000000..059fd62e --- /dev/null +++ b/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs @@ -0,0 +1,58 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Writers.Tar; + +public partial class TarWriter : IWriterOpenable +{ + public static IWriter Open(string filePath, TarWriterOptions writerOptions) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), writerOptions); + } + + public static IWriter Open(FileInfo fileInfo, TarWriterOptions writerOptions) + { + fileInfo.NotNull(nameof(fileInfo)); + return new TarWriter(fileInfo.OpenWrite(), writerOptions); + } + + public static IWriter Open(Stream stream, TarWriterOptions writerOptions) + { + stream.NotNull(nameof(stream)); + return new TarWriter(stream, writerOptions); + } + + public static IAsyncWriter OpenAsync( + string path, + TarWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(path, writerOptions); + } + + public static IAsyncWriter OpenAsync( + Stream stream, + TarWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(stream, writerOptions); + } + + public static IAsyncWriter OpenAsync( + FileInfo fileInfo, + TarWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(fileInfo, writerOptions); + } +} +#endif diff --git a/src/SharpCompress/Writers/Tar/TarWriter.cs b/src/SharpCompress/Writers/Tar/TarWriter.cs index aec01454..ac16e66f 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.cs @@ -12,7 +12,7 @@ using SharpCompress.IO; namespace SharpCompress.Writers.Tar; -public class TarWriter : AbstractWriter +public partial class TarWriter : AbstractWriter { private readonly bool finalizeArchiveOnClose; private TarHeaderWriteFormat headerFormat; diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index be2a7780..ebeda45f 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -2,7 +2,6 @@ using System; using System.IO; using System.Linq; using System.Threading; -using System.Threading.Tasks; using SharpCompress.Common; namespace SharpCompress.Writers; diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs b/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs new file mode 100644 index 00000000..44825a72 --- /dev/null +++ b/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs @@ -0,0 +1,58 @@ +#if NET8_0_OR_GREATER +using System.IO; +using System.Threading; +using SharpCompress.Common; + +namespace SharpCompress.Writers.Zip; + +public partial class ZipWriter : IWriterOpenable +{ + public static IWriter Open(string filePath, ZipWriterOptions writerOptions) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), writerOptions); + } + + public static IWriter Open(FileInfo fileInfo, ZipWriterOptions writerOptions) + { + fileInfo.NotNull(nameof(fileInfo)); + return new ZipWriter(fileInfo.OpenWrite(), writerOptions); + } + + public static IWriter Open(Stream stream, ZipWriterOptions writerOptions) + { + stream.NotNull(nameof(stream)); + return new ZipWriter(stream, writerOptions); + } + + public static IAsyncWriter OpenAsync( + string path, + ZipWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(path, writerOptions); + } + + public static IAsyncWriter OpenAsync( + Stream stream, + ZipWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(stream, writerOptions); + } + + public static IAsyncWriter OpenAsync( + FileInfo fileInfo, + ZipWriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return (IAsyncWriter)Open(fileInfo, writerOptions); + } +} +#endif diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.cs b/src/SharpCompress/Writers/Zip/ZipWriter.cs index 845856b8..dc8929b8 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.cs @@ -18,7 +18,7 @@ using SharpCompress.IO; namespace SharpCompress.Writers.Zip; -public class ZipWriter : AbstractWriter +public partial class ZipWriter : AbstractWriter { private readonly CompressionType compressionType; private readonly int compressionLevel; From 95d2278d8b0aba11da1d399dd9449a0e86ea9e57 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 14 Jan 2026 12:13:29 +0000 Subject: [PATCH 28/36] fmt --- src/SharpCompress/Writers/IWriterOpenable.cs | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/SharpCompress/Writers/IWriterOpenable.cs b/src/SharpCompress/Writers/IWriterOpenable.cs index 5935bbe9..f856a309 100644 --- a/src/SharpCompress/Writers/IWriterOpenable.cs +++ b/src/SharpCompress/Writers/IWriterOpenable.cs @@ -10,7 +10,7 @@ public interface IWriterOpenable public static abstract IWriter Open(string filePath, TWriterOptions writerOptions); public static abstract IWriter Open(FileInfo fileInfo, TWriterOptions writerOptions); - public static abstract IWriter Open(Stream stream, TWriterOptions writerOptions); + public static abstract IWriter Open(Stream stream, TWriterOptions writerOptions); /// /// Opens a Writer asynchronously. @@ -20,16 +20,22 @@ public interface IWriterOpenable /// Writer options. /// Cancellation token. /// A task that returns an IWriter. - public static abstract IAsyncWriter OpenAsync( + public static abstract IAsyncWriter OpenAsync( Stream stream, TWriterOptions writerOptions, CancellationToken cancellationToken = default ); - public static abstract IAsyncWriter OpenAsync(string filePath, TWriterOptions writerOptions, - CancellationToken cancellationToken = default); + public static abstract IAsyncWriter OpenAsync( + string filePath, + TWriterOptions writerOptions, + CancellationToken cancellationToken = default + ); - public static abstract IAsyncWriter OpenAsync(FileInfo fileInfo, TWriterOptions writerOptions, - CancellationToken cancellationToken = default); + public static abstract IAsyncWriter OpenAsync( + FileInfo fileInfo, + TWriterOptions writerOptions, + CancellationToken cancellationToken = default + ); } #endif From 5c4719f4a92c0a9bcc84334b6b5784e7aaba0199 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 14 Jan 2026 13:39:11 +0000 Subject: [PATCH 29/36] missing extensions --- .../Archives/IArchiveExtensions.cs | 9 -- .../Archives/IAsyncArchiveExtensions.cs | 139 +++++++++--------- .../IWritableArchiveCommonExtensions.cs | 91 +++++------- .../Archives/IWritableArchiveExtensions.cs | 24 ++- .../IWritableAsyncArchiveExtensions.cs | 45 +++--- .../Archives/Rar/RarArchive.Extensions.cs | 46 +++--- .../Readers/IAsyncReaderExtensions.cs | 28 ++++ .../Writers/IWriterExtensions.cs | 4 +- src/SharpCompress/Writers/WriterFactory.cs | 47 ++++++ 9 files changed, 237 insertions(+), 196 deletions(-) diff --git a/src/SharpCompress/Archives/IArchiveExtensions.cs b/src/SharpCompress/Archives/IArchiveExtensions.cs index f02dfc38..80857a25 100644 --- a/src/SharpCompress/Archives/IArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IArchiveExtensions.cs @@ -8,7 +8,6 @@ namespace SharpCompress.Archives; public static class IArchiveExtensions { - /// The archive to extract. extension(IArchive archive) { /// @@ -23,7 +22,6 @@ public static class IArchiveExtensions IProgress? progress = null ) { - // For solid archives (Rar, 7Zip), use the optimized reader-based approach if (archive.IsSolid || archive.Type == ArchiveType.SevenZip) { using var reader = archive.ExtractAllEntries(); @@ -31,7 +29,6 @@ public static class IArchiveExtensions } else { - // For non-solid archives, extract entries directly archive.WriteToDirectoryInternal(destinationDirectory, options, progress); } } @@ -42,14 +39,10 @@ public static class IArchiveExtensions IProgress? progress ) { - // Prepare for progress reporting var totalBytes = archive.TotalUncompressedSize; var bytesRead = 0L; - - // Tracking for created directories. var seenDirectories = new HashSet(); - // Extract foreach (var entry in archive.Entries) { if (entry.IsDirectory) @@ -68,10 +61,8 @@ public static class IArchiveExtensions continue; } - // Use the entry's WriteToDirectory method which respects ExtractionOptions entry.WriteToDirectory(destinationDirectory, options); - // Update progress bytesRead += entry.Size; progress?.Report( new ProgressReport(entry.Key ?? string.Empty, bytesRead, totalBytes) diff --git a/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs b/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs index 128258b3..df4cb05c 100644 --- a/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IAsyncArchiveExtensions.cs @@ -10,84 +10,83 @@ namespace SharpCompress.Archives; public static class IAsyncArchiveExtensions { - /// - /// Extract to specific directory asynchronously with progress reporting and cancellation support - /// - /// The archive to extract. - /// The folder to extract into. - /// Extraction options. - /// Optional progress reporter for tracking extraction progress. - /// Optional cancellation token. - public static async Task WriteToDirectoryAsync( - this IAsyncArchive archive, - string destinationDirectory, - ExtractionOptions? options = null, - IProgress? progress = null, - CancellationToken cancellationToken = default - ) + extension(IAsyncArchive archive) { - // For solid archives (Rar, 7Zip), use the optimized reader-based approach - if (await archive.IsSolidAsync() || archive.Type == ArchiveType.SevenZip) + /// + /// Extract to specific directory asynchronously with progress reporting and cancellation support + /// + /// The archive to extract. + /// The folder to extract into. + /// Extraction options. + /// Optional progress reporter for tracking extraction progress. + /// Optional cancellation token. + public async Task WriteToDirectoryAsync( + string destinationDirectory, + ExtractionOptions? options = null, + IProgress? progress = null, + CancellationToken cancellationToken = default + ) { - await using var reader = await archive.ExtractAllEntriesAsync(); - await reader.WriteAllToDirectoryAsync(destinationDirectory, options, cancellationToken); - } - else - { - // For non-solid archives, extract entries directly - await archive.WriteToDirectoryAsyncInternal( - destinationDirectory, - options, - progress, - cancellationToken - ); - } - } - - private static async Task WriteToDirectoryAsyncInternal( - this IAsyncArchive archive, - string destinationDirectory, - ExtractionOptions? options, - IProgress? progress, - CancellationToken cancellationToken - ) - { - // Prepare for progress reporting - var totalBytes = await archive.TotalUncompressedSizeAsync(); - var bytesRead = 0L; - - // Tracking for created directories. - var seenDirectories = new HashSet(); - - // Extract - await foreach (var entry in archive.EntriesAsync.WithCancellation(cancellationToken)) - { - cancellationToken.ThrowIfCancellationRequested(); - - if (entry.IsDirectory) + if (await archive.IsSolidAsync() || archive.Type == ArchiveType.SevenZip) { - var dirPath = Path.Combine( + await using var reader = await archive.ExtractAllEntriesAsync(); + await reader.WriteAllToDirectoryAsync( destinationDirectory, - entry.Key.NotNull("Entry Key is null") + options, + cancellationToken ); - if ( - Path.GetDirectoryName(dirPath + "/") is { } parentDirectory - && seenDirectories.Add(dirPath) - ) - { - Directory.CreateDirectory(parentDirectory); - } - continue; } + else + { + await archive.WriteToDirectoryAsyncInternal( + destinationDirectory, + options, + progress, + cancellationToken + ); + } + } - // Use the entry's WriteToDirectoryAsync method which respects ExtractionOptions - await entry - .WriteToDirectoryAsync(destinationDirectory, options, cancellationToken) - .ConfigureAwait(false); + private async Task WriteToDirectoryAsyncInternal( + string destinationDirectory, + ExtractionOptions? options, + IProgress? progress, + CancellationToken cancellationToken + ) + { + var totalBytes = await archive.TotalUncompressedSizeAsync(); + var bytesRead = 0L; + var seenDirectories = new HashSet(); - // Update progress - bytesRead += entry.Size; - progress?.Report(new ProgressReport(entry.Key ?? string.Empty, bytesRead, totalBytes)); + await foreach (var entry in archive.EntriesAsync.WithCancellation(cancellationToken)) + { + cancellationToken.ThrowIfCancellationRequested(); + + if (entry.IsDirectory) + { + var dirPath = Path.Combine( + destinationDirectory, + entry.Key.NotNull("Entry Key is null") + ); + if ( + Path.GetDirectoryName(dirPath + "/") is { } parentDirectory + && seenDirectories.Add(dirPath) + ) + { + Directory.CreateDirectory(parentDirectory); + } + continue; + } + + await entry + .WriteToDirectoryAsync(destinationDirectory, options, cancellationToken) + .ConfigureAwait(false); + + bytesRead += entry.Size; + progress?.Report( + new ProgressReport(entry.Key ?? string.Empty, bytesRead, totalBytes) + ); + } } } } diff --git a/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs b/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs index 2fa86ee3..77a7f499 100644 --- a/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs +++ b/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs @@ -5,71 +5,48 @@ namespace SharpCompress.Archives; public static class IWritableArchiveCommonExtensions { - public static void AddEntry( - this IWritableArchive writableArchive, - string entryPath, - string filePath - ) + extension(IWritableArchiveCommon writableArchive) { - var fileInfo = new FileInfo(filePath); - if (!fileInfo.Exists) + public void AddAllFromDirectory( + string filePath, + string searchPattern = "*.*", + SearchOption searchOption = SearchOption.AllDirectories + ) { - throw new FileNotFoundException("Could not AddEntry: " + filePath); - } - writableArchive.AddEntry( - entryPath, - new FileInfo(filePath).OpenRead(), - true, - fileInfo.Length, - fileInfo.LastWriteTime - ); - } - - public static void AddAllFromDirectory( - this IWritableArchiveCommon writableArchive, - string filePath, - string searchPattern = "*.*", - SearchOption searchOption = SearchOption.AllDirectories - ) - { - using (writableArchive.PauseEntryRebuilding()) - { - foreach (var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption)) + using (writableArchive.PauseEntryRebuilding()) { - var fileInfo = new FileInfo(path); - writableArchive.AddEntry( - path.Substring(filePath.Length), - fileInfo.OpenRead(), - true, - fileInfo.Length, - fileInfo.LastWriteTime - ); + foreach ( + var path in Directory.EnumerateFiles(filePath, searchPattern, searchOption) + ) + { + var fileInfo = new FileInfo(path); + writableArchive.AddEntry( + path.Substring(filePath.Length), + fileInfo.OpenRead(), + true, + fileInfo.Length, + fileInfo.LastWriteTime + ); + } } } - } - public static IArchiveEntry AddEntry( - this IWritableArchiveCommon writableArchive, - string key, - string file - ) => AddEntry(writableArchive, key, new FileInfo(file)); + public IArchiveEntry AddEntry(string key, string file) => + writableArchive.AddEntry(key, new FileInfo(file)); - public static IArchiveEntry AddEntry( - this IWritableArchiveCommon writableArchive, - string key, - FileInfo fileInfo - ) - { - if (!fileInfo.Exists) + public IArchiveEntry AddEntry(string key, FileInfo fileInfo) { - throw new ArgumentException("FileInfo does not exist."); + if (!fileInfo.Exists) + { + throw new ArgumentException("FileInfo does not exist."); + } + return writableArchive.AddEntry( + key, + fileInfo.OpenRead(), + true, + fileInfo.Length, + fileInfo.LastWriteTime + ); } - return writableArchive.AddEntry( - key, - fileInfo.OpenRead(), - true, - fileInfo.Length, - fileInfo.LastWriteTime - ); } } diff --git a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs index 71b790b7..60971bfc 100644 --- a/src/SharpCompress/Archives/IWritableArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IWritableArchiveExtensions.cs @@ -1,4 +1,4 @@ -using System.IO; +using System.IO; using SharpCompress.Common; using SharpCompress.Writers; @@ -6,19 +6,15 @@ namespace SharpCompress.Archives; public static class IWritableArchiveExtensions { - public static void SaveTo( - this IWritableArchive writableArchive, - string filePath, - WriterOptions? options = null - ) => writableArchive.SaveTo(new FileInfo(filePath), options ?? new(CompressionType.Deflate)); - - public static void SaveTo( - this IWritableArchive writableArchive, - FileInfo fileInfo, - WriterOptions? options = null - ) + extension(IWritableArchive writableArchive) { - using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); - writableArchive.SaveTo(stream, options ?? new(CompressionType.Deflate)); + public void SaveTo(string filePath, WriterOptions? options = null) => + writableArchive.SaveTo(new FileInfo(filePath), options ?? new(CompressionType.Deflate)); + + public void SaveTo(FileInfo fileInfo, WriterOptions? options = null) + { + using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); + writableArchive.SaveTo(stream, options ?? new(CompressionType.Deflate)); + } } } diff --git a/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs b/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs index ffe35eda..a69bfc0a 100644 --- a/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs +++ b/src/SharpCompress/Archives/IWritableAsyncArchiveExtensions.cs @@ -8,28 +8,29 @@ namespace SharpCompress.Archives; public static class IWritableAsyncArchiveExtensions { - public static ValueTask SaveToAsync( - this IWritableAsyncArchive writableArchive, - string filePath, - WriterOptions? options = null, - CancellationToken cancellationToken = default - ) => - writableArchive.SaveToAsync( - new FileInfo(filePath), - options ?? new(CompressionType.Deflate), - cancellationToken - ); - - public static async ValueTask SaveToAsync( - this IWritableAsyncArchive writableArchive, - FileInfo fileInfo, - WriterOptions? options = null, - CancellationToken cancellationToken = default - ) + extension(IWritableAsyncArchive writableArchive) { - using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); - await writableArchive - .SaveToAsync(stream, options ?? new(CompressionType.Deflate), cancellationToken) - .ConfigureAwait(false); + public ValueTask SaveToAsync( + string filePath, + WriterOptions? options = null, + CancellationToken cancellationToken = default + ) => + writableArchive.SaveToAsync( + new FileInfo(filePath), + options ?? new(CompressionType.Deflate), + cancellationToken + ); + + public async ValueTask SaveToAsync( + FileInfo fileInfo, + WriterOptions? options = null, + CancellationToken cancellationToken = default + ) + { + using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); + await writableArchive + .SaveToAsync(stream, options ?? new(CompressionType.Deflate), cancellationToken) + .ConfigureAwait(false); + } } } diff --git a/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs b/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs index 71e2fa7c..eb7c1f3b 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.Extensions.cs @@ -1,4 +1,4 @@ -using System.Linq; +using System.Linq; using System.Threading.Tasks; using SharpCompress.Common.Rar; @@ -6,27 +6,31 @@ namespace SharpCompress.Archives.Rar; public static class RarArchiveExtensions { - /// - /// RarArchive is the first volume of a multi-part archive. If MultipartVolume is true and IsFirstVolume is false then the first volume file must be missing. - /// - public static bool IsFirstVolume(this IRarArchive archive) => - archive.Volumes.Cast().First().IsFirstVolume; + extension(IRarArchive archive) + { + /// + /// RarArchive is the first volume of a multi-part archive. If MultipartVolume is true and IsFirstVolume is false then the first volume file must be missing. + /// + public bool IsFirstVolume() => archive.Volumes.Cast().First().IsFirstVolume; - /// - /// RarArchive is part of a multi-part archive. - /// - public static bool IsMultipartVolume(this IRarArchive archive) => - archive.Volumes.Cast().First().IsMultiVolume; + /// + /// RarArchive is part of a multi-part archive. + /// + public bool IsMultipartVolume() => archive.Volumes.Cast().First().IsMultiVolume; + } - /// - /// RarArchive is the first volume of a multi-part archive. If MultipartVolume is true and IsFirstVolume is false then the first volume file must be missing. - /// - public static async ValueTask IsFirstVolumeAsync(this IRarAsyncArchive archive) => - (await archive.VolumesAsync.CastAsync().FirstAsync()).IsFirstVolume; + extension(IRarAsyncArchive archive) + { + /// + /// RarArchive is the first volume of a multi-part archive. If MultipartVolume is true and IsFirstVolume is false then the first volume file must be missing. + /// + public async ValueTask IsFirstVolumeAsync() => + (await archive.VolumesAsync.CastAsync().FirstAsync()).IsFirstVolume; - /// - /// RarArchive is part of a multi-part archive. - /// - public static async ValueTask IsMultipartVolumeAsync(this IRarAsyncArchive archive) => - (await archive.VolumesAsync.CastAsync().FirstAsync()).IsMultiVolume; + /// + /// RarArchive is part of a multi-part archive. + /// + public async ValueTask IsMultipartVolumeAsync() => + (await archive.VolumesAsync.CastAsync().FirstAsync()).IsMultiVolume; + } } diff --git a/src/SharpCompress/Readers/IAsyncReaderExtensions.cs b/src/SharpCompress/Readers/IAsyncReaderExtensions.cs index 2b9a6a6b..be53d630 100644 --- a/src/SharpCompress/Readers/IAsyncReaderExtensions.cs +++ b/src/SharpCompress/Readers/IAsyncReaderExtensions.cs @@ -65,5 +65,33 @@ public static class IAsyncReaderExtensions .ConfigureAwait(false); } } + + public async ValueTask WriteEntryToAsync( + string destinationFileName, + ExtractionOptions? options = null, + CancellationToken cancellationToken = default + ) => + await ExtractionMethods + .WriteEntryToFileAsync( + reader.Entry, + destinationFileName, + options, + async (x, fm, ct) => + { + using var fs = File.Open(destinationFileName, fm); + await reader.WriteEntryToAsync(fs, ct).ConfigureAwait(false); + }, + cancellationToken + ) + .ConfigureAwait(false); + + public async ValueTask WriteEntryToAsync( + FileInfo destinationFileInfo, + ExtractionOptions? options = null, + CancellationToken cancellationToken = default + ) => + await reader + .WriteEntryToAsync(destinationFileInfo.FullName, options, cancellationToken) + .ConfigureAwait(false); } } diff --git a/src/SharpCompress/Writers/IWriterExtensions.cs b/src/SharpCompress/Writers/IWriterExtensions.cs index 706baf0e..a178dd5d 100644 --- a/src/SharpCompress/Writers/IWriterExtensions.cs +++ b/src/SharpCompress/Writers/IWriterExtensions.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.IO; using System.Linq; using System.Threading; @@ -127,6 +127,4 @@ public static class IWriterExtensions CancellationToken cancellationToken = default ) => writer.WriteDirectoryAsync(directoryName, null, cancellationToken); } - - // Async extensions } diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index ebeda45f..675ef675 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -8,6 +8,53 @@ namespace SharpCompress.Writers; public static class WriterFactory { + public static IWriter Open( + string filePath, + ArchiveType archiveType, + WriterOptions writerOptions + ) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return Open(new FileInfo(filePath), archiveType, writerOptions); + } + + public static IWriter Open( + FileInfo fileInfo, + ArchiveType archiveType, + WriterOptions writerOptions + ) + { + fileInfo.NotNull(nameof(fileInfo)); + return Open(fileInfo.OpenWrite(), archiveType, writerOptions); + } + + public static IAsyncWriter OpenAsync( + string filePath, + ArchiveType archiveType, + WriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + filePath.NotNullOrEmpty(nameof(filePath)); + return OpenAsync(new FileInfo(filePath), archiveType, writerOptions, cancellationToken); + } + + public static IAsyncWriter OpenAsync( + FileInfo fileInfo, + ArchiveType archiveType, + WriterOptions writerOptions, + CancellationToken cancellationToken = default + ) + { + fileInfo.NotNull(nameof(fileInfo)); + return OpenAsync( + fileInfo.Open(FileMode.Create, FileAccess.Write), + archiveType, + writerOptions, + cancellationToken + ); + } + public static IWriter Open(Stream stream, ArchiveType archiveType, WriterOptions writerOptions) { var factory = Factories From 7b76858ae1275568771c7ffddfe5b89022979d8e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 15 Jan 2026 11:41:30 +0000 Subject: [PATCH 30/36] refactoring naming again --- src/SharpCompress/Archives/ArchiveFactory.cs | 54 ++++++++-------- .../Archives/AutoArchiveFactory.cs | 16 ++--- .../Archives/GZip/GZipArchive.Factory.cs | 37 ++++++----- src/SharpCompress/Archives/IArchiveFactory.cs | 8 +-- .../Archives/IArchiveOpenable.cs | 15 +++-- .../Archives/IMultiArchiveFactory.cs | 11 ++-- .../Archives/IMultiArchiveOpenable.cs | 8 +-- .../Archives/IWriteableArchiveFactory.cs | 2 +- .../Archives/Rar/RarArchive.Factory.cs | 33 +++++----- .../SevenZip/SevenZipArchive.Factory.cs | 35 ++++++----- .../Archives/Tar/TarArchive.Factory.cs | 37 ++++++----- .../Archives/Zip/ZipArchive.Factory.cs | 37 ++++++----- src/SharpCompress/Factories/GZipFactory.cs | 48 +++++++------- src/SharpCompress/Factories/RarFactory.cs | 40 ++++++------ .../Factories/SevenZipFactory.cs | 40 ++++++------ src/SharpCompress/Factories/TarFactory.cs | 48 +++++++------- src/SharpCompress/Factories/ZipFactory.cs | 48 +++++++------- src/SharpCompress/Readers/ReaderFactory.cs | 20 +++--- .../Writers/GZip/GZipWriter.Factory.cs | 20 +++--- src/SharpCompress/Writers/IWriterFactory.cs | 4 +- src/SharpCompress/Writers/IWriterOpenable.cs | 12 ++-- .../Writers/Tar/TarWriter.Factory.cs | 20 +++--- src/SharpCompress/Writers/WriterFactory.cs | 14 ++--- .../Writers/Zip/ZipWriter.Factory.cs | 20 +++--- tests/SharpCompress.Performance/Program.cs | 4 +- .../SharpCompress.Test/Ace/AceReaderTests.cs | 2 +- tests/SharpCompress.Test/ArchiveTests.cs | 56 ++++++++++------- .../SharpCompress.Test/Arj/ArjReaderTests.cs | 2 +- .../BZip2/BZip2ReaderTests.cs | 2 +- tests/SharpCompress.Test/ExtractAll.cs | 4 +- .../ExtractAllEntriesTests.cs | 4 +- tests/SharpCompress.Test/ExtractionTests.cs | 8 +-- tests/SharpCompress.Test/GZip/AsyncTests.cs | 14 ++--- .../GZip/GZipArchiveAsyncTests.cs | 12 ++-- .../GZip/GZipArchiveDirectoryTests.cs | 2 +- .../GZip/GZipArchiveTests.cs | 14 ++--- .../GZip/GZipReaderAsyncTests.cs | 4 +- .../GZip/GZipReaderTests.cs | 2 +- .../GZip/GZipWriterAsyncTests.cs | 4 +- .../GZip/GZipWriterTests.cs | 4 +- .../SharpCompress.Test/ProgressReportTests.cs | 22 +++---- .../Rar/RarArchiveAsyncTests.cs | 40 ++++++------ .../SharpCompress.Test/Rar/RarArchiveTests.cs | 36 +++++------ .../Rar/RarReaderAsyncTests.cs | 18 +++--- .../SharpCompress.Test/Rar/RarReaderTests.cs | 20 +++--- tests/SharpCompress.Test/ReaderTests.cs | 8 +-- .../SevenZip/SevenZipArchiveAsyncTests.cs | 20 +++--- .../SevenZip/SevenZipArchiveTests.cs | 12 ++-- .../Streams/ZLibBaseStreamAsyncTests.cs | 4 +- .../Streams/ZlibBaseStreamTests.cs | 4 +- .../Tar/TarArchiveAsyncTests.cs | 18 +++--- .../Tar/TarArchiveDirectoryTests.cs | 12 ++-- .../SharpCompress.Test/Tar/TarArchiveTests.cs | 26 ++++---- .../Tar/TarReaderAsyncTests.cs | 20 +++--- .../SharpCompress.Test/Tar/TarReaderTests.cs | 22 +++---- .../Tar/TarWriterDirectoryTests.cs | 12 ++-- tests/SharpCompress.Test/TestBase.cs | 4 +- tests/SharpCompress.Test/WriterTests.cs | 12 ++-- .../SharpCompress.Test/Zip/Zip64AsyncTests.cs | 6 +- tests/SharpCompress.Test/Zip/Zip64Tests.cs | 6 +- .../Zip/Zip64VersionConsistencyTests.cs | 10 +-- .../Zip/ZipArchiveAsyncTests.cs | 12 ++-- .../Zip/ZipArchiveDirectoryTests.cs | 12 ++-- .../SharpCompress.Test/Zip/ZipArchiveTests.cs | 62 +++++++++---------- .../Zip/ZipMemoryArchiveWithCrcAsyncTests.cs | 8 +-- .../Zip/ZipMemoryArchiveWithCrcTests.cs | 6 +- .../Zip/ZipReaderAsyncTests.cs | 16 ++--- .../SharpCompress.Test/Zip/ZipReaderTests.cs | 40 ++++++------ .../Zip/ZipWriterDirectoryTests.cs | 12 ++-- .../SharpCompress.Test/Zip/ZipWriterTests.cs | 4 +- 70 files changed, 661 insertions(+), 608 deletions(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index 8d4b9c27..d94786e7 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -13,14 +13,14 @@ namespace SharpCompress.Archives; public static class ArchiveFactory { - public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) { readerOptions ??= new ReaderOptions(); stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); - return FindFactory(stream).Open(stream, readerOptions); + return FindFactory(stream).OpenArchive(stream, readerOptions); } - public static async ValueTask OpenAsync( + public static async ValueTask OpenAsyncArchive( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -29,10 +29,10 @@ public static class ArchiveFactory readerOptions ??= new ReaderOptions(); stream = SharpCompressStream.Create(stream, bufferSize: readerOptions.BufferSize); var factory = await FindFactoryAsync(stream, cancellationToken); - return factory.OpenAsync(stream, readerOptions); + return factory.OpenAsyncArchive(stream, readerOptions); } - public static IWritableArchive Create(ArchiveType type) + public static IWritableArchive CreateArchive(ArchiveType type) { var factory = Factory .Factories.OfType() @@ -40,36 +40,36 @@ public static class ArchiveFactory if (factory != null) { - return factory.CreateWriteableArchive(); + return factory.CreateArchive(); } throw new NotSupportedException("Cannot create Archives of type: " + type); } - public static IArchive Open(string filePath, ReaderOptions? options = null) + public static IArchive OpenArchive(string filePath, ReaderOptions? options = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), options); + return OpenArchive(new FileInfo(filePath), options); } - public static ValueTask OpenAsync( + public static ValueTask OpenAsyncArchive( string filePath, ReaderOptions? options = null, CancellationToken cancellationToken = default ) { filePath.NotNullOrEmpty(nameof(filePath)); - return OpenAsync(new FileInfo(filePath), options, cancellationToken); + return OpenAsyncArchive(new FileInfo(filePath), options, cancellationToken); } - public static IArchive Open(FileInfo fileInfo, ReaderOptions? options = null) + public static IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? options = null) { options ??= new ReaderOptions { LeaveStreamOpen = false }; - return FindFactory(fileInfo).Open(fileInfo, options); + return FindFactory(fileInfo).OpenArchive(fileInfo, options); } - public static async ValueTask OpenAsync( + public static async ValueTask OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -78,10 +78,10 @@ public static class ArchiveFactory options ??= new ReaderOptions { LeaveStreamOpen = false }; var factory = await FindFactoryAsync(fileInfo, cancellationToken); - return factory.OpenAsync(fileInfo, options, cancellationToken); + return factory.OpenAsyncArchive(fileInfo, options, cancellationToken); } - public static IArchive Open(IEnumerable fileInfos, ReaderOptions? options = null) + public static IArchive OpenArchive(IEnumerable fileInfos, ReaderOptions? options = null) { fileInfos.NotNull(nameof(fileInfos)); var filesArray = fileInfos.ToArray(); @@ -93,16 +93,16 @@ public static class ArchiveFactory var fileInfo = filesArray[0]; if (filesArray.Length == 1) { - return Open(fileInfo, options); + return OpenArchive(fileInfo, options); } fileInfo.NotNull(nameof(fileInfo)); options ??= new ReaderOptions { LeaveStreamOpen = false }; - return FindFactory(fileInfo).Open(filesArray, options); + return FindFactory(fileInfo).OpenArchive(filesArray, options); } - public static async ValueTask OpenAsync( + public static async ValueTask OpenAsyncArchive( IEnumerable fileInfos, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -118,17 +118,17 @@ public static class ArchiveFactory var fileInfo = filesArray[0]; if (filesArray.Length == 1) { - return await OpenAsync(fileInfo, options, cancellationToken); + return await OpenAsyncArchive(fileInfo, options, cancellationToken); } fileInfo.NotNull(nameof(fileInfo)); options ??= new ReaderOptions { LeaveStreamOpen = false }; var factory = await FindFactoryAsync(fileInfo, cancellationToken); - return factory.OpenAsync(filesArray, options, cancellationToken); + return factory.OpenAsyncArchive(filesArray, options, cancellationToken); } - public static IArchive Open(IEnumerable streams, ReaderOptions? options = null) + public static IArchive OpenArchive(IEnumerable streams, ReaderOptions? options = null) { streams.NotNull(nameof(streams)); var streamsArray = streams.ToArray(); @@ -140,16 +140,16 @@ public static class ArchiveFactory var firstStream = streamsArray[0]; if (streamsArray.Length == 1) { - return Open(firstStream, options); + return OpenArchive(firstStream, options); } firstStream.NotNull(nameof(firstStream)); options ??= new ReaderOptions(); - return FindFactory(firstStream).Open(streamsArray, options); + return FindFactory(firstStream).OpenArchive(streamsArray, options); } - public static async ValueTask OpenAsync( + public static async ValueTask OpenAsyncArchive( IEnumerable streams, ReaderOptions? options = null, CancellationToken cancellationToken = default @@ -166,14 +166,14 @@ public static class ArchiveFactory var firstStream = streamsArray[0]; if (streamsArray.Length == 1) { - return await OpenAsync(firstStream, options, cancellationToken); + return await OpenAsyncArchive(firstStream, options, cancellationToken); } firstStream.NotNull(nameof(firstStream)); options ??= new ReaderOptions(); var factory = FindFactory(firstStream); - return factory.OpenAsync(streamsArray, options); + return factory.OpenAsyncArchive(streamsArray, options); } public static void WriteToDirectory( @@ -182,7 +182,7 @@ public static class ArchiveFactory ExtractionOptions? options = null ) { - using var archive = Open(sourceArchive); + using var archive = OpenArchive(sourceArchive); archive.WriteToDirectory(destinationDirectory, options); } diff --git a/src/SharpCompress/Archives/AutoArchiveFactory.cs b/src/SharpCompress/Archives/AutoArchiveFactory.cs index c92c305f..f588b085 100644 --- a/src/SharpCompress/Archives/AutoArchiveFactory.cs +++ b/src/SharpCompress/Archives/AutoArchiveFactory.cs @@ -31,22 +31,22 @@ internal class AutoArchiveFactory : IArchiveFactory public FileInfo? GetFilePart(int index, FileInfo part1) => throw new NotSupportedException(); - public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) => - ArchiveFactory.Open(stream, readerOptions); + public IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) => + ArchiveFactory.OpenArchive(stream, readerOptions); - public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => - (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsyncArchive(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)OpenArchive(stream, readerOptions); - public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => - ArchiveFactory.Open(fileInfo, readerOptions); + public IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) => + ArchiveFactory.OpenArchive(fileInfo, readerOptions); - public IAsyncArchive OpenAsync( + public IAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfo, readerOptions); } } diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs index b2053faa..16092968 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs @@ -20,7 +20,7 @@ public partial class GZipArchive IMultiArchiveOpenable #endif { - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -28,19 +28,22 @@ public partial class GZipArchive { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IWritableAsyncArchive)Open( + return (IWritableAsyncArchive)OpenArchive( new FileInfo(path), readerOptions ?? new ReaderOptions() ); } - public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); + return OpenArchive(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } - public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive( + FileInfo fileInfo, + ReaderOptions? readerOptions = null + ) { fileInfo.NotNull(nameof(fileInfo)); return new GZipArchive( @@ -52,7 +55,7 @@ public partial class GZipArchive ); } - public static IWritableArchive Open( + public static IWritableArchive OpenArchive( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -68,7 +71,7 @@ public partial class GZipArchive ); } - public static IWritableArchive Open( + public static IWritableArchive OpenArchive( IEnumerable streams, ReaderOptions? readerOptions = null ) @@ -84,7 +87,7 @@ public partial class GZipArchive ); } - public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -98,47 +101,47 @@ public partial class GZipArchive ); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(stream, readerOptions); + return (IWritableAsyncArchive)OpenArchive(stream, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfo, readerOptions); + return (IWritableAsyncArchive)OpenArchive(fileInfo, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(streams, readerOptions); + return (IWritableAsyncArchive)OpenArchive(streams, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfos, readerOptions); + return (IWritableAsyncArchive)OpenArchive(fileInfos, readerOptions); } - public static GZipArchive Create() => new(); + public static GZipArchive CreateArchive() => new(); public static bool IsGZipFile(string filePath) => IsGZipFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/IArchiveFactory.cs b/src/SharpCompress/Archives/IArchiveFactory.cs index b94128d6..eb80e072 100644 --- a/src/SharpCompress/Archives/IArchiveFactory.cs +++ b/src/SharpCompress/Archives/IArchiveFactory.cs @@ -25,21 +25,21 @@ public interface IArchiveFactory : IFactory /// /// An open, readable and seekable stream. /// reading options. - IArchive Open(Stream stream, ReaderOptions? readerOptions = null); + IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null); /// /// Opens an Archive for random access asynchronously. /// /// An open, readable and seekable stream. /// reading options. - IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null); + IAsyncArchive OpenAsyncArchive(Stream stream, ReaderOptions? readerOptions = null); /// /// Constructor with a FileInfo object to an existing file. /// /// the file to open. /// reading options. - IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null); + IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null); /// /// Opens an Archive from a FileInfo object asynchronously. @@ -47,7 +47,7 @@ public interface IArchiveFactory : IFactory /// the file to open. /// reading options. /// Cancellation token. - IAsyncArchive OpenAsync( + IAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Archives/IArchiveOpenable.cs b/src/SharpCompress/Archives/IArchiveOpenable.cs index cf606a65..d884aed0 100644 --- a/src/SharpCompress/Archives/IArchiveOpenable.cs +++ b/src/SharpCompress/Archives/IArchiveOpenable.cs @@ -9,25 +9,28 @@ public interface IArchiveOpenable where TSync : IArchive where TASync : IAsyncArchive { - public static abstract TSync Open(string filePath, ReaderOptions? readerOptions = null); + public static abstract TSync OpenArchive(string filePath, ReaderOptions? readerOptions = null); - public static abstract TSync Open(FileInfo fileInfo, ReaderOptions? readerOptions = null); + public static abstract TSync OpenArchive( + FileInfo fileInfo, + ReaderOptions? readerOptions = null + ); - public static abstract TSync Open(Stream stream, ReaderOptions? readerOptions = null); + public static abstract TSync OpenArchive(Stream stream, ReaderOptions? readerOptions = null); - public static abstract TASync OpenAsync( + public static abstract TASync OpenAsyncArchive( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ); - public static abstract TASync OpenAsync( + public static abstract TASync OpenAsyncArchive( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ); - public static abstract TASync OpenAsync( + public static abstract TASync OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Archives/IMultiArchiveFactory.cs b/src/SharpCompress/Archives/IMultiArchiveFactory.cs index e0064cc0..fc418ad4 100644 --- a/src/SharpCompress/Archives/IMultiArchiveFactory.cs +++ b/src/SharpCompress/Archives/IMultiArchiveFactory.cs @@ -26,21 +26,24 @@ public interface IMultiArchiveFactory : IFactory /// /// /// reading options. - IArchive Open(IReadOnlyList streams, ReaderOptions? readerOptions = null); + IArchive OpenArchive(IReadOnlyList streams, ReaderOptions? readerOptions = null); /// /// Opens a multi-part archive from streams asynchronously. /// /// /// reading options. - IAsyncArchive OpenAsync(IReadOnlyList streams, ReaderOptions? readerOptions = null); + IAsyncArchive OpenAsyncArchive( + IReadOnlyList streams, + ReaderOptions? readerOptions = null + ); /// /// Constructor with IEnumerable Stream objects, multi and split support. /// /// /// reading options. - IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null); + IArchive OpenArchive(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null); /// /// Opens a multi-part archive from files asynchronously. @@ -48,7 +51,7 @@ public interface IMultiArchiveFactory : IFactory /// /// reading options. /// Cancellation token. - IAsyncArchive OpenAsync( + IAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Archives/IMultiArchiveOpenable.cs b/src/SharpCompress/Archives/IMultiArchiveOpenable.cs index 06c5bdd3..53f375c0 100644 --- a/src/SharpCompress/Archives/IMultiArchiveOpenable.cs +++ b/src/SharpCompress/Archives/IMultiArchiveOpenable.cs @@ -10,23 +10,23 @@ public interface IMultiArchiveOpenable where TSync : IArchive where TASync : IAsyncArchive { - public static abstract TSync Open( + public static abstract TSync OpenArchive( IEnumerable fileInfos, ReaderOptions? readerOptions = null ); - public static abstract TSync Open( + public static abstract TSync OpenArchive( IEnumerable streams, ReaderOptions? readerOptions = null ); - public static abstract TASync OpenAsync( + public static abstract TASync OpenAsyncArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ); - public static abstract TASync OpenAsync( + public static abstract TASync OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Archives/IWriteableArchiveFactory.cs b/src/SharpCompress/Archives/IWriteableArchiveFactory.cs index 4fae9f55..f1de642d 100644 --- a/src/SharpCompress/Archives/IWriteableArchiveFactory.cs +++ b/src/SharpCompress/Archives/IWriteableArchiveFactory.cs @@ -16,5 +16,5 @@ public interface IWriteableArchiveFactory : Factories.IFactory /// Creates a new, empty archive, ready to be written. /// /// - IWritableArchive CreateWriteableArchive(); + IWritableArchive CreateArchive(); } diff --git a/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs b/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs index 6ede4790..f3e9a99d 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.Factory.cs @@ -19,7 +19,7 @@ public partial class RarArchive IMultiArchiveOpenable #endif { - public static IRarAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsyncArchive( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -27,10 +27,10 @@ public partial class RarArchive { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IRarAsyncArchive)Open(new FileInfo(path), readerOptions); + return (IRarAsyncArchive)OpenArchive(new FileInfo(path), readerOptions); } - public static IRarArchive Open(string filePath, ReaderOptions? options = null) + public static IRarArchive OpenArchive(string filePath, ReaderOptions? options = null) { filePath.NotNullOrEmpty(nameof(filePath)); var fileInfo = new FileInfo(filePath); @@ -43,7 +43,7 @@ public partial class RarArchive ); } - public static IRarArchive Open(FileInfo fileInfo, ReaderOptions? options = null) + public static IRarArchive OpenArchive(FileInfo fileInfo, ReaderOptions? options = null) { fileInfo.NotNull(nameof(fileInfo)); return new RarArchive( @@ -55,7 +55,7 @@ public partial class RarArchive ); } - public static IRarArchive Open(Stream stream, ReaderOptions? options = null) + public static IRarArchive OpenArchive(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); @@ -67,7 +67,7 @@ public partial class RarArchive return new RarArchive(new SourceStream(stream, _ => null, options ?? new ReaderOptions())); } - public static IRarArchive Open( + public static IRarArchive OpenArchive( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -83,7 +83,10 @@ public partial class RarArchive ); } - public static IRarArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IRarArchive OpenArchive( + IEnumerable streams, + ReaderOptions? readerOptions = null + ) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -96,44 +99,44 @@ public partial class RarArchive ); } - public static IRarAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsyncArchive( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IRarAsyncArchive)Open(stream, readerOptions); + return (IRarAsyncArchive)OpenArchive(stream, readerOptions); } - public static IRarAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IRarAsyncArchive)Open(fileInfo, readerOptions); + return (IRarAsyncArchive)OpenArchive(fileInfo, readerOptions); } - public static IRarAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsyncArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IRarAsyncArchive)Open(streams, readerOptions); + return (IRarAsyncArchive)OpenArchive(streams, readerOptions); } - public static IRarAsyncArchive OpenAsync( + public static IRarAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IRarAsyncArchive)Open(fileInfos, readerOptions); + return (IRarAsyncArchive)OpenArchive(fileInfos, readerOptions); } public static bool IsRarFile(string filePath) => IsRarFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs index 3b39cff4..2abb00ab 100644 --- a/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/SevenZip/SevenZipArchive.Factory.cs @@ -18,7 +18,7 @@ public partial class SevenZipArchive IMultiArchiveOpenable #endif { - public static IAsyncArchive OpenAsync( + public static IAsyncArchive OpenAsyncArchive( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -26,16 +26,16 @@ public partial class SevenZipArchive { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty("path"); - return (IAsyncArchive)Open(new FileInfo(path), readerOptions ?? new ReaderOptions()); + return (IAsyncArchive)OpenArchive(new FileInfo(path), readerOptions ?? new ReaderOptions()); } - public static IArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty("filePath"); - return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); + return OpenArchive(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } - public static IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull("fileInfo"); return new SevenZipArchive( @@ -47,7 +47,7 @@ public partial class SevenZipArchive ); } - public static IArchive Open( + public static IArchive OpenArchive( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -63,7 +63,10 @@ public partial class SevenZipArchive ); } - public static IArchive Open(IEnumerable streams, ReaderOptions? readerOptions = null) + public static IArchive OpenArchive( + IEnumerable streams, + ReaderOptions? readerOptions = null + ) { streams.NotNull(nameof(streams)); var strms = streams.ToArray(); @@ -76,7 +79,7 @@ public partial class SevenZipArchive ); } - public static IArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull("stream"); @@ -90,44 +93,44 @@ public partial class SevenZipArchive ); } - public static IAsyncArchive OpenAsync( + public static IAsyncArchive OpenAsyncArchive( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(stream, readerOptions); + return (IAsyncArchive)OpenArchive(stream, readerOptions); } - public static IAsyncArchive OpenAsync( + public static IAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfo, readerOptions); } - public static IAsyncArchive OpenAsync( + public static IAsyncArchive OpenAsyncArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(streams, readerOptions); + return (IAsyncArchive)OpenArchive(streams, readerOptions); } - public static IAsyncArchive OpenAsync( + public static IAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfos, readerOptions); } public static bool IsSevenZipFile(string filePath) => IsSevenZipFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs index adf12909..5ce2d944 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs @@ -20,13 +20,16 @@ public partial class TarArchive IMultiArchiveOpenable #endif { - public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); + return OpenArchive(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } - public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive( + FileInfo fileInfo, + ReaderOptions? readerOptions = null + ) { fileInfo.NotNull(nameof(fileInfo)); return new TarArchive( @@ -38,7 +41,7 @@ public partial class TarArchive ); } - public static IWritableArchive Open( + public static IWritableArchive OpenArchive( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -54,7 +57,7 @@ public partial class TarArchive ); } - public static IWritableArchive Open( + public static IWritableArchive OpenArchive( IEnumerable streams, ReaderOptions? readerOptions = null ) @@ -70,7 +73,7 @@ public partial class TarArchive ); } - public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -84,54 +87,54 @@ public partial class TarArchive ); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(stream, readerOptions); + return (IWritableAsyncArchive)OpenArchive(stream, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(new FileInfo(path), readerOptions); + return (IWritableAsyncArchive)OpenArchive(new FileInfo(path), readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfo, readerOptions); + return (IWritableAsyncArchive)OpenArchive(fileInfo, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(streams, readerOptions); + return (IWritableAsyncArchive)OpenArchive(streams, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfos, readerOptions); + return (IWritableAsyncArchive)OpenArchive(fileInfos, readerOptions); } public static bool IsTarFile(string filePath) => IsTarFile(new FileInfo(filePath)); @@ -162,5 +165,5 @@ public partial class TarArchive return false; } - public static TarArchive Create() => new(); + public static TarArchive CreateArchive() => new(); } diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs index 770fe048..85110f12 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs @@ -18,13 +18,16 @@ public partial class ZipArchive IMultiArchiveOpenable #endif { - public static IWritableArchive Open(string filePath, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); + return OpenArchive(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } - public static IWritableArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive( + FileInfo fileInfo, + ReaderOptions? readerOptions = null + ) { fileInfo.NotNull(nameof(fileInfo)); return new ZipArchive( @@ -36,7 +39,7 @@ public partial class ZipArchive ); } - public static IWritableArchive Open( + public static IWritableArchive OpenArchive( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -52,7 +55,7 @@ public partial class ZipArchive ); } - public static IWritableArchive Open( + public static IWritableArchive OpenArchive( IEnumerable streams, ReaderOptions? readerOptions = null ) @@ -68,7 +71,7 @@ public partial class ZipArchive ); } - public static IWritableArchive Open(Stream stream, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -82,54 +85,54 @@ public partial class ZipArchive ); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(path, readerOptions); + return (IWritableAsyncArchive)OpenArchive(path, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(stream, readerOptions); + return (IWritableAsyncArchive)OpenArchive(stream, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfo, readerOptions); + return (IWritableAsyncArchive)OpenArchive(fileInfo, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(streams, readerOptions); + return (IWritableAsyncArchive)OpenArchive(streams, readerOptions); } - public static IWritableAsyncArchive OpenAsync( + public static IWritableAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)Open(fileInfos, readerOptions); + return (IWritableAsyncArchive)OpenArchive(fileInfos, readerOptions); } public static bool IsZipFile( @@ -263,7 +266,7 @@ public partial class ZipArchive } } - public static ZipArchive Create() => new(); + public static ZipArchive CreateArchive() => new(); public static async ValueTask IsZipMultiAsync( Stream stream, diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index f222fc31..c7898b68 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -61,12 +61,12 @@ public class GZipFactory #region IArchiveFactory /// - public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) => - GZipArchive.Open(stream, readerOptions); + public IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) => + GZipArchive.OpenArchive(stream, readerOptions); /// - public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => - (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsyncArchive(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)OpenArchive(stream, readerOptions); public override ValueTask IsArchiveAsync( Stream stream, @@ -75,14 +75,14 @@ public class GZipFactory ) => new(IsArchive(stream, password, bufferSize)); /// - public IAsyncArchive OpenAsync( + public IAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfo, readerOptions); } #endregion @@ -90,28 +90,32 @@ public class GZipFactory #region IMultiArchiveFactory /// - public IArchive Open(IReadOnlyList streams, ReaderOptions? readerOptions = null) => - GZipArchive.Open(streams, readerOptions); - - /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null - ) => (IAsyncArchive)Open(streams, readerOptions); + ) => GZipArchive.OpenArchive(streams, readerOptions); /// - public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => - GZipArchive.Open(fileInfos, readerOptions); + public IAsyncArchive OpenAsyncArchive( + IReadOnlyList streams, + ReaderOptions? readerOptions = null + ) => (IAsyncArchive)OpenArchive(streams, readerOptions); /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null + ) => GZipArchive.OpenArchive(fileInfos, readerOptions); + + /// + public IAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfos, readerOptions); } #endregion @@ -164,15 +168,15 @@ public class GZipFactory } /// - public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => - GZipArchive.Open(fileInfo, readerOptions); + public IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) => + GZipArchive.OpenArchive(fileInfo, readerOptions); #endregion #region IWriterFactory /// - public IWriter Open(Stream stream, WriterOptions writerOptions) + public IWriter OpenWriter(Stream stream, WriterOptions writerOptions) { if (writerOptions.CompressionType != CompressionType.GZip) { @@ -182,7 +186,7 @@ public class GZipFactory } /// - public IAsyncWriter OpenAsync( + public IAsyncWriter OpenAsyncWriter( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default @@ -193,7 +197,7 @@ public class GZipFactory { throw new InvalidFormatException("GZip archives only support GZip compression type."); } - return (IAsyncWriter)Open(stream, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } #endregion @@ -201,7 +205,7 @@ public class GZipFactory #region IWriteableArchiveFactory /// - public IWritableArchive CreateWriteableArchive() => GZipArchive.Create(); + public IWritableArchive CreateArchive() => GZipArchive.CreateArchive(); #endregion } diff --git a/src/SharpCompress/Factories/RarFactory.cs b/src/SharpCompress/Factories/RarFactory.cs index faf35ab5..7cc09e3c 100644 --- a/src/SharpCompress/Factories/RarFactory.cs +++ b/src/SharpCompress/Factories/RarFactory.cs @@ -46,26 +46,26 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade #region IArchiveFactory /// - public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) => - RarArchive.Open(stream, readerOptions); + public IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) => + RarArchive.OpenArchive(stream, readerOptions); /// - public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => - (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsyncArchive(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)OpenArchive(stream, readerOptions); /// - public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => - RarArchive.Open(fileInfo, readerOptions); + public IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) => + RarArchive.OpenArchive(fileInfo, readerOptions); /// - public IAsyncArchive OpenAsync( + public IAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfo, readerOptions); } public override ValueTask IsArchiveAsync( @@ -79,28 +79,32 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade #region IMultiArchiveFactory /// - public IArchive Open(IReadOnlyList streams, ReaderOptions? readerOptions = null) => - RarArchive.Open(streams, readerOptions); - - /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null - ) => (IAsyncArchive)Open(streams, readerOptions); + ) => RarArchive.OpenArchive(streams, readerOptions); /// - public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => - RarArchive.Open(fileInfos, readerOptions); + public IAsyncArchive OpenAsyncArchive( + IReadOnlyList streams, + ReaderOptions? readerOptions = null + ) => (IAsyncArchive)OpenArchive(streams, readerOptions); /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null + ) => RarArchive.OpenArchive(fileInfos, readerOptions); + + /// + public IAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfos, readerOptions); } #endregion diff --git a/src/SharpCompress/Factories/SevenZipFactory.cs b/src/SharpCompress/Factories/SevenZipFactory.cs index f430919a..c2ea69f1 100644 --- a/src/SharpCompress/Factories/SevenZipFactory.cs +++ b/src/SharpCompress/Factories/SevenZipFactory.cs @@ -41,26 +41,26 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory #region IArchiveFactory /// - public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) => - SevenZipArchive.Open(stream, readerOptions); + public IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) => + SevenZipArchive.OpenArchive(stream, readerOptions); /// - public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => - (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsyncArchive(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)OpenArchive(stream, readerOptions); /// - public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => - SevenZipArchive.Open(fileInfo, readerOptions); + public IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) => + SevenZipArchive.OpenArchive(fileInfo, readerOptions); /// - public IAsyncArchive OpenAsync( + public IAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfo, readerOptions); } public override ValueTask IsArchiveAsync( @@ -74,28 +74,32 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory #region IMultiArchiveFactory /// - public IArchive Open(IReadOnlyList streams, ReaderOptions? readerOptions = null) => - SevenZipArchive.Open(streams, readerOptions); - - /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null - ) => (IAsyncArchive)Open(streams, readerOptions); + ) => SevenZipArchive.OpenArchive(streams, readerOptions); /// - public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => - SevenZipArchive.Open(fileInfos, readerOptions); + public IAsyncArchive OpenAsyncArchive( + IReadOnlyList streams, + ReaderOptions? readerOptions = null + ) => (IAsyncArchive)OpenArchive(streams, readerOptions); /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null + ) => SevenZipArchive.OpenArchive(fileInfos, readerOptions); + + /// + public IAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfos, readerOptions); } #endregion diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index a664b243..35827ea0 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -72,26 +72,26 @@ public class TarFactory #region IArchiveFactory /// - public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) => - TarArchive.Open(stream, readerOptions); + public IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) => + TarArchive.OpenArchive(stream, readerOptions); /// - public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => - (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsyncArchive(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)OpenArchive(stream, readerOptions); /// - public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => - TarArchive.Open(fileInfo, readerOptions); + public IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) => + TarArchive.OpenArchive(fileInfo, readerOptions); /// - public IAsyncArchive OpenAsync( + public IAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfo, readerOptions); } #endregion @@ -99,28 +99,32 @@ public class TarFactory #region IMultiArchiveFactory /// - public IArchive Open(IReadOnlyList streams, ReaderOptions? readerOptions = null) => - TarArchive.Open(streams, readerOptions); - - /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null - ) => (IAsyncArchive)Open(streams, readerOptions); + ) => TarArchive.OpenArchive(streams, readerOptions); /// - public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => - TarArchive.Open(fileInfos, readerOptions); + public IAsyncArchive OpenAsyncArchive( + IReadOnlyList streams, + ReaderOptions? readerOptions = null + ) => (IAsyncArchive)OpenArchive(streams, readerOptions); /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null + ) => TarArchive.OpenArchive(fileInfos, readerOptions); + + /// + public IAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfos, readerOptions); } #endregion @@ -290,18 +294,18 @@ public class TarFactory #region IWriterFactory /// - public IWriter Open(Stream stream, WriterOptions writerOptions) => + public IWriter OpenWriter(Stream stream, WriterOptions writerOptions) => new TarWriter(stream, new TarWriterOptions(writerOptions)); /// - public IAsyncWriter OpenAsync( + public IAsyncWriter OpenAsyncWriter( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(stream, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } #endregion @@ -309,7 +313,7 @@ public class TarFactory #region IWriteableArchiveFactory /// - public IWritableArchive CreateWriteableArchive() => TarArchive.Create(); + public IWritableArchive CreateArchive() => TarArchive.CreateArchive(); #endregion } diff --git a/src/SharpCompress/Factories/ZipFactory.cs b/src/SharpCompress/Factories/ZipFactory.cs index bdf8fedd..8487fa5d 100644 --- a/src/SharpCompress/Factories/ZipFactory.cs +++ b/src/SharpCompress/Factories/ZipFactory.cs @@ -139,26 +139,26 @@ public class ZipFactory #region IArchiveFactory /// - public IArchive Open(Stream stream, ReaderOptions? readerOptions = null) => - ZipArchive.Open(stream, readerOptions); + public IArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) => + ZipArchive.OpenArchive(stream, readerOptions); /// - public IAsyncArchive OpenAsync(Stream stream, ReaderOptions? readerOptions = null) => - (IAsyncArchive)Open(stream, readerOptions); + public IAsyncArchive OpenAsyncArchive(Stream stream, ReaderOptions? readerOptions = null) => + (IAsyncArchive)OpenArchive(stream, readerOptions); /// - public IArchive Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) => - ZipArchive.Open(fileInfo, readerOptions); + public IArchive OpenArchive(FileInfo fileInfo, ReaderOptions? readerOptions = null) => + ZipArchive.OpenArchive(fileInfo, readerOptions); /// - public IAsyncArchive OpenAsync( + public IAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfo, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfo, readerOptions); } #endregion @@ -166,28 +166,32 @@ public class ZipFactory #region IMultiArchiveFactory /// - public IArchive Open(IReadOnlyList streams, ReaderOptions? readerOptions = null) => - ZipArchive.Open(streams, readerOptions); - - /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null - ) => (IAsyncArchive)Open(streams, readerOptions); + ) => ZipArchive.OpenArchive(streams, readerOptions); /// - public IArchive Open(IReadOnlyList fileInfos, ReaderOptions? readerOptions = null) => - ZipArchive.Open(fileInfos, readerOptions); + public IAsyncArchive OpenAsyncArchive( + IReadOnlyList streams, + ReaderOptions? readerOptions = null + ) => (IAsyncArchive)OpenArchive(streams, readerOptions); /// - public IAsyncArchive OpenAsync( + public IArchive OpenArchive( + IReadOnlyList fileInfos, + ReaderOptions? readerOptions = null + ) => ZipArchive.OpenArchive(fileInfos, readerOptions); + + /// + public IAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncArchive)Open(fileInfos, readerOptions); + return (IAsyncArchive)OpenArchive(fileInfos, readerOptions); } #endregion @@ -214,18 +218,18 @@ public class ZipFactory #region IWriterFactory /// - public IWriter Open(Stream stream, WriterOptions writerOptions) => + public IWriter OpenWriter(Stream stream, WriterOptions writerOptions) => new ZipWriter(stream, new ZipWriterOptions(writerOptions)); /// - public IAsyncWriter OpenAsync( + public IAsyncWriter OpenAsyncWriter( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(stream, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } #endregion @@ -233,7 +237,7 @@ public class ZipFactory #region IWriteableArchiveFactory /// - public IWritableArchive CreateWriteableArchive() => ZipArchive.Create(); + public IWritableArchive CreateArchive() => ZipArchive.CreateArchive(); #endregion } diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index 7bf72fe9..e104e67d 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -11,10 +11,10 @@ namespace SharpCompress.Readers; public static class ReaderFactory { - public static IReader Open(string filePath, ReaderOptions? options = null) + public static IReader OpenReader(string filePath, ReaderOptions? options = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), options); + return OpenReader(new FileInfo(filePath), options); } /// @@ -24,20 +24,20 @@ public static class ReaderFactory /// /// /// - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( string filePath, ReaderOptions? options = null, CancellationToken cancellationToken = default ) { filePath.NotNullOrEmpty(nameof(filePath)); - return OpenAsync(new FileInfo(filePath), options, cancellationToken); + return OpenAsyncReader(new FileInfo(filePath), options, cancellationToken); } - public static IReader Open(FileInfo fileInfo, ReaderOptions? options = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? options = null) { options ??= new ReaderOptions { LeaveStreamOpen = false }; - return Open(fileInfo.OpenRead(), options); + return OpenReader(fileInfo.OpenRead(), options); } /// @@ -47,14 +47,14 @@ public static class ReaderFactory /// /// /// - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? options = null, CancellationToken cancellationToken = default ) { options ??= new ReaderOptions { LeaveStreamOpen = false }; - return OpenAsync(fileInfo.OpenRead(), options, cancellationToken); + return OpenAsyncReader(fileInfo.OpenRead(), options, cancellationToken); } /// @@ -63,7 +63,7 @@ public static class ReaderFactory /// /// /// - public static IReader Open(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); options ??= new ReaderOptions() { LeaveStreamOpen = false }; @@ -110,7 +110,7 @@ public static class ReaderFactory ); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options = null, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs b/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs index 57a22b54..7fd8aec1 100644 --- a/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs +++ b/src/SharpCompress/Writers/GZip/GZipWriter.Factory.cs @@ -7,52 +7,52 @@ namespace SharpCompress.Writers.GZip; public partial class GZipWriter : IWriterOpenable { - public static IWriter Open(string filePath, GZipWriterOptions writerOptions) + public static IWriter OpenWriter(string filePath, GZipWriterOptions writerOptions) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), writerOptions); + return OpenWriter(new FileInfo(filePath), writerOptions); } - public static IWriter Open(FileInfo fileInfo, GZipWriterOptions writerOptions) + public static IWriter OpenWriter(FileInfo fileInfo, GZipWriterOptions writerOptions) { fileInfo.NotNull(nameof(fileInfo)); return new GZipWriter(fileInfo.OpenWrite(), writerOptions); } - public static IWriter Open(Stream stream, GZipWriterOptions writerOptions) + public static IWriter OpenWriter(Stream stream, GZipWriterOptions writerOptions) { stream.NotNull(nameof(stream)); return new GZipWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( string path, GZipWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(path, writerOptions); + return (IAsyncWriter)OpenWriter(path, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( Stream stream, GZipWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(stream, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( FileInfo fileInfo, GZipWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(fileInfo, writerOptions); + return (IAsyncWriter)OpenWriter(fileInfo, writerOptions); } } #endif diff --git a/src/SharpCompress/Writers/IWriterFactory.cs b/src/SharpCompress/Writers/IWriterFactory.cs index 9615f078..f8bd8bde 100644 --- a/src/SharpCompress/Writers/IWriterFactory.cs +++ b/src/SharpCompress/Writers/IWriterFactory.cs @@ -6,9 +6,9 @@ namespace SharpCompress.Writers; public interface IWriterFactory : IFactory { - IWriter Open(Stream stream, WriterOptions writerOptions); + IWriter OpenWriter(Stream stream, WriterOptions writerOptions); - IAsyncWriter OpenAsync( + IAsyncWriter OpenAsyncWriter( Stream stream, WriterOptions writerOptions, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Writers/IWriterOpenable.cs b/src/SharpCompress/Writers/IWriterOpenable.cs index f856a309..14b98f71 100644 --- a/src/SharpCompress/Writers/IWriterOpenable.cs +++ b/src/SharpCompress/Writers/IWriterOpenable.cs @@ -7,10 +7,10 @@ namespace SharpCompress.Writers; public interface IWriterOpenable where TWriterOptions : WriterOptions { - public static abstract IWriter Open(string filePath, TWriterOptions writerOptions); + public static abstract IWriter OpenWriter(string filePath, TWriterOptions writerOptions); - public static abstract IWriter Open(FileInfo fileInfo, TWriterOptions writerOptions); - public static abstract IWriter Open(Stream stream, TWriterOptions writerOptions); + public static abstract IWriter OpenWriter(FileInfo fileInfo, TWriterOptions writerOptions); + public static abstract IWriter OpenWriter(Stream stream, TWriterOptions writerOptions); /// /// Opens a Writer asynchronously. @@ -20,19 +20,19 @@ public interface IWriterOpenable /// Writer options. /// Cancellation token. /// A task that returns an IWriter. - public static abstract IAsyncWriter OpenAsync( + public static abstract IAsyncWriter OpenAsyncWriter( Stream stream, TWriterOptions writerOptions, CancellationToken cancellationToken = default ); - public static abstract IAsyncWriter OpenAsync( + public static abstract IAsyncWriter OpenAsyncWriter( string filePath, TWriterOptions writerOptions, CancellationToken cancellationToken = default ); - public static abstract IAsyncWriter OpenAsync( + public static abstract IAsyncWriter OpenAsyncWriter( FileInfo fileInfo, TWriterOptions writerOptions, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs b/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs index 059fd62e..c5f9c846 100644 --- a/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs +++ b/src/SharpCompress/Writers/Tar/TarWriter.Factory.cs @@ -7,52 +7,52 @@ namespace SharpCompress.Writers.Tar; public partial class TarWriter : IWriterOpenable { - public static IWriter Open(string filePath, TarWriterOptions writerOptions) + public static IWriter OpenWriter(string filePath, TarWriterOptions writerOptions) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), writerOptions); + return OpenWriter(new FileInfo(filePath), writerOptions); } - public static IWriter Open(FileInfo fileInfo, TarWriterOptions writerOptions) + public static IWriter OpenWriter(FileInfo fileInfo, TarWriterOptions writerOptions) { fileInfo.NotNull(nameof(fileInfo)); return new TarWriter(fileInfo.OpenWrite(), writerOptions); } - public static IWriter Open(Stream stream, TarWriterOptions writerOptions) + public static IWriter OpenWriter(Stream stream, TarWriterOptions writerOptions) { stream.NotNull(nameof(stream)); return new TarWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( string path, TarWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(path, writerOptions); + return (IAsyncWriter)OpenWriter(path, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( Stream stream, TarWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(stream, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( FileInfo fileInfo, TarWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(fileInfo, writerOptions); + return (IAsyncWriter)OpenWriter(fileInfo, writerOptions); } } #endif diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index 675ef675..0237e985 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -28,7 +28,7 @@ public static class WriterFactory return Open(fileInfo.OpenWrite(), archiveType, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( string filePath, ArchiveType archiveType, WriterOptions writerOptions, @@ -36,10 +36,10 @@ public static class WriterFactory ) { filePath.NotNullOrEmpty(nameof(filePath)); - return OpenAsync(new FileInfo(filePath), archiveType, writerOptions, cancellationToken); + return OpenAsyncWriter(new FileInfo(filePath), archiveType, writerOptions, cancellationToken); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( FileInfo fileInfo, ArchiveType archiveType, WriterOptions writerOptions, @@ -47,7 +47,7 @@ public static class WriterFactory ) { fileInfo.NotNull(nameof(fileInfo)); - return OpenAsync( + return OpenAsyncWriter( fileInfo.Open(FileMode.Create, FileAccess.Write), archiveType, writerOptions, @@ -63,7 +63,7 @@ public static class WriterFactory if (factory != null) { - return factory.Open(stream, writerOptions); + return factory.OpenWriter(stream, writerOptions); } throw new NotSupportedException("Archive Type does not have a Writer: " + archiveType); @@ -77,7 +77,7 @@ public static class WriterFactory /// Writer options. /// Cancellation token. /// A task that returns an IWriter. - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( Stream stream, ArchiveType archiveType, WriterOptions writerOptions, @@ -90,7 +90,7 @@ public static class WriterFactory if (factory != null) { - return factory.OpenAsync(stream, writerOptions, cancellationToken); + return factory.OpenAsyncWriter(stream, writerOptions, cancellationToken); } throw new NotSupportedException("Archive Type does not have a Writer: " + archiveType); diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs b/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs index 44825a72..0df1a81c 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.Factory.cs @@ -7,52 +7,52 @@ namespace SharpCompress.Writers.Zip; public partial class ZipWriter : IWriterOpenable { - public static IWriter Open(string filePath, ZipWriterOptions writerOptions) + public static IWriter OpenWriter(string filePath, ZipWriterOptions writerOptions) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), writerOptions); + return OpenWriter(new FileInfo(filePath), writerOptions); } - public static IWriter Open(FileInfo fileInfo, ZipWriterOptions writerOptions) + public static IWriter OpenWriter(FileInfo fileInfo, ZipWriterOptions writerOptions) { fileInfo.NotNull(nameof(fileInfo)); return new ZipWriter(fileInfo.OpenWrite(), writerOptions); } - public static IWriter Open(Stream stream, ZipWriterOptions writerOptions) + public static IWriter OpenWriter(Stream stream, ZipWriterOptions writerOptions) { stream.NotNull(nameof(stream)); return new ZipWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( string path, ZipWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(path, writerOptions); + return (IAsyncWriter)OpenWriter(path, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( Stream stream, ZipWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(stream, writerOptions); + return (IAsyncWriter)OpenWriter(stream, writerOptions); } - public static IAsyncWriter OpenAsync( + public static IAsyncWriter OpenAsyncWriter( FileInfo fileInfo, ZipWriterOptions writerOptions, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncWriter)Open(fileInfo, writerOptions); + return (IAsyncWriter)OpenWriter(fileInfo, writerOptions); } } #endif diff --git a/tests/SharpCompress.Performance/Program.cs b/tests/SharpCompress.Performance/Program.cs index d445208a..c6d29ec6 100644 --- a/tests/SharpCompress.Performance/Program.cs +++ b/tests/SharpCompress.Performance/Program.cs @@ -30,14 +30,14 @@ using (var __ = JetbrainsProfiler.Cpu($"/Users/adam/temp/")) for (int i = 0; i < 50; i++) { - using var found = ArchiveFactory.Open(arcs[0]); + using var found = ArchiveFactory.OpenArchive(arcs[0]); foreach (var entry in found.Entries.Where(entry => !entry.IsDirectory)) { Console.WriteLine($"Extracting {entry.Key}"); using var entryStream = entry.OpenEntryStream(); entryStream.CopyTo(Stream.Null); } - /*using var found = ReaderFactory.Open(arcs[0]); + /*using var found = ReaderFactory.OpenReader(arcs[0]); while (found.MoveToNextEntry()) { var entry = found.Entry; diff --git a/tests/SharpCompress.Test/Ace/AceReaderTests.cs b/tests/SharpCompress.Test/Ace/AceReaderTests.cs index b6cf97f2..60c51c12 100644 --- a/tests/SharpCompress.Test/Ace/AceReaderTests.cs +++ b/tests/SharpCompress.Test/Ace/AceReaderTests.cs @@ -53,7 +53,7 @@ namespace SharpCompress.Test.Ace var exception = Assert.Throws(() => DoMultiReader( ["Ace.store.split.ace", "Ace.store.split.c01"], - streams => AceReader.Open(streams) + streams => AceReader.OpenReader(streams) ) ); } diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index 1fe1b024..e392f9d3 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -43,7 +43,7 @@ public class ArchiveTests : ReaderTests foreach (var path in testArchives) { using ( - var stream = SharpCompressStream.Create( + var stream = SharpCompressStream.CreateArchive( File.OpenRead(path), leaveOpen: true, throwOnDispose: true @@ -52,7 +52,7 @@ public class ArchiveTests : ReaderTests { try { - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); Assert.True(archive.IsSolid); using (var reader = archive.ExtractAllEntries()) { @@ -123,13 +123,13 @@ public class ArchiveTests : ReaderTests foreach (var path in testArchives) { using ( - var stream = SharpCompressStream.Create( + var stream = SharpCompressStream.CreateArchive( File.OpenRead(path), leaveOpen: true, throwOnDispose: true ) ) - using (var archive = archiveFactory.Open(stream, readerOptions)) + using (var archive = archiveFactory.OpenArchive(stream, readerOptions)) { try { @@ -168,7 +168,7 @@ public class ArchiveTests : ReaderTests ) { using ( - var archive = ArchiveFactory.Open( + var archive = ArchiveFactory.OpenArchive( testArchives.Select(a => new FileInfo(a)), readerOptions ) @@ -200,7 +200,7 @@ public class ArchiveTests : ReaderTests ) { using ( - var archive = ArchiveFactory.Open( + var archive = ArchiveFactory.OpenArchive( testArchives.Select(f => new FileInfo(f)), readerOptions ) @@ -235,7 +235,10 @@ public class ArchiveTests : ReaderTests ) { var src = testArchives.ToArray(); - using var archive = ArchiveFactory.Open(src.Select(f => new FileInfo(f)), readerOptions); + using var archive = ArchiveFactory.OpenArchive( + src.Select(f => new FileInfo(f)), + readerOptions + ); var idx = 0; foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -260,7 +263,7 @@ public class ArchiveTests : ReaderTests ) { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); - using (var archive = ArchiveFactory.Open(new FileInfo(testArchive), readerOptions)) + using (var archive = ArchiveFactory.OpenArchive(new FileInfo(testArchive), readerOptions)) { archive.WriteToDirectory(SCRATCH_FILES_PATH); } @@ -274,7 +277,7 @@ public class ArchiveTests : ReaderTests ) { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); - using (var archive = archiveFactory.Open(new FileInfo(testArchive), readerOptions)) + using (var archive = archiveFactory.OpenArchive(new FileInfo(testArchive), readerOptions)) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -302,7 +305,7 @@ public class ArchiveTests : ReaderTests } var expected = new Stack(fileOrder.Split(' ')); testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); - using var archive = ArchiveFactory.Open(testArchive, readerOptions); + using var archive = ArchiveFactory.OpenArchive(testArchive, readerOptions); foreach (var entry in archive.Entries) { Assert.Equal(expected.Pop(), entry.Key); @@ -315,7 +318,7 @@ public class ArchiveTests : ReaderTests protected void ArchiveFileReadEx(string testArchive) { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); - using (var archive = ArchiveFactory.Open(testArchive)) + using (var archive = ArchiveFactory.OpenArchive(testArchive)) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -337,7 +340,7 @@ public class ArchiveTests : ReaderTests protected void ArchiveDeltaDistanceRead(string testArchive) { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); - using var archive = ArchiveFactory.Open(testArchive); + using var archive = ArchiveFactory.OpenArchive(testArchive); foreach (var entry in archive.Entries) { if (!entry.IsDirectory) @@ -377,7 +380,7 @@ public class ArchiveTests : ReaderTests { writerOptions.CompressionLevel = compressionLevel.Value; } - return WriterFactory.Open(stream, ArchiveType.Zip, writerOptions); + return WriterFactory.OpenArchive(stream, ArchiveType.Zip, writerOptions); } protected static IAsyncWriter CreateWriterWithLevelAsync( @@ -391,7 +394,11 @@ public class ArchiveTests : ReaderTests { writerOptions.CompressionLevel = compressionLevel.Value; } - return WriterFactory.OpenAsync(new AsyncOnlyStream(stream), ArchiveType.Zip, writerOptions); + return WriterFactory.OpenAsyncArchive( + new AsyncOnlyStream(stream), + ArchiveType.Zip, + writerOptions + ); } /// @@ -403,7 +410,7 @@ public class ArchiveTests : ReaderTests ) { zipStream.Position = 0; - using var archive = ArchiveFactory.Open(zipStream); + using var archive = ArchiveFactory.OpenArchive(zipStream); Assert.Equal(expectedFiles.Count, archive.Entries.Count()); foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) @@ -554,7 +561,7 @@ public class ArchiveTests : ReaderTests ) { zipStream.Position = 0; - using var archive = ArchiveFactory.Open(zipStream); + using var archive = ArchiveFactory.OpenArchive(zipStream); foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) { @@ -571,7 +578,7 @@ public class ArchiveTests : ReaderTests ) { zipStream.Position = 0; - using var archive = ArchiveFactory.Open(zipStream); + using var archive = ArchiveFactory.OpenArchive(zipStream); var entry = archive.Entries.FirstOrDefault(e => e.Key == entryName && !e.IsDirectory); Assert.NotNull(entry); @@ -608,14 +615,17 @@ public class ArchiveTests : ReaderTests foreach (var path in testArchives) { using ( - var stream = SharpCompressStream.Create( + var stream = SharpCompressStream.CreateArchive( File.OpenRead(path), leaveOpen: true, throwOnDispose: true ) ) await using ( - var archive = archiveFactory.OpenAsync(new AsyncOnlyStream(stream), readerOptions) + var archive = archiveFactory.OpenAsyncArchive( + new AsyncOnlyStream(stream), + readerOptions + ) ) { try @@ -651,9 +661,9 @@ public class ArchiveTests : ReaderTests // Open with a pre-wrapped stream using (var fileStream = File.OpenRead(testArchive)) - using (var wrappedStream = SharpCompressStream.Create(fileStream, bufferSize: 32768)) + using (var wrappedStream = SharpCompressStream.CreateArchive(fileStream, bufferSize: 32768)) await using ( - var archive = await ArchiveFactory.OpenAsync(new AsyncOnlyStream(wrappedStream)) + var archive = await ArchiveFactory.OpenAsyncArchive(new AsyncOnlyStream(wrappedStream)) ) { Assert.Equal(ArchiveType.Zip, archive.Type); @@ -669,7 +679,9 @@ public class ArchiveTests : ReaderTests var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.noEmptyDirs.zip"); using (var stream = File.OpenRead(testArchive)) - await using (var archive = await ArchiveFactory.OpenAsync(new AsyncOnlyStream(stream))) + await using ( + var archive = await ArchiveFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)) + ) { Assert.Equal(ArchiveType.Zip, archive.Type); Assert.Equal(3, await archive.EntriesAsync.CountAsync()); diff --git a/tests/SharpCompress.Test/Arj/ArjReaderTests.cs b/tests/SharpCompress.Test/Arj/ArjReaderTests.cs index bad98ec8..054c18f6 100644 --- a/tests/SharpCompress.Test/Arj/ArjReaderTests.cs +++ b/tests/SharpCompress.Test/Arj/ArjReaderTests.cs @@ -54,7 +54,7 @@ namespace SharpCompress.Test.Arj "Arj.store.split.a04", "Arj.store.split.a05", ], - streams => ArjReader.Open(streams) + streams => ArjReader.OpenReader(streams) ) ); } diff --git a/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs b/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs index e0fa0583..620f307c 100644 --- a/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs +++ b/tests/SharpCompress.Test/BZip2/BZip2ReaderTests.cs @@ -16,6 +16,6 @@ public class BZip2ReaderTests : ReaderTests Stream stream = new MemoryStream( new byte[] { 0x42, 0x5a, 0x68, 0x34, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x35 } ); - Assert.Throws(typeof(InvalidOperationException), () => ReaderFactory.Open(stream)); + Assert.Throws(typeof(InvalidOperationException), () => ReaderFactory.OpenReader(stream)); } } diff --git a/tests/SharpCompress.Test/ExtractAll.cs b/tests/SharpCompress.Test/ExtractAll.cs index d2643fcd..1e047bc0 100644 --- a/tests/SharpCompress.Test/ExtractAll.cs +++ b/tests/SharpCompress.Test/ExtractAll.cs @@ -23,7 +23,7 @@ public class ExtractAllTests : TestBase var testArchive = Path.Combine(TEST_ARCHIVES_PATH, archivePath); var options = new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }; - await using var archive = await ArchiveFactory.OpenAsync(testArchive); + await using var archive = await ArchiveFactory.OpenAsyncArchive(testArchive); await archive.WriteToDirectoryAsync(SCRATCH_FILES_PATH, options); } @@ -40,7 +40,7 @@ public class ExtractAllTests : TestBase var testArchive = Path.Combine(TEST_ARCHIVES_PATH, archivePath); var options = new ExtractionOptions() { ExtractFullPath = true, Overwrite = true }; - using var archive = ArchiveFactory.Open(testArchive); + using var archive = ArchiveFactory.OpenArchive(testArchive); archive.WriteToDirectory(SCRATCH_FILES_PATH, options); } } diff --git a/tests/SharpCompress.Test/ExtractAllEntriesTests.cs b/tests/SharpCompress.Test/ExtractAllEntriesTests.cs index 007f620f..57bbcc50 100644 --- a/tests/SharpCompress.Test/ExtractAllEntriesTests.cs +++ b/tests/SharpCompress.Test/ExtractAllEntriesTests.cs @@ -18,7 +18,7 @@ public class ExtractAllEntriesTests : TestBase { var archivePath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.zip"); - using var archive = ArchiveFactory.Open(archivePath); + using var archive = ArchiveFactory.OpenArchive(archivePath); Assert.Throws(() => { using var reader = archive.ExtractAllEntries(); @@ -30,7 +30,7 @@ public class ExtractAllEntriesTests : TestBase { var archivePath = Path.Combine(TEST_ARCHIVES_PATH, "Rar.solid.rar"); - using var archive = ArchiveFactory.Open(archivePath); + using var archive = ArchiveFactory.OpenArchive(archivePath); Assert.True(archive.IsSolid); // Calculate total size like user code does diff --git a/tests/SharpCompress.Test/ExtractionTests.cs b/tests/SharpCompress.Test/ExtractionTests.cs index 04b5b08f..2edb8110 100644 --- a/tests/SharpCompress.Test/ExtractionTests.cs +++ b/tests/SharpCompress.Test/ExtractionTests.cs @@ -27,7 +27,7 @@ public class ExtractionTests : TestBase using (var stream = File.Create(testArchive)) { using var writer = (ZipWriter) - WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate); + WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate); // Create a test file to add to the archive var testFilePath = Path.Combine(SCRATCH2_FILES_PATH, "testfile.txt"); @@ -39,7 +39,7 @@ public class ExtractionTests : TestBase // Extract the archive - this should succeed regardless of path casing using (var stream = File.OpenRead(testArchive)) { - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); // This should not throw an exception even if Path.GetFullPath returns // a path with different casing than the actual directory @@ -72,7 +72,7 @@ public class ExtractionTests : TestBase using (var stream = File.Create(testArchive)) { using var writer = (ZipWriter) - WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate); + WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate); var testFilePath = Path.Combine(SCRATCH2_FILES_PATH, "testfile2.txt"); File.WriteAllText(testFilePath, "Test content"); @@ -84,7 +84,7 @@ public class ExtractionTests : TestBase // Extract the archive - this should throw an exception for path traversal using (var stream = File.OpenRead(testArchive)) { - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); var exception = Assert.Throws(() => reader.WriteAllToDirectory( diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index 5d1a7fb2..a35b295f 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -26,7 +26,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)); await reader.WriteAllToDirectoryAsync( SCRATCH_FILES_PATH, @@ -51,7 +51,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { @@ -74,7 +74,7 @@ public class AsyncTests : TestBase public async ValueTask Archive_Entry_Async_Open_Stream() { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"); - await using var archive = await ArchiveFactory.OpenAsync( + await using var archive = await ArchiveFactory.OpenAsyncArchive( new AsyncOnlyStream(File.OpenRead(testArchive)) ); @@ -106,7 +106,7 @@ public class AsyncTests : TestBase await using (var stream = File.Create(outputPath)) #endif using ( - var writer = WriterFactory.OpenAsync( + var writer = WriterFactory.OpenAsyncArchive( new AsyncOnlyStream(stream), ArchiveType.Zip, CompressionType.Deflate @@ -125,7 +125,7 @@ public class AsyncTests : TestBase // Verify the archive was created and contains the entry Assert.True(File.Exists(outputPath)); - await using var archive = ZipArchive.OpenAsync(outputPath); + await using var archive = ZipArchive.OpenAsyncArchive(outputPath); Assert.Single(await archive.EntriesAsync.Where(e => !e.IsDirectory).ToListAsync()); } @@ -141,7 +141,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsyncArchive( new AsyncOnlyStream(stream), cancellationToken: cts.Token ); @@ -195,7 +195,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index 98bcf41a..7ebf2425 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -22,7 +22,7 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) #endif - using (var archive = ArchiveFactory.Open(new AsyncOnlyStream(stream))) + using (var archive = ArchiveFactory.OpenArchive(new AsyncOnlyStream(stream))) { var entry = archive.Entries.First(); await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); @@ -49,7 +49,7 @@ public class GZipArchiveAsyncTests : ArchiveTests await using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) #endif { - await using (var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream))) + await using (var archive = GZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream))) { var entry = await archive.EntriesAsync.FirstAsync(); await entry.WriteToFileAsync(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); @@ -77,7 +77,7 @@ public class GZipArchiveAsyncTests : ArchiveTests #else await using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); #endif - await using (var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream))) + await using (var archive = GZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream))) { Assert.Throws(() => archive.AddEntry("jpg\\test.jpg", jpg)); await archive.SaveToAsync(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); @@ -98,7 +98,7 @@ public class GZipArchiveAsyncTests : ArchiveTests inputStream.Position = 0; } - using var archive = GZipArchive.Open(new AsyncOnlyStream(inputStream)); + using var archive = GZipArchive.OpenArchive(new AsyncOnlyStream(inputStream)); var archiveEntry = archive.Entries.First(); MemoryStream tarStream; @@ -148,7 +148,7 @@ public class GZipArchiveAsyncTests : ArchiveTests public async Task TestGzCrcWithMostSignificantBitNotNegative_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - await using var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + await using var archive = GZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream)); await foreach (var entry in archive.EntriesAsync.Where(entry => !entry.IsDirectory)) { Assert.InRange(entry.Crc, 0L, 0xFFFFFFFFL); @@ -159,7 +159,7 @@ public class GZipArchiveAsyncTests : ArchiveTests public async Task TestGzArchiveTypeGzip_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - await using var archive = GZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + await using var archive = GZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream)); Assert.Equal(archive.Type, ArchiveType.GZip); } } diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveDirectoryTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveDirectoryTests.cs index 201effcb..d1d7fbf1 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveDirectoryTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveDirectoryTests.cs @@ -10,7 +10,7 @@ public class GZipArchiveDirectoryTests : TestBase [Fact] public void GZipArchive_AddDirectoryEntry_ThrowsNotSupportedException() { - using var archive = GZipArchive.Create(); + using var archive = GZipArchive.CreateArchive(); Assert.Throws(() => archive.AddDirectoryEntry("test-dir", DateTime.Now) diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs index c8a87327..448406a2 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveTests.cs @@ -17,7 +17,7 @@ public class GZipArchiveTests : ArchiveTests public void GZip_Archive_Generic() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) - using (var archive = ArchiveFactory.Open(stream)) + using (var archive = ArchiveFactory.OpenArchive(stream)) { var entry = archive.Entries.First(); entry.WriteToFile(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); @@ -39,7 +39,7 @@ public class GZipArchiveTests : ArchiveTests public void GZip_Archive() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"))) - using (var archive = GZipArchive.Open(stream)) + using (var archive = GZipArchive.OpenArchive(stream)) { var entry = archive.Entries.First(); entry.WriteToFile(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); @@ -62,7 +62,7 @@ public class GZipArchiveTests : ArchiveTests { var jpg = Path.Combine(ORIGINAL_FILES_PATH, "jpg", "test.jpg"); using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - using var archive = GZipArchive.Open(stream); + using var archive = GZipArchive.OpenArchive(stream); Assert.Throws(() => archive.AddEntry("jpg\\test.jpg", jpg)); archive.SaveTo(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); } @@ -76,7 +76,7 @@ public class GZipArchiveTests : ArchiveTests fileStream.CopyTo(inputStream); inputStream.Position = 0; } - using var archive = GZipArchive.Open(inputStream); + using var archive = GZipArchive.OpenArchive(inputStream); var archiveEntry = archive.Entries.First(); MemoryStream tarStream; @@ -110,7 +110,7 @@ public class GZipArchiveTests : ArchiveTests public void TestGzCrcWithMostSignificantBitNotNegative() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - using var archive = GZipArchive.Open(stream); + using var archive = GZipArchive.OpenArchive(stream); //process all entries in solid archive until the one we want to test foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -122,7 +122,7 @@ public class GZipArchiveTests : ArchiveTests public void TestGzArchiveTypeGzip() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - using var archive = GZipArchive.Open(stream); + using var archive = GZipArchive.OpenArchive(stream); Assert.Equal(archive.Type, ArchiveType.GZip); } @@ -137,7 +137,7 @@ public class GZipArchiveTests : ArchiveTests // Create a non-seekable wrapper around the MemoryStream using var nonSeekableStream = new NonSeekableStream(buffer); - using var reader = SharpCompress.Readers.GZip.GZipReader.Open(nonSeekableStream); + using var reader = SharpCompress.Readers.GZip.GZipReader.OpenReader(nonSeekableStream); // Verify we can move to the first entry and read it without exceptions Assert.True(reader.MoveToNextEntry()); diff --git a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs index 58f5d9dc..8f47f8ab 100644 --- a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs @@ -22,7 +22,7 @@ public class GZipReaderAsyncTests : ReaderTests { //read only as GZip item using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { Assert.NotEqual(0, reader.Entry.Size); @@ -70,7 +70,7 @@ public class GZipReaderAsyncTests : ReaderTests bufferSize: options.BufferSize ); using var testStream = new TestStream(protectedStream); - await using (var reader = ReaderFactory.OpenAsync(testStream, options, default)) + await using (var reader = ReaderFactory.OpenAsyncArchive(testStream, options, default)) { await UseReaderAsync(reader, expectedCompression); protectedStream.ThrowOnDispose = false; diff --git a/tests/SharpCompress.Test/GZip/GZipReaderTests.cs b/tests/SharpCompress.Test/GZip/GZipReaderTests.cs index 187cd80c..c382a471 100644 --- a/tests/SharpCompress.Test/GZip/GZipReaderTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipReaderTests.cs @@ -18,7 +18,7 @@ public class GZipReaderTests : ReaderTests { //read only as GZip itme using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - using var reader = GZipReader.Open(new SharpCompressStream(stream)); + using var reader = GZipReader.OpenReader(new SharpCompressStream(stream)); while (reader.MoveToNextEntry()) // Crash here { Assert.NotEqual(0, reader.Entry.Size); diff --git a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs index c93a14c9..80a59a59 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs @@ -24,7 +24,7 @@ public class GZipWriterAsyncTests : WriterTests ) ) using ( - var writer = WriterFactory.OpenAsync( + var writer = WriterFactory.OpenAsyncArchive( new AsyncOnlyStream(stream), ArchiveType.GZip, CompressionType.GZip @@ -64,7 +64,7 @@ public class GZipWriterAsyncTests : WriterTests Assert.Throws(() => { using Stream stream = File.OpenWrite(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); - using var writer = WriterFactory.Open( + using var writer = WriterFactory.OpenWriter( new AsyncOnlyStream(stream), ArchiveType.GZip, CompressionType.BZip2 diff --git a/tests/SharpCompress.Test/GZip/GZipWriterTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterTests.cs index f4ca13d3..c8d9deef 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterTests.cs @@ -21,7 +21,7 @@ public class GZipWriterTests : WriterTests FileAccess.Write ) ) - using (var writer = WriterFactory.Open(stream, ArchiveType.GZip, CompressionType.GZip)) + using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.GZip, CompressionType.GZip)) { writer.Write("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); } @@ -56,7 +56,7 @@ public class GZipWriterTests : WriterTests Assert.Throws(() => { using Stream stream = File.OpenWrite(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); - using var writer = WriterFactory.Open(stream, ArchiveType.GZip, CompressionType.BZip2); + using var writer = WriterFactory.OpenWriter(stream, ArchiveType.GZip, CompressionType.BZip2); }); [Fact] diff --git a/tests/SharpCompress.Test/ProgressReportTests.cs b/tests/SharpCompress.Test/ProgressReportTests.cs index f2c7121e..a9308197 100644 --- a/tests/SharpCompress.Test/ProgressReportTests.cs +++ b/tests/SharpCompress.Test/ProgressReportTests.cs @@ -110,7 +110,7 @@ public class ProgressReportTests : TestBase archiveStream.Position = 0; var readerOptions = new ReaderOptions { Progress = progress }; - using (var reader = ReaderFactory.Open(archiveStream, readerOptions)) + using (var reader = ReaderFactory.OpenReader(archiveStream, readerOptions)) { while (reader.MoveToNextEntry()) { @@ -148,7 +148,7 @@ public class ProgressReportTests : TestBase // Now open as archive and extract entry with progress as parameter archiveStream.Position = 0; - using var archive = ZipArchive.Open(archiveStream); + using var archive = ZipArchive.OpenArchive(archiveStream); foreach (var entry in archive.Entries) { if (!entry.IsDirectory) @@ -184,7 +184,7 @@ public class ProgressReportTests : TestBase // Now open as archive and extract entry async with progress as parameter archiveStream.Position = 0; - using var archive = ZipArchive.Open(archiveStream); + using var archive = ZipArchive.OpenArchive(archiveStream); foreach (var entry in archive.Entries) { if (!entry.IsDirectory) @@ -237,7 +237,7 @@ public class ProgressReportTests : TestBase var readerOptions = new ReaderOptions(); Assert.Null(readerOptions.Progress); - using (var reader = ReaderFactory.Open(archiveStream, readerOptions)) + using (var reader = ReaderFactory.OpenReader(archiveStream, readerOptions)) { while (reader.MoveToNextEntry()) { @@ -267,7 +267,7 @@ public class ProgressReportTests : TestBase // Open archive and extract without progress archiveStream.Position = 0; - using var archive = ZipArchive.Open(archiveStream); + using var archive = ZipArchive.OpenArchive(archiveStream); foreach (var entry in archive.Entries) { if (!entry.IsDirectory) @@ -326,7 +326,7 @@ public class ProgressReportTests : TestBase archiveStream.Position = 0; var readerOptions = new ReaderOptions { Progress = progress }; - using (var reader = ReaderFactory.Open(archiveStream, readerOptions)) + using (var reader = ReaderFactory.OpenReader(archiveStream, readerOptions)) { while (reader.MoveToNextEntry()) { @@ -367,7 +367,7 @@ public class ProgressReportTests : TestBase // Now open as archive and extract entry with progress as parameter archiveStream.Position = 0; - using var archive = SharpCompress.Archives.Tar.TarArchive.Open(archiveStream); + using var archive = SharpCompress.Archives.Tar.TarArchive.OpenArchive(archiveStream); foreach (var entry in archive.Entries) { if (!entry.IsDirectory) @@ -406,7 +406,7 @@ public class ProgressReportTests : TestBase // Now open as archive and extract entry async with progress as parameter archiveStream.Position = 0; - using var archive = SharpCompress.Archives.Tar.TarArchive.Open(archiveStream); + using var archive = SharpCompress.Archives.Tar.TarArchive.OpenArchive(archiveStream); foreach (var entry in archive.Entries) { if (!entry.IsDirectory) @@ -447,7 +447,7 @@ public class ProgressReportTests : TestBase archiveStream.Position = 0; var readerOptions = new ReaderOptions { Progress = progress }; - using (var reader = ReaderFactory.Open(archiveStream, readerOptions)) + using (var reader = ReaderFactory.OpenReader(archiveStream, readerOptions)) { while (reader.MoveToNextEntry()) { @@ -496,7 +496,7 @@ public class ProgressReportTests : TestBase // Now open as archive and extract entries with progress as parameter archiveStream.Position = 0; - using var archive = ZipArchive.Open(archiveStream); + using var archive = ZipArchive.OpenArchive(archiveStream); foreach (var entry in archive.Entries) { if (!entry.IsDirectory) @@ -541,7 +541,7 @@ public class ProgressReportTests : TestBase var readerOptions = new ReaderOptions { Progress = progress }; await using ( - var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(archiveStream), readerOptions) + var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(archiveStream), readerOptions) ) { while (await reader.MoveToNextEntryAsync()) diff --git a/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs index acfe4c27..c7cb76c4 100644 --- a/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs @@ -68,7 +68,7 @@ public class RarArchiveAsyncTests : ArchiveTests { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testArchive))) using ( - var archive = RarArchive.Open( + var archive = RarArchive.OpenArchive( stream, new ReaderOptions { Password = password, LeaveStreamOpen = true } ) @@ -98,7 +98,7 @@ public class RarArchiveAsyncTests : ArchiveTests protected async Task ArchiveFileReadPasswordAsync(string archiveName, string password) { using ( - var archive = RarArchive.Open( + var archive = RarArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, archiveName), new ReaderOptions { Password = password, LeaveStreamOpen = true } ) @@ -137,7 +137,7 @@ public class RarArchiveAsyncTests : ArchiveTests private async ValueTask DoRar_test_invalid_exttime_ArchiveStreamReadAsync(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { await entry.WriteToDirectoryAsync( @@ -151,7 +151,7 @@ public class RarArchiveAsyncTests : ArchiveTests public async ValueTask Rar_Jpg_ArchiveStreamRead_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg")); - using (var archive = RarArchive.Open(stream, new ReaderOptions { LookForHeader = true })) + using (var archive = RarArchive.OpenArchive(stream, new ReaderOptions { LookForHeader = true })) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -176,7 +176,7 @@ public class RarArchiveAsyncTests : ArchiveTests { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename))) { - using var archive = RarArchive.Open(stream); + using var archive = RarArchive.OpenArchive(stream); Assert.False(archive.IsSolid); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -196,7 +196,7 @@ public class RarArchiveAsyncTests : ArchiveTests private async ValueTask DoRar_IsSolidEntryStreamCheckAsync(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var archive = RarArchive.Open(stream); + using var archive = RarArchive.OpenArchive(stream); Assert.True(archive.IsSolid); IArchiveEntry[] entries = archive.Entries.Where(a => !a.IsDirectory).ToArray(); Assert.NotInRange(entries.Length, 0, 1); @@ -264,7 +264,7 @@ public class RarArchiveAsyncTests : ArchiveTests private async ValueTask DoRar_Multi_ArchiveStreamReadAsync(string[] archives, bool isSolid) { - using var archive = RarArchive.Open( + using var archive = RarArchive.OpenArchive( archives.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)).Select(File.OpenRead) ); Assert.Equal(archive.IsSolid, isSolid); @@ -316,7 +316,7 @@ public class RarArchiveAsyncTests : ArchiveTests private Task DoRar_ArchiveFileRead_HasDirectoriesAsync(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var archive = RarArchive.Open(stream); + using var archive = RarArchive.OpenArchive(stream); Assert.False(archive.IsSolid); Assert.Contains(true, archive.Entries.Select(entry => entry.IsDirectory)); return Task.CompletedTask; @@ -326,7 +326,7 @@ public class RarArchiveAsyncTests : ArchiveTests public async ValueTask Rar_Jpg_ArchiveFileRead_Async() { using ( - var archive = RarArchive.Open( + var archive = RarArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"), new ReaderOptions { LookForHeader = true } ) @@ -386,7 +386,7 @@ public class RarArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar15.rar"); - using var archive = RarArchive.Open(testArchive); + using var archive = RarArchive.OpenArchive(testArchive); Assert.Equal(1, archive.MinVersion); Assert.Equal(1, archive.MaxVersion); } @@ -396,7 +396,7 @@ public class RarArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar2.rar"); - using var archive = RarArchive.Open(testArchive); + using var archive = RarArchive.OpenArchive(testArchive); Assert.Equal(2, archive.MinVersion); Assert.Equal(2, archive.MaxVersion); } @@ -406,7 +406,7 @@ public class RarArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar4.multi.part01.rar"); - using var archive = RarArchive.Open(testArchive); + using var archive = RarArchive.OpenArchive(testArchive); Assert.Equal(3, archive.MinVersion); Assert.Equal(4, archive.MaxVersion); } @@ -416,7 +416,7 @@ public class RarArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar5.solid.rar"); - using var archive = RarArchive.Open(testArchive); + using var archive = RarArchive.OpenArchive(testArchive); Assert.Equal(5, archive.MinVersion); Assert.Equal(6, archive.MaxVersion); } @@ -573,7 +573,7 @@ public class RarArchiveAsyncTests : ArchiveTests private void DoRar_IsFirstVolume_True(string firstFilename) { - using var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, firstFilename)); + using var archive = RarArchive.OpenArchive(Path.Combine(TEST_ARCHIVES_PATH, firstFilename)); Assert.True(archive.IsMultipartVolume()); Assert.True(archive.IsFirstVolume()); } @@ -588,7 +588,7 @@ public class RarArchiveAsyncTests : ArchiveTests private void DoRar_IsFirstVolume_False(string notFirstFilename) { - using var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, notFirstFilename)); + using var archive = RarArchive.OpenArchive(Path.Combine(TEST_ARCHIVES_PATH, notFirstFilename)); Assert.True(archive.IsMultipartVolume()); Assert.False(archive.IsFirstVolume()); } @@ -631,7 +631,7 @@ public class RarArchiveAsyncTests : ArchiveTests { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); using var stream = File.OpenRead(testArchive); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { await entry.WriteToDirectoryAsync( @@ -649,7 +649,7 @@ public class RarArchiveAsyncTests : ArchiveTests { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); using var stream = File.OpenRead(testArchive); - await using var archive = await ArchiveFactory.OpenAsync(stream); + await using var archive = await ArchiveFactory.OpenAsyncArchive(stream); Assert.True(await archive.IsSolidAsync()); await using (var reader = await archive.ExtractAllEntriesAsync()) { @@ -680,7 +680,7 @@ public class RarArchiveAsyncTests : ArchiveTests private async ValueTask ArchiveFileReadAsync(string testArchive) { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); - using var archive = ArchiveFactory.Open(testArchive); + using var archive = ArchiveFactory.OpenArchive(testArchive); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { await entry.WriteToDirectoryAsync( @@ -697,7 +697,7 @@ public class RarArchiveAsyncTests : ArchiveTests ) { var paths = testArchives.Select(x => Path.Combine(TEST_ARCHIVES_PATH, x)); - using var archive = ArchiveFactory.Open(paths.Select(a => new FileInfo(a)), readerOptions); + using var archive = ArchiveFactory.OpenArchive(paths.Select(a => new FileInfo(a)), readerOptions); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { await entry.WriteToDirectoryAsync( @@ -714,7 +714,7 @@ public class RarArchiveAsyncTests : ArchiveTests ) { var paths = testArchives.Select(x => Path.Combine(TEST_ARCHIVES_PATH, x)); - using var archive = ArchiveFactory.Open(paths.Select(f => new FileInfo(f)), readerOptions); + using var archive = ArchiveFactory.OpenArchive(paths.Select(f => new FileInfo(f)), readerOptions); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { await entry.WriteToDirectoryAsync( diff --git a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs index ba42f649..2117153d 100644 --- a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs +++ b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs @@ -68,7 +68,7 @@ public class RarArchiveTests : ArchiveTests { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testArchive))) using ( - var archive = RarArchive.Open( + var archive = RarArchive.OpenArchive( stream, new ReaderOptions { Password = password, LeaveStreamOpen = true } ) @@ -98,7 +98,7 @@ public class RarArchiveTests : ArchiveTests protected void ArchiveFileReadPassword(string archiveName, string password) { using ( - var archive = RarArchive.Open( + var archive = RarArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, archiveName), new ReaderOptions { Password = password, LeaveStreamOpen = true } ) @@ -134,7 +134,7 @@ public class RarArchiveTests : ArchiveTests private void DoRar_test_invalid_exttime_ArchiveStreamRead(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory( @@ -148,7 +148,7 @@ public class RarArchiveTests : ArchiveTests public void Rar_Jpg_ArchiveStreamRead() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg")); - using (var archive = RarArchive.Open(stream, new ReaderOptions { LookForHeader = true })) + using (var archive = RarArchive.OpenArchive(stream, new ReaderOptions { LookForHeader = true })) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -171,7 +171,7 @@ public class RarArchiveTests : ArchiveTests { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename))) { - using var archive = RarArchive.Open(stream); + using var archive = RarArchive.OpenArchive(stream); Assert.False(archive.IsSolid); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -191,7 +191,7 @@ public class RarArchiveTests : ArchiveTests private void DoRar_IsSolidEntryStreamCheck(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var archive = RarArchive.Open(stream); + using var archive = RarArchive.OpenArchive(stream); Assert.True(archive.IsSolid); IArchiveEntry[] entries = archive.Entries.Where(a => !a.IsDirectory).ToArray(); Assert.NotInRange(entries.Length, 0, 1); @@ -258,7 +258,7 @@ public class RarArchiveTests : ArchiveTests private void DoRar_Multi_ArchiveStreamRead(string[] archives, bool isSolid) { - using var archive = RarArchive.Open( + using var archive = RarArchive.OpenArchive( archives.Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)).Select(File.OpenRead) ); Assert.Equal(archive.IsSolid, isSolid); @@ -308,7 +308,7 @@ public class RarArchiveTests : ArchiveTests private void DoRar_ArchiveFileRead_HasDirectories(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var archive = RarArchive.Open(stream); + using var archive = RarArchive.OpenArchive(stream); Assert.False(archive.IsSolid); Assert.Contains(true, archive.Entries.Select(entry => entry.IsDirectory)); } @@ -317,7 +317,7 @@ public class RarArchiveTests : ArchiveTests public void Rar_Jpg_ArchiveFileRead() { using ( - var archive = RarArchive.Open( + var archive = RarArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"), new ReaderOptions { LookForHeader = true } ) @@ -374,7 +374,7 @@ public class RarArchiveTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar15.rar"); - using var archive = RarArchive.Open(testArchive); + using var archive = RarArchive.OpenArchive(testArchive); Assert.Equal(1, archive.MinVersion); Assert.Equal(1, archive.MaxVersion); } @@ -384,7 +384,7 @@ public class RarArchiveTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar2.rar"); - using var archive = RarArchive.Open(testArchive); + using var archive = RarArchive.OpenArchive(testArchive); Assert.Equal(2, archive.MinVersion); Assert.Equal(2, archive.MaxVersion); } @@ -394,7 +394,7 @@ public class RarArchiveTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar4.multi.part01.rar"); - using var archive = RarArchive.Open(testArchive); + using var archive = RarArchive.OpenArchive(testArchive); Assert.Equal(3, archive.MinVersion); Assert.Equal(4, archive.MaxVersion); } @@ -404,7 +404,7 @@ public class RarArchiveTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "Rar5.solid.rar"); - using var archive = RarArchive.Open(testArchive); + using var archive = RarArchive.OpenArchive(testArchive); Assert.Equal(5, archive.MinVersion); Assert.Equal(6, archive.MaxVersion); } @@ -585,7 +585,7 @@ public class RarArchiveTests : ArchiveTests private void DoRar_IsFirstVolume_True(string firstFilename) { - using var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, firstFilename)); + using var archive = RarArchive.OpenArchive(Path.Combine(TEST_ARCHIVES_PATH, firstFilename)); Assert.True(archive.IsMultipartVolume()); Assert.True(archive.IsFirstVolume()); } @@ -598,7 +598,7 @@ public class RarArchiveTests : ArchiveTests private void DoRar_IsFirstVolume_False(string notFirstFilename) { - using var archive = RarArchive.Open(Path.Combine(TEST_ARCHIVES_PATH, notFirstFilename)); + using var archive = RarArchive.OpenArchive(Path.Combine(TEST_ARCHIVES_PATH, notFirstFilename)); Assert.True(archive.IsMultipartVolume()); Assert.False(archive.IsFirstVolume()); } @@ -639,7 +639,7 @@ public class RarArchiveTests : ArchiveTests [Fact] public void Rar_TestEncryptedDetection() { - using var passwordProtectedFilesArchive = RarArchive.Open( + using var passwordProtectedFilesArchive = RarArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Rar.encrypted_filesOnly.rar") ); Assert.True(passwordProtectedFilesArchive.IsEncrypted); @@ -662,7 +662,7 @@ public class RarArchiveTests : ArchiveTests foreach (var testFile in testFiles) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, testFile)); - using var archive = RarArchive.Open(stream); + using var archive = RarArchive.OpenArchive(stream); // Extract all entries and read them completely foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) @@ -704,7 +704,7 @@ public class RarArchiveTests : ArchiveTests // when we try to read beyond the truncated data var exception = Assert.Throws(() => { - using var archive = RarArchive.Open(truncatedStream); + using var archive = RarArchive.OpenArchive(truncatedStream); foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) { using var entryStream = entry.OpenEntryStream(); diff --git a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs index d58c2a93..f32e0886 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs @@ -39,7 +39,7 @@ public class RarReaderAsyncTests : ReaderTests private async ValueTask DoRar_Multi_Reader_Async(string[] archives) { using ( - IReader baseReader = RarReader.Open( + IReader baseReader = RarReader.OpenReader( archives .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) .Select(p => File.OpenRead(p)) @@ -72,7 +72,7 @@ public class RarReaderAsyncTests : ReaderTests "Rar.EncryptedParts.part06.rar", ]; using ( - IReader baseReader = RarReader.Open( + IReader baseReader = RarReader.OpenReader( archives .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) .Select(p => File.OpenRead(p)), @@ -127,7 +127,7 @@ public class RarReaderAsyncTests : ReaderTests .Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)) .Select(File.OpenRead) .ToList(); - using (IReader baseReader = RarReader.Open(streams)) + using (IReader baseReader = RarReader.OpenReader(streams)) { IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) @@ -208,7 +208,7 @@ public class RarReaderAsyncTests : ReaderTests private async ValueTask DoRar_Entry_Stream_Async(string filename) { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename))) - await using (var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream))) + await using (var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream))) { while (await reader.MoveToNextEntryAsync()) { @@ -253,7 +253,7 @@ public class RarReaderAsyncTests : ReaderTests var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.Audio_program.rar")) ) await using ( - var reader = ReaderFactory.OpenAsync( + var reader = ReaderFactory.OpenAsyncReader( new AsyncOnlyStream(stream), new ReaderOptions { LookForHeader = true } ) @@ -279,7 +279,7 @@ public class RarReaderAsyncTests : ReaderTests { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) using ( - IReader baseReader = RarReader.Open(stream, new ReaderOptions { LookForHeader = true }) + IReader baseReader = RarReader.OpenReader(stream, new ReaderOptions { LookForHeader = true }) ) { IAsyncReader reader = (IAsyncReader)baseReader; @@ -322,7 +322,7 @@ public class RarReaderAsyncTests : ReaderTests private async ValueTask DoRar_Solid_Skip_Reader_Async(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - await using var reader = ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsyncReader( new AsyncOnlyStream(stream), new ReaderOptions { LookForHeader = true } ); @@ -348,7 +348,7 @@ public class RarReaderAsyncTests : ReaderTests private async ValueTask DoRar_Reader_Skip_Async(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - await using var reader = ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsyncReader( new AsyncOnlyStream(stream), new ReaderOptions { LookForHeader = true } ); @@ -373,7 +373,7 @@ public class RarReaderAsyncTests : ReaderTests { testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); using Stream stream = File.OpenRead(testArchive); - await using var reader = ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsyncReader( new AsyncOnlyStream(stream), readerOptions ?? new ReaderOptions() ); diff --git a/tests/SharpCompress.Test/Rar/RarReaderTests.cs b/tests/SharpCompress.Test/Rar/RarReaderTests.cs index b3c98f2e..866d1d42 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderTests.cs @@ -37,7 +37,7 @@ public class RarReaderTests : ReaderTests private void DoRar_Multi_Reader(string[] archives) { using ( - var reader = RarReader.Open( + var reader = RarReader.OpenReader( archives .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) .Select(p => File.OpenRead(p)) @@ -70,7 +70,7 @@ public class RarReaderTests : ReaderTests Assert.Throws(() => { using ( - var reader = RarReader.Open( + var reader = RarReader.OpenReader( archives .Select(s => Path.Combine(TEST_ARCHIVES_PATH, s)) .Select(p => File.OpenRead(p)), @@ -124,7 +124,7 @@ public class RarReaderTests : ReaderTests .Select(s => Path.Combine(SCRATCH2_FILES_PATH, s)) .Select(File.OpenRead) .ToList(); - using (var reader = RarReader.Open(streams)) + using (var reader = RarReader.OpenReader(streams)) { while (reader.MoveToNextEntry()) { @@ -193,7 +193,7 @@ public class RarReaderTests : ReaderTests private void DoRar_Entry_Stream(string filename) { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename))) - using (var reader = ReaderFactory.Open(stream)) + using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -226,7 +226,7 @@ public class RarReaderTests : ReaderTests using ( var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.Audio_program.rar")) ) - using (var reader = ReaderFactory.Open(stream, new ReaderOptions { LookForHeader = true })) + using (var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true })) { while (reader.MoveToNextEntry()) { @@ -247,7 +247,7 @@ public class RarReaderTests : ReaderTests public void Rar_Jpg_Reader() { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) - using (var reader = RarReader.Open(stream, new ReaderOptions { LookForHeader = true })) + using (var reader = RarReader.OpenReader(stream, new ReaderOptions { LookForHeader = true })) { while (reader.MoveToNextEntry()) { @@ -282,7 +282,7 @@ public class RarReaderTests : ReaderTests private void DoRar_Solid_Skip_Reader(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var reader = ReaderFactory.Open(stream, new ReaderOptions { LookForHeader = true }); + using var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true }); while (reader.MoveToNextEntry()) { if (reader.Entry.Key.NotNull().Contains("jpg")) @@ -305,7 +305,7 @@ public class RarReaderTests : ReaderTests private void DoRar_Reader_Skip(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var reader = ReaderFactory.Open(stream, new ReaderOptions { LookForHeader = true }); + using var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true }); while (reader.MoveToNextEntry()) { if (reader.Entry.Key.NotNull().Contains("jpg")) @@ -325,7 +325,7 @@ public class RarReaderTests : ReaderTests using var stream = File.OpenRead( Path.Combine(TEST_ARCHIVES_PATH, "Rar.encrypted_filesOnly.rar") ); - using var reader = ReaderFactory.Open(stream, new ReaderOptions { LookForHeader = true }); + using var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true }); while (reader.MoveToNextEntry()) { // @@ -397,7 +397,7 @@ public class RarReaderTests : ReaderTests Path.Combine("exe", "test.exe"), } ); - using var reader = RarReader.Open([ + using var reader = RarReader.OpenReader([ Path.Combine(TEST_ARCHIVES_PATH, "Rar.multi.part01.rar"), Path.Combine(TEST_ARCHIVES_PATH, "Rar.multi.part02.rar"), Path.Combine(TEST_ARCHIVES_PATH, "Rar.multi.part03.rar"), diff --git a/tests/SharpCompress.Test/ReaderTests.cs b/tests/SharpCompress.Test/ReaderTests.cs index cb1dac03..039c4e51 100644 --- a/tests/SharpCompress.Test/ReaderTests.cs +++ b/tests/SharpCompress.Test/ReaderTests.cs @@ -70,7 +70,7 @@ public abstract class ReaderTests : TestBase bufferSize: options.BufferSize ); using var testStream = new TestStream(protectedStream); - using (var reader = ReaderFactory.Open(testStream, options)) + using (var reader = ReaderFactory.OpenReader(testStream, options)) { useReader(reader); protectedStream.ThrowOnDispose = false; @@ -146,7 +146,7 @@ public abstract class ReaderTests : TestBase ); using var testStream = new TestStream(protectedStream); await using ( - var reader = ReaderFactory.OpenAsync( + var reader = ReaderFactory.OpenAsyncReader( new AsyncOnlyStream(testStream), options, cancellationToken @@ -190,7 +190,7 @@ public abstract class ReaderTests : TestBase protected void ReadForBufferBoundaryCheck(string fileName, CompressionType compressionType) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, fileName)); - using var reader = ReaderFactory.Open(stream, new ReaderOptions { LookForHeader = true }); + using var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true }); while (reader.MoveToNextEntry()) { @@ -224,7 +224,7 @@ public abstract class ReaderTests : TestBase testArchive = Path.Combine(TEST_ARCHIVES_PATH, testArchive); using var file = File.OpenRead(testArchive); using var forward = new ForwardOnlyStream(file); - using var reader = ReaderFactory.Open(forward, options); + using var reader = ReaderFactory.OpenReader(forward, options); while (reader.MoveToNextEntry()) { Assert.Equal(expectedCompression, reader.Entry.CompressionType); diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveAsyncTests.cs index 7a8718ee..a7920f0b 100644 --- a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveAsyncTests.cs @@ -16,7 +16,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.LZMA.7z"); using var stream = File.OpenRead(testArchive); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -29,7 +29,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.Create(targetPath); + await using var targetStream = File.CreateArchive(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } @@ -41,7 +41,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.LZMA2.7z"); using var stream = File.OpenRead(testArchive); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -54,7 +54,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.Create(targetPath); + await using var targetStream = File.CreateArchive(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } @@ -66,7 +66,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.solid.7z"); using var stream = File.OpenRead(testArchive); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -79,7 +79,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.Create(targetPath); + await using var targetStream = File.CreateArchive(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } @@ -91,7 +91,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.BZip2.7z"); using var stream = File.OpenRead(testArchive); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -104,7 +104,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.Create(targetPath); + await using var targetStream = File.CreateArchive(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } @@ -116,7 +116,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests { var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "7Zip.PPMd.7z"); using var stream = File.OpenRead(testArchive); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -129,7 +129,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.Create(targetPath); + await using var targetStream = File.CreateArchive(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs index af21ff94..6cbdc87e 100644 --- a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveTests.cs @@ -132,7 +132,7 @@ public class SevenZipArchiveTests : ArchiveTests public void SevenZipArchive_Copy_CompressionType() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "7Zip.Copy.7z"))) - using (var archive = SevenZipArchive.Open(stream)) + using (var archive = SevenZipArchive.OpenArchive(stream)) { foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) { @@ -205,7 +205,7 @@ public class SevenZipArchiveTests : ArchiveTests public void SevenZipArchive_Tar_PathRead() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "7Zip.Tar.tar.7z"))) - using (var archive = SevenZipArchive.Open(stream)) + using (var archive = SevenZipArchive.OpenArchive(stream)) { var entry = archive.Entries.First(); entry.WriteToFile(Path.Combine(SCRATCH_FILES_PATH, entry.Key.NotNull())); @@ -227,7 +227,7 @@ public class SevenZipArchiveTests : ArchiveTests [Fact] public void SevenZipArchive_TestEncryptedDetection() { - using var passwordProtectedFilesArchive = SevenZipArchive.Open( + using var passwordProtectedFilesArchive = SevenZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "7Zip.encryptedFiles.7z") ); Assert.True(passwordProtectedFilesArchive.IsEncrypted); @@ -236,17 +236,17 @@ public class SevenZipArchiveTests : ArchiveTests [Fact] public void SevenZipArchive_TestSolidDetection() { - using var oneBlockSolidArchive = SevenZipArchive.Open( + using var oneBlockSolidArchive = SevenZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "7Zip.solid.1block.7z") ); Assert.True(oneBlockSolidArchive.IsSolid); - using var solidArchive = SevenZipArchive.Open( + using var solidArchive = SevenZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "7Zip.solid.7z") ); Assert.True(solidArchive.IsSolid); - using var nonSolidArchive = SevenZipArchive.Open( + using var nonSolidArchive = SevenZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "7Zip.nonsolid.7z") ); Assert.False(nonSolidArchive.IsSolid); diff --git a/tests/SharpCompress.Test/Streams/ZLibBaseStreamAsyncTests.cs b/tests/SharpCompress.Test/Streams/ZLibBaseStreamAsyncTests.cs index a29477da..39c9f92b 100644 --- a/tests/SharpCompress.Test/Streams/ZLibBaseStreamAsyncTests.cs +++ b/tests/SharpCompress.Test/Streams/ZLibBaseStreamAsyncTests.cs @@ -86,7 +86,7 @@ public class ZLibBaseStreamAsyncTests private async ValueTask CompressAsync(Stream input, Stream output, int compressionLevel) { using var zlibStream = new ZlibStream( - SharpCompressStream.Create(output, leaveOpen: true), + SharpCompressStream.CreateArchive(output, leaveOpen: true), CompressionMode.Compress, (CompressionLevel)compressionLevel ); @@ -97,7 +97,7 @@ public class ZLibBaseStreamAsyncTests private async ValueTask DecompressAsync(Stream input, Stream output) { using var zlibStream = new ZlibStream( - SharpCompressStream.Create(input, leaveOpen: true), + SharpCompressStream.CreateArchive(input, leaveOpen: true), CompressionMode.Decompress ); await zlibStream.CopyToAsync(output).ConfigureAwait(false); diff --git a/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs b/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs index 5808078c..7a5d547e 100644 --- a/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs @@ -80,7 +80,7 @@ public class ZLibBaseStreamTests private void Compress(Stream input, Stream output, int compressionLevel) { using var zlibStream = new ZlibStream( - SharpCompressStream.Create(output, leaveOpen: true), + SharpCompressStream.CreateArchive(output, leaveOpen: true), CompressionMode.Compress, (CompressionLevel)compressionLevel ); @@ -91,7 +91,7 @@ public class ZLibBaseStreamTests private void Decompress(Stream input, Stream output) { using var zlibStream = new ZlibStream( - SharpCompressStream.Create(input, leaveOpen: true), + SharpCompressStream.CreateArchive(input, leaveOpen: true), CompressionMode.Decompress ); zlibStream.CopyTo(output); diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index f7758bb3..8de94f65 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -33,7 +33,7 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 1: create a tar file containing a file with the test name using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var writer = WriterFactory.OpenAsync(stream, ArchiveType.Tar, CompressionType.None)) + using (var writer = WriterFactory.OpenAsyncWriter(stream, ArchiveType.Tar, CompressionType.None)) using (Stream inputStream = new MemoryStream()) { var sw = new StreamWriter(inputStream); @@ -47,7 +47,7 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); await using ( - var archive2 = TarArchive.OpenAsync(new AsyncOnlyStream(File.OpenRead(unmodified))) + var archive2 = TarArchive.OpenAsyncArchive(new AsyncOnlyStream(File.OpenRead(unmodified))) ) { Assert.Equal(1, await archive2.EntriesAsync.CountAsync()); @@ -82,7 +82,7 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 1: create a tar file containing a file with a long name using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var writer = WriterFactory.OpenAsync(stream, ArchiveType.Tar, CompressionType.None)) + using (var writer = WriterFactory.OpenAsyncWriter(stream, ArchiveType.Tar, CompressionType.None)) using (Stream inputStream = new MemoryStream()) { var sw = new StreamWriter(inputStream); @@ -96,7 +96,7 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); await using ( - var archive2 = TarArchive.OpenAsync(new AsyncOnlyStream(File.OpenRead(unmodified))) + var archive2 = TarArchive.OpenAsyncArchive(new AsyncOnlyStream(File.OpenRead(unmodified))) ) { Assert.Equal(1, await archive2.EntriesAsync.CountAsync()); @@ -121,7 +121,7 @@ public class TarArchiveAsyncTests : ArchiveTests var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - using (var archive = TarArchive.Create()) + using (var archive = TarArchive.CreateArchive()) { archive.AddAllFromDirectory(ORIGINAL_FILES_PATH); var twopt = new TarWriterOptions(CompressionType.None, true); @@ -139,7 +139,7 @@ public class TarArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.mod.tar"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - await using (var archive = TarArchive.OpenAsync(unmodified)) + await using (var archive = TarArchive.OpenAsyncArchive(unmodified)) { archive.AddEntry("jpg\\test.jpg", jpg); await archive.SaveToAsync(scratchPath, new WriterOptions(CompressionType.None)); @@ -154,7 +154,7 @@ public class TarArchiveAsyncTests : ArchiveTests var modified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.mod.tar"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - await using (var archive = TarArchive.OpenAsync(unmodified)) + await using (var archive = TarArchive.OpenAsyncArchive(unmodified)) { var entry = await archive.EntriesAsync.SingleAsync(x => x.Key.NotNull().EndsWith("jpg", StringComparison.OrdinalIgnoreCase) @@ -183,7 +183,7 @@ public class TarArchiveAsyncTests : ArchiveTests using (var inputMemory = new MemoryStream(mstm.ToArray())) { var tropt = new ReaderOptions { ArchiveEncoding = enc }; - await using (var tr = ReaderFactory.OpenAsync(inputMemory, tropt)) + await using (var tr = ReaderFactory.OpenAsyncReader(inputMemory, tropt)) { while (await tr.MoveToNextEntryAsync()) { @@ -216,7 +216,7 @@ public class TarArchiveAsyncTests : ArchiveTests var numberOfEntries = 0; - await using (var archiveFactory = TarArchive.OpenAsync(new AsyncOnlyStream(memoryStream))) + await using (var archiveFactory = TarArchive.OpenAsyncArchive(new AsyncOnlyStream(memoryStream))) { await foreach (var entry in archiveFactory.EntriesAsync) { diff --git a/tests/SharpCompress.Test/Tar/TarArchiveDirectoryTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveDirectoryTests.cs index 3046246d..f5d3ad73 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveDirectoryTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveDirectoryTests.cs @@ -12,7 +12,7 @@ public class TarArchiveDirectoryTests : TestBase [Fact] public void TarArchive_AddDirectoryEntry_CreatesDirectoryEntry() { - using var archive = TarArchive.Create(); + using var archive = TarArchive.CreateArchive(); archive.AddDirectoryEntry("test-dir", DateTime.Now); @@ -25,7 +25,7 @@ public class TarArchiveDirectoryTests : TestBase [Fact] public void TarArchive_AddDirectoryEntry_MultipleDirectories() { - using var archive = TarArchive.Create(); + using var archive = TarArchive.CreateArchive(); archive.AddDirectoryEntry("dir1", DateTime.Now); archive.AddDirectoryEntry("dir2", DateTime.Now); @@ -39,7 +39,7 @@ public class TarArchiveDirectoryTests : TestBase [Fact] public void TarArchive_AddDirectoryEntry_MixedWithFiles() { - using var archive = TarArchive.Create(); + using var archive = TarArchive.CreateArchive(); archive.AddDirectoryEntry("dir1", DateTime.Now); @@ -62,7 +62,7 @@ public class TarArchiveDirectoryTests : TestBase { var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "tar-directory-test.tar"); - using (var archive = TarArchive.Create()) + using (var archive = TarArchive.CreateArchive()) { archive.AddDirectoryEntry("dir1", DateTime.Now); archive.AddDirectoryEntry("dir2", DateTime.Now); @@ -84,7 +84,7 @@ public class TarArchiveDirectoryTests : TestBase } } - using (var archive = TarArchive.Open(scratchPath)) + using (var archive = TarArchive.OpenArchive(scratchPath)) { var entries = archive.Entries.OrderBy(e => e.Key).ToList(); Assert.Equal(3, entries.Count); @@ -103,7 +103,7 @@ public class TarArchiveDirectoryTests : TestBase [Fact] public void TarArchive_AddDirectoryEntry_DuplicateKey_ThrowsException() { - using var archive = TarArchive.Create(); + using var archive = TarArchive.CreateArchive(); archive.AddDirectoryEntry("test-dir", DateTime.Now); diff --git a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs index 0f92681c..af33dff7 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveTests.cs @@ -34,7 +34,7 @@ public class TarArchiveTests : ArchiveTests // Step 1: create a tar file containing a file with the test name using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.None)) + using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Tar, CompressionType.None)) using (Stream inputStream = new MemoryStream()) { var sw = new StreamWriter(inputStream); @@ -47,7 +47,7 @@ public class TarArchiveTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); - using (var archive2 = TarArchive.Open(unmodified)) + using (var archive2 = TarArchive.OpenArchive(unmodified)) { Assert.Equal(1, archive2.Entries.Count()); Assert.Contains(filename, archive2.Entries.Select(entry => entry.Key)); @@ -66,7 +66,7 @@ public class TarArchiveTests : ArchiveTests public void Tar_NonUstarArchiveWithLongNameDoesNotSkipEntriesAfterTheLongOne() { var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "very long filename.tar"); - using var archive = TarArchive.Open(unmodified); + using var archive = TarArchive.OpenArchive(unmodified); Assert.Equal(5, archive.Entries.Count()); Assert.Contains("very long filename/", archive.Entries.Select(entry => entry.Key)); Assert.Contains( @@ -94,7 +94,7 @@ public class TarArchiveTests : ArchiveTests // Step 1: create a tar file containing a file with a long name using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.None)) + using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Tar, CompressionType.None)) using (Stream inputStream = new MemoryStream()) { var sw = new StreamWriter(inputStream); @@ -107,7 +107,7 @@ public class TarArchiveTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); - using (var archive2 = TarArchive.Open(unmodified)) + using (var archive2 = TarArchive.OpenArchive(unmodified)) { Assert.Equal(1, archive2.Entries.Count()); Assert.Contains(longFilename, archive2.Entries.Select(entry => entry.Key)); @@ -126,7 +126,7 @@ public class TarArchiveTests : ArchiveTests public void Tar_UstarArchivePathReadLongName() { var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "ustar with long names.tar"); - using var archive = TarArchive.Open(unmodified); + using var archive = TarArchive.OpenArchive(unmodified); Assert.Equal(6, archive.Entries.Count()); Assert.Contains("Directory/", archive.Entries.Select(entry => entry.Key)); Assert.Contains( @@ -159,7 +159,7 @@ public class TarArchiveTests : ArchiveTests // var aropt = new Ar - using (var archive = TarArchive.Create()) + using (var archive = TarArchive.CreateArchive()) { archive.AddAllFromDirectory(ORIGINAL_FILES_PATH); var twopt = new TarWriterOptions(CompressionType.None, true); @@ -177,7 +177,7 @@ public class TarArchiveTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.mod.tar"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - using (var archive = TarArchive.Open(unmodified)) + using (var archive = TarArchive.OpenArchive(unmodified)) { archive.AddEntry("jpg\\test.jpg", jpg); archive.SaveTo(scratchPath, CompressionType.None); @@ -192,7 +192,7 @@ public class TarArchiveTests : ArchiveTests var modified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.mod.tar"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - using (var archive = TarArchive.Open(unmodified)) + using (var archive = TarArchive.OpenArchive(unmodified)) { var entry = archive.Entries.Single(x => x.Key.NotNull().EndsWith("jpg", StringComparison.OrdinalIgnoreCase) @@ -208,7 +208,7 @@ public class TarArchiveTests : ArchiveTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.ContainsRar.tar"); using Stream stream = File.OpenRead(archiveFullPath); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); Assert.True(archive.Type == ArchiveType.Tar); } @@ -217,7 +217,7 @@ public class TarArchiveTests : ArchiveTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.Empty.tar"); using Stream stream = File.OpenRead(archiveFullPath); - using var archive = ArchiveFactory.Open(stream); + using var archive = ArchiveFactory.OpenArchive(stream); Assert.True(archive.Type == ArchiveType.Tar); } @@ -239,7 +239,7 @@ public class TarArchiveTests : ArchiveTests using (var inputMemory = new MemoryStream(mstm.ToArray())) { var tropt = new ReaderOptions { ArchiveEncoding = enc }; - using (var tr = TarReader.Open(inputMemory, tropt)) + using (var tr = TarReader.OpenReader(inputMemory, tropt)) { while (tr.MoveToNextEntry()) { @@ -272,7 +272,7 @@ public class TarArchiveTests : ArchiveTests var numberOfEntries = 0; - using (var archiveFactory = TarArchive.Open(memoryStream)) + using (var archiveFactory = TarArchive.OpenArchive(memoryStream)) { foreach (var entry in archiveFactory.Entries) { diff --git a/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs index 1a18f3ce..7ce0d9b2 100644 --- a/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarReaderAsyncTests.cs @@ -23,7 +23,7 @@ public class TarReaderAsyncTests : ReaderTests using Stream stream = new ForwardOnlyStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")) ); - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); var x = 0; while (await reader.MoveToNextEntryAsync()) { @@ -73,7 +73,7 @@ public class TarReaderAsyncTests : ReaderTests public async ValueTask Tar_BZip2_Entry_Stream_Async() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.bz2"))) - await using (var reader = ReaderFactory.OpenAsync(stream)) + await using (var reader = ReaderFactory.OpenAsyncReader(stream)) { while (await reader.MoveToNextEntryAsync()) { @@ -110,7 +110,7 @@ public class TarReaderAsyncTests : ReaderTests Path.Combine(TEST_ARCHIVES_PATH, "Tar.LongPathsWithLongNameExtension.tar") ) ) - using (var reader = TarReader.Open(stream)) + using (var reader = TarReader.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -137,7 +137,7 @@ public class TarReaderAsyncTests : ReaderTests public void Tar_BZip2_Skip_Entry_Stream_Async() { using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.bz2")); - using var reader = TarReader.Open(stream); + using var reader = TarReader.OpenReader(stream); var names = new List(); while (reader.MoveToNextEntry()) { @@ -157,7 +157,7 @@ public class TarReaderAsyncTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.ContainsRar.tar"); using Stream stream = File.OpenRead(archiveFullPath); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); Assert.True(reader.ArchiveType == ArchiveType.Tar); } @@ -166,7 +166,7 @@ public class TarReaderAsyncTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.ContainsTarGz.tar"); using Stream stream = File.OpenRead(archiveFullPath); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); Assert.True(reader.MoveToNextEntry()); Assert.Equal("inner.tar.gz", reader.Entry.Key); @@ -174,7 +174,7 @@ public class TarReaderAsyncTests : ReaderTests using var flushingStream = new FlushOnDisposeStream(entryStream); // Extract inner.tar.gz - using var innerReader = ReaderFactory.Open(flushingStream); + using var innerReader = ReaderFactory.OpenReader(flushingStream); Assert.True(innerReader.MoveToNextEntry()); Assert.Equal("test", innerReader.Entry.Key); } @@ -184,7 +184,7 @@ public class TarReaderAsyncTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"); using Stream stream = File.OpenRead(archiveFullPath); - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); var memoryStream = new MemoryStream(); Assert.True(await reader.MoveToNextEntryAsync()); @@ -201,7 +201,7 @@ public class TarReaderAsyncTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "TarCorrupted.tar"); using Stream stream = File.OpenRead(archiveFullPath); - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); var memoryStream = new MemoryStream(); Assert.True(await reader.MoveToNextEntryAsync()); @@ -220,7 +220,7 @@ public class TarReaderAsyncTests : ReaderTests using Stream stream = File.OpenRead( Path.Combine(TEST_ARCHIVES_PATH, "TarWithSymlink.tar.gz") ); - await using var reader = ReaderFactory.OpenAsync( + await using var reader = ReaderFactory.OpenAsyncReader( new AsyncOnlyStream(stream), new ReaderOptions { LookForHeader = true } ); diff --git a/tests/SharpCompress.Test/Tar/TarReaderTests.cs b/tests/SharpCompress.Test/Tar/TarReaderTests.cs index c4763b3c..2f0b9248 100644 --- a/tests/SharpCompress.Test/Tar/TarReaderTests.cs +++ b/tests/SharpCompress.Test/Tar/TarReaderTests.cs @@ -22,7 +22,7 @@ public class TarReaderTests : ReaderTests using Stream stream = new ForwardOnlyStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")) ); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); var x = 0; while (reader.MoveToNextEntry()) { @@ -65,7 +65,7 @@ public class TarReaderTests : ReaderTests public void Tar_BZip2_Entry_Stream() { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.bz2"))) - using (var reader = TarReader.Open(stream)) + using (var reader = TarReader.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -102,7 +102,7 @@ public class TarReaderTests : ReaderTests Path.Combine(TEST_ARCHIVES_PATH, "Tar.LongPathsWithLongNameExtension.tar") ) ) - using (var reader = TarReader.Open(stream)) + using (var reader = TarReader.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -129,7 +129,7 @@ public class TarReaderTests : ReaderTests public void Tar_BZip2_Skip_Entry_Stream() { using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.bz2")); - using var reader = TarReader.Open(stream); + using var reader = TarReader.OpenReader(stream); var names = new List(); while (reader.MoveToNextEntry()) { @@ -149,7 +149,7 @@ public class TarReaderTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.ContainsRar.tar"); using Stream stream = File.OpenRead(archiveFullPath); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); Assert.True(reader.ArchiveType == ArchiveType.Tar); } @@ -158,7 +158,7 @@ public class TarReaderTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.ContainsTarGz.tar"); using Stream stream = File.OpenRead(archiveFullPath); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); Assert.True(reader.MoveToNextEntry()); Assert.Equal("inner.tar.gz", reader.Entry.Key); @@ -166,7 +166,7 @@ public class TarReaderTests : ReaderTests using var flushingStream = new FlushOnDisposeStream(entryStream); // Extract inner.tar.gz - using var innerReader = ReaderFactory.Open(flushingStream); + using var innerReader = ReaderFactory.OpenReader(flushingStream); Assert.True(innerReader.MoveToNextEntry()); Assert.Equal("test", innerReader.Entry.Key); } @@ -176,7 +176,7 @@ public class TarReaderTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"); using Stream stream = File.OpenRead(archiveFullPath); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); var memoryStream = new MemoryStream(); Assert.True(reader.MoveToNextEntry()); @@ -191,7 +191,7 @@ public class TarReaderTests : ReaderTests { var archiveFullPath = Path.Combine(TEST_ARCHIVES_PATH, "TarCorrupted.tar"); using Stream stream = File.OpenRead(archiveFullPath); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); var memoryStream = new MemoryStream(); Assert.True(reader.MoveToNextEntry()); @@ -208,7 +208,7 @@ public class TarReaderTests : ReaderTests using Stream stream = File.OpenRead( Path.Combine(TEST_ARCHIVES_PATH, "TarWithSymlink.tar.gz") ); - using var reader = TarReader.Open(stream); + using var reader = TarReader.OpenReader(stream); while (reader.MoveToNextEntry()) { if (reader.Entry.IsDirectory) @@ -304,7 +304,7 @@ public class TarReaderTests : ReaderTests // The important thing is it doesn't cause OutOfMemoryException Assert.Throws(() => { - using var reader = TarReader.Open(stream); + using var reader = TarReader.OpenReader(stream); reader.MoveToNextEntry(); }); } diff --git a/tests/SharpCompress.Test/Tar/TarWriterDirectoryTests.cs b/tests/SharpCompress.Test/Tar/TarWriterDirectoryTests.cs index e0b5d821..b900f9b5 100644 --- a/tests/SharpCompress.Test/Tar/TarWriterDirectoryTests.cs +++ b/tests/SharpCompress.Test/Tar/TarWriterDirectoryTests.cs @@ -25,7 +25,7 @@ public class TarWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = TarArchive.Open(memoryStream); + using var archive = TarArchive.OpenArchive(memoryStream); var entries = archive.Entries.ToList(); Assert.Single(entries); @@ -48,7 +48,7 @@ public class TarWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = TarArchive.Open(memoryStream); + using var archive = TarArchive.OpenArchive(memoryStream); var entries = archive.Entries.ToList(); Assert.Single(entries); @@ -71,7 +71,7 @@ public class TarWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = TarArchive.Open(memoryStream); + using var archive = TarArchive.OpenArchive(memoryStream); var entries = archive.Entries.ToList(); Assert.Single(entries); @@ -94,7 +94,7 @@ public class TarWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = TarArchive.Open(memoryStream); + using var archive = TarArchive.OpenArchive(memoryStream); Assert.Empty(archive.Entries); } @@ -116,7 +116,7 @@ public class TarWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = TarArchive.Open(memoryStream); + using var archive = TarArchive.OpenArchive(memoryStream); var entries = archive.Entries.OrderBy(e => e.Key).ToList(); Assert.Equal(3, entries.Count); @@ -150,7 +150,7 @@ public class TarWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = TarArchive.Open(memoryStream); + using var archive = TarArchive.OpenArchive(memoryStream); var entries = archive.Entries.OrderBy(e => e.Key).ToList(); Assert.Equal(3, entries.Count); diff --git a/tests/SharpCompress.Test/TestBase.cs b/tests/SharpCompress.Test/TestBase.cs index 702d6bca..5a5ea765 100644 --- a/tests/SharpCompress.Test/TestBase.cs +++ b/tests/SharpCompress.Test/TestBase.cs @@ -228,8 +228,8 @@ public class TestBase : IAsyncDisposable //don't compare the order. OS X reads files from the file system in a different order therefore makes the archive ordering different var archive1Entries = new List(); var archive2Entries = new List(); - using (var archive1 = ReaderFactory.Open(File.OpenRead(file1), readerOptions)) - using (var archive2 = ReaderFactory.Open(File.OpenRead(file2), readerOptions)) + using (var archive1 = ReaderFactory.OpenArchive(File.OpenRead(file1), readerOptions)) + using (var archive2 = ReaderFactory.OpenArchive(File.OpenRead(file2), readerOptions)) { while (archive1.MoveToNextEntry()) { diff --git a/tests/SharpCompress.Test/WriterTests.cs b/tests/SharpCompress.Test/WriterTests.cs index 95d82da3..bb16ac03 100644 --- a/tests/SharpCompress.Test/WriterTests.cs +++ b/tests/SharpCompress.Test/WriterTests.cs @@ -29,7 +29,7 @@ public class WriterTests : TestBase writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using var writer = WriterFactory.Open(stream, _type, writerOptions); + using var writer = WriterFactory.OpenArchive(stream, _type, writerOptions); writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories); } CompareArchivesByPath( @@ -43,8 +43,8 @@ public class WriterTests : TestBase readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using var reader = ReaderFactory.Open( - SharpCompressStream.Create(stream, leaveOpen: true), + using var reader = ReaderFactory.OpenArchive( + SharpCompressStream.CreateArchive(stream, leaveOpen: true), readerOptions ); reader.WriteAllToDirectory( @@ -73,7 +73,7 @@ public class WriterTests : TestBase writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using var writer = WriterFactory.OpenAsync(stream, _type, writerOptions); + using var writer = WriterFactory.OpenAsyncArchive(stream, _type, writerOptions); await writer.WriteAllAsync( ORIGINAL_FILES_PATH, "*", @@ -92,8 +92,8 @@ public class WriterTests : TestBase readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - await using var reader = ReaderFactory.OpenAsync( - new AsyncOnlyStream(SharpCompressStream.Create(stream, leaveOpen: true)), + await using var reader = ReaderFactory.OpenAsyncArchive( + new AsyncOnlyStream(SharpCompressStream.CreateArchive(stream, leaveOpen: true)), readerOptions, cancellationToken ); diff --git a/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs b/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs index eab7e1c7..f1b53f61 100644 --- a/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64AsyncTests.cs @@ -177,7 +177,7 @@ public class Zip64AsyncTests : WriterTests using var zip = File.OpenWrite(filename); using var st = forwardOnly ? (Stream)new ForwardOnlyStream(zip) : zip; - using var zipWriter = (ZipWriter)WriterFactory.Open(st, ArchiveType.Zip, opts); + using var zipWriter = (ZipWriter)WriterFactory.OpenWriter(st, ArchiveType.Zip, opts); for (var i = 0; i < files; i++) { using var str = zipWriter.WriteToStream(i.ToString(), eo); @@ -199,7 +199,7 @@ public class Zip64AsyncTests : WriterTests ZipEntry? prev = null; using (var fs = File.OpenRead(filename)) { - var rd = ReaderFactory.OpenAsync(fs, new ReaderOptions { LookForHeader = false }); + var rd = ReaderFactory.OpenAsyncReader(fs, new ReaderOptions { LookForHeader = false }); await using (rd) { while (await rd.MoveToNextEntryAsync()) @@ -236,7 +236,7 @@ public class Zip64AsyncTests : WriterTests public Tuple ReadArchive(string filename) { - using var archive = ArchiveFactory.Open(filename); + using var archive = ArchiveFactory.OpenArchive(filename); return new Tuple( archive.Entries.Count(), archive.Entries.Select(x => x.Size).Sum() diff --git a/tests/SharpCompress.Test/Zip/Zip64Tests.cs b/tests/SharpCompress.Test/Zip/Zip64Tests.cs index dc733df0..6242b877 100644 --- a/tests/SharpCompress.Test/Zip/Zip64Tests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64Tests.cs @@ -162,7 +162,7 @@ public class Zip64Tests : WriterTests using var zip = File.OpenWrite(filename); using var st = forwardOnly ? (Stream)new ForwardOnlyStream(zip) : zip; - using var zipWriter = (ZipWriter)WriterFactory.Open(st, ArchiveType.Zip, opts); + using var zipWriter = (ZipWriter)WriterFactory.OpenWriter(st, ArchiveType.Zip, opts); for (var i = 0; i < files; i++) { using var str = zipWriter.WriteToStream(i.ToString(), eo); @@ -182,7 +182,7 @@ public class Zip64Tests : WriterTests long size = 0; IEntry? prev = null; using (var fs = File.OpenRead(filename)) - using (var rd = ZipReader.Open(fs, new ReaderOptions { LookForHeader = false })) + using (var rd = ZipReader.OpenReader(fs, new ReaderOptions { LookForHeader = false })) { while (rd.MoveToNextEntry()) { @@ -208,7 +208,7 @@ public class Zip64Tests : WriterTests public Tuple ReadArchive(string filename) { - using var archive = ArchiveFactory.Open(filename); + using var archive = ArchiveFactory.OpenArchive(filename); return new Tuple( archive.Entries.Count(), archive.Entries.Select(x => x.Size).Sum() diff --git a/tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs b/tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs index b8451a45..b3a03342 100644 --- a/tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs @@ -37,7 +37,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = true, }; - ZipArchive zipArchive = ZipArchive.Create(); + ZipArchive zipArchive = ZipArchive.CreateArchive(); zipArchive.AddEntry("empty", new MemoryStream()); zipArchive.SaveTo(filename, writerOptions); @@ -141,7 +141,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = false, }; - ZipArchive zipArchive = ZipArchive.Create(); + ZipArchive zipArchive = ZipArchive.CreateArchive(); zipArchive.AddEntry("empty", new MemoryStream()); zipArchive.SaveTo(filename, writerOptions); @@ -192,7 +192,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = false, }; - ZipArchive zipArchive = ZipArchive.Create(); + ZipArchive zipArchive = ZipArchive.CreateArchive(); var data = new byte[100]; new Random(42).NextBytes(data); zipArchive.AddEntry("test.bin", new MemoryStream(data)); @@ -245,7 +245,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = false, }; - ZipArchive zipArchive = ZipArchive.Create(); + ZipArchive zipArchive = ZipArchive.CreateArchive(); var data = new byte[100]; new Random(42).NextBytes(data); zipArchive.AddEntry("test.bin", new MemoryStream(data)); @@ -298,7 +298,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = true, }; - ZipArchive zipArchive = ZipArchive.Create(); + ZipArchive zipArchive = ZipArchive.CreateArchive(); for (int i = 0; i < 5; i++) { var data = new byte[100]; diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs index ecc1c9c8..d16bec4f 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs @@ -126,7 +126,7 @@ public class ZipArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip"); - await using (var archive = ZipArchive.OpenAsync(unmodified)) + await using (var archive = ZipArchive.OpenAsyncArchive(unmodified)) { var entry = await archive.EntriesAsync.SingleAsync(x => x.Key.NotNull().EndsWith("jpg", StringComparison.OrdinalIgnoreCase) @@ -149,7 +149,7 @@ public class ZipArchiveAsyncTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - await using (var archive = ZipArchive.OpenAsync(unmodified)) + await using (var archive = ZipArchive.OpenAsyncArchive(unmodified)) { archive.AddEntry("jpg\\test.jpg", jpg); @@ -167,7 +167,7 @@ public class ZipArchiveAsyncTests : ArchiveTests var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Zip.deflate.noEmptyDirs.zip"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using (ZipArchive archive = ZipArchive.Create()) + using (ZipArchive archive = ZipArchive.CreateArchive()) { archive.DeflateCompressionLevel = CompressionLevel.BestSpeed; archive.AddAllFromDirectory(ORIGINAL_FILES_PATH); @@ -185,7 +185,7 @@ public class ZipArchiveAsyncTests : ArchiveTests { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.zip"))) { - IAsyncArchive archive = ZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + IAsyncArchive archive = ZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream)); try { await foreach (var entry in archive.EntriesAsync.Where(entry => !entry.IsDirectory)) @@ -209,7 +209,7 @@ public class ZipArchiveAsyncTests : ArchiveTests { using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.zip"))) { - IAsyncArchive archive = ZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + IAsyncArchive archive = ZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream)); try { await archive.WriteToDirectoryAsync( @@ -233,7 +233,7 @@ public class ZipArchiveAsyncTests : ArchiveTests using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.zip"))) { - IAsyncArchive archive = ZipArchive.OpenAsync(new AsyncOnlyStream(stream)); + IAsyncArchive archive = ZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream)); try { await archive.WriteToDirectoryAsync( diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveDirectoryTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveDirectoryTests.cs index 45bab5e3..183312cd 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveDirectoryTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveDirectoryTests.cs @@ -12,7 +12,7 @@ public class ZipArchiveDirectoryTests : TestBase [Fact] public void ZipArchive_AddDirectoryEntry_CreatesDirectoryEntry() { - using var archive = ZipArchive.Create(); + using var archive = ZipArchive.CreateArchive(); archive.AddDirectoryEntry("test-dir", DateTime.Now); @@ -25,7 +25,7 @@ public class ZipArchiveDirectoryTests : TestBase [Fact] public void ZipArchive_AddDirectoryEntry_MultipleDirectories() { - using var archive = ZipArchive.Create(); + using var archive = ZipArchive.CreateArchive(); archive.AddDirectoryEntry("dir1", DateTime.Now); archive.AddDirectoryEntry("dir2", DateTime.Now); @@ -39,7 +39,7 @@ public class ZipArchiveDirectoryTests : TestBase [Fact] public void ZipArchive_AddDirectoryEntry_MixedWithFiles() { - using var archive = ZipArchive.Create(); + using var archive = ZipArchive.CreateArchive(); archive.AddDirectoryEntry("dir1", DateTime.Now); @@ -62,7 +62,7 @@ public class ZipArchiveDirectoryTests : TestBase { var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "zip-directory-test.zip"); - using (var archive = ZipArchive.Create()) + using (var archive = ZipArchive.CreateArchive()) { archive.AddDirectoryEntry("dir1", DateTime.Now); archive.AddDirectoryEntry("dir2", DateTime.Now); @@ -84,7 +84,7 @@ public class ZipArchiveDirectoryTests : TestBase } } - using (var archive = ZipArchive.Open(scratchPath)) + using (var archive = ZipArchive.OpenArchive(scratchPath)) { var entries = archive.Entries.OrderBy(e => e.Key).ToList(); Assert.Equal(3, entries.Count); @@ -103,7 +103,7 @@ public class ZipArchiveDirectoryTests : TestBase [Fact] public void ZipArchive_AddDirectoryEntry_DuplicateKey_ThrowsException() { - using var archive = ZipArchive.Create(); + using var archive = ZipArchive.CreateArchive(); archive.AddDirectoryEntry("test-dir", DateTime.Now); diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index a8192416..562fc09d 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -235,7 +235,7 @@ public class ZipArchiveTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip"); - using (var archive = ZipArchive.Open(unmodified)) + using (var archive = ZipArchive.OpenArchive(unmodified)) { var entry = archive.Entries.Single(x => x.Key.NotNull().EndsWith("jpg", StringComparison.OrdinalIgnoreCase) @@ -258,7 +258,7 @@ public class ZipArchiveTests : ArchiveTests var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod.zip"); var modified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.mod2.zip"); - using (var archive = ZipArchive.Open(unmodified)) + using (var archive = ZipArchive.OpenArchive(unmodified)) { archive.AddEntry("jpg\\test.jpg", jpg); @@ -276,7 +276,7 @@ public class ZipArchiveTests : ArchiveTests var scratchPath1 = Path.Combine(SCRATCH_FILES_PATH, "a.zip"); var scratchPath2 = Path.Combine(SCRATCH_FILES_PATH, "b.zip"); - using (var arc = ZipArchive.Create()) + using (var arc = ZipArchive.CreateArchive()) { var str = "test.txt"; var source = new MemoryStream(Encoding.UTF8.GetBytes(str)); @@ -293,7 +293,7 @@ public class ZipArchiveTests : ArchiveTests { var scratchPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using var vfs = (ZipArchive)ArchiveFactory.Open(scratchPath); + using var vfs = (ZipArchive)ArchiveFactory.OpenArchive(scratchPath); var e = vfs.Entries.First(v => v.Key.NotNull().EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ); @@ -313,7 +313,7 @@ public class ZipArchiveTests : ArchiveTests [Fact] public void Zip_Create_NoDups() { - using var arc = ZipArchive.Create(); + using var arc = ZipArchive.CreateArchive(); arc.AddEntry("1.txt", new MemoryStream()); Assert.Throws(() => arc.AddEntry("\\1.txt", new MemoryStream())); } @@ -324,7 +324,7 @@ public class ZipArchiveTests : ArchiveTests var scratchPath1 = Path.Combine(SCRATCH_FILES_PATH, "a.zip"); var scratchPath2 = Path.Combine(SCRATCH_FILES_PATH, "b.zip"); - using (var arc = ZipArchive.Create()) + using (var arc = ZipArchive.CreateArchive()) { using (var stream = new MemoryStream(Encoding.UTF8.GetBytes("qwert"))) { @@ -370,7 +370,7 @@ public class ZipArchiveTests : ArchiveTests var scratchPath = Path.Combine(SCRATCH2_FILES_PATH, "Zip.deflate.noEmptyDirs.zip"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using (var archive = ZipArchive.Create()) + using (var archive = ZipArchive.CreateArchive()) { archive.AddAllFromDirectory(SCRATCH_FILES_PATH); @@ -389,7 +389,7 @@ public class ZipArchiveTests : ArchiveTests [Fact] public void Zip_Create_Empty_And_Read() { - var archive = ZipArchive.Create(); + var archive = ZipArchive.CreateArchive(); var archiveStream = new MemoryStream(); @@ -397,7 +397,7 @@ public class ZipArchiveTests : ArchiveTests archiveStream.Position = 0; - var readArchive = ArchiveFactory.Open(archiveStream); + var readArchive = ArchiveFactory.OpenArchive(archiveStream); var count = readArchive.Entries.Count(); @@ -435,7 +435,7 @@ public class ZipArchiveTests : ArchiveTests } var scratchPath = Path.Combine(SCRATCH2_FILES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using (var archive = ZipArchive.Create()) + using (var archive = ZipArchive.CreateArchive()) { archive.AddAllFromDirectory(SCRATCH_FILES_PATH); archive.RemoveEntry( @@ -455,7 +455,7 @@ public class ZipArchiveTests : ArchiveTests public void Zip_Deflate_WinzipAES_Read() { using ( - var reader = ZipArchive.Open( + var reader = ZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip"), new ReaderOptions { Password = "test" } ) @@ -475,7 +475,7 @@ public class ZipArchiveTests : ArchiveTests [Fact] public void Zip_Deflate_WinzipAES_MultiOpenEntryStream() { - using var reader = ZipArchive.Open( + using var reader = ZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES2.zip"), new ReaderOptions { Password = "test" } ); @@ -491,7 +491,7 @@ public class ZipArchiveTests : ArchiveTests [Fact] public void Zip_Read_Volume_Comment() { - using var reader = ZipArchive.Open( + using var reader = ZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Zip.zip64.zip"), new ReaderOptions { Password = "test" } ); @@ -507,7 +507,7 @@ public class ZipArchiveTests : ArchiveTests public void Zip_BZip2_Pkware_Read() { using ( - var reader = ZipArchive.Open( + var reader = ZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip"), new ReaderOptions { Password = "test" } ) @@ -529,7 +529,7 @@ public class ZipArchiveTests : ArchiveTests { var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - var a = ZipArchive.Open(unmodified); + var a = ZipArchive.OpenArchive(unmodified); var count = 0; foreach (var e in a.Entries) { @@ -540,7 +540,7 @@ public class ZipArchiveTests : ArchiveTests Assert.Equal(3, count); a.Dispose(); - a = ZipArchive.Open(unmodified); + a = ZipArchive.OpenArchive(unmodified); var count2 = 0; foreach (var e in a.Entries) @@ -572,7 +572,7 @@ public class ZipArchiveTests : ArchiveTests var zipFile = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.pkware.zip"); using var fileStream = File.OpenRead(zipFile); - using var archive = ArchiveFactory.Open( + using var archive = ArchiveFactory.OpenArchive( fileStream, new ReaderOptions { Password = "12345678" } ); @@ -596,7 +596,7 @@ public class ZipArchiveTests : ArchiveTests Assert.ThrowsAny(() => { - using var archive = ZipArchive.Open(zipFile); + using var archive = ZipArchive.OpenArchive(zipFile); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { entry.WriteToDirectory( @@ -618,7 +618,7 @@ public class ZipArchiveTests : ArchiveTests { MemoryStream stream = new NonSeekableMemoryStream(); - using (var zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate)) + using (var zipWriter = WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate)) { zipWriter.Write("foo.txt", new MemoryStream(Array.Empty())); zipWriter.Write("foo2.txt", new MemoryStream(new byte[10])); @@ -627,7 +627,7 @@ public class ZipArchiveTests : ArchiveTests stream = new MemoryStream(stream.ToArray()); File.WriteAllBytes(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), stream.ToArray()); - using (var zipArchive = ZipArchive.Open(stream)) + using (var zipArchive = ZipArchive.OpenArchive(stream)) { foreach (var entry in zipArchive.Entries) { @@ -649,7 +649,7 @@ public class ZipArchiveTests : ArchiveTests { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.badlocalextra.zip"); - using var za = ZipArchive.Open(zipPath); + using var za = ZipArchive.OpenArchive(zipPath); var ex = Record.Exception(() => { var firstEntry = za.Entries.First(x => x.Key == "first.txt"); @@ -669,7 +669,7 @@ public class ZipArchiveTests : ArchiveTests { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.datadescriptors.zip"); - using var za = ZipArchive.Open(zipPath); + using var za = ZipArchive.OpenArchive(zipPath); var firstEntry = za.Entries.First(x => x.Key == "first.txt"); var buffer = new byte[4096]; @@ -708,7 +708,7 @@ public class ZipArchiveTests : ArchiveTests { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.LongComment.zip"); - using var za = ZipArchive.Open(zipPath); + using var za = ZipArchive.OpenArchive(zipPath); var count = za.Entries.Count(); Assert.Equal(1, count); } @@ -718,7 +718,7 @@ public class ZipArchiveTests : ArchiveTests { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.zip64.compressedonly.zip"); - using var za = ZipArchive.Open(zipPath); + using var za = ZipArchive.OpenArchive(zipPath); var firstEntry = za.Entries.First(x => x.Key == "test/test.txt"); using var memoryStream = new MemoryStream(); @@ -732,7 +732,7 @@ public class ZipArchiveTests : ArchiveTests { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.uncompressed.zip"); using var stream = File.OpenRead(zipPath); - var reader = ReaderFactory.Open(stream); + var reader = ReaderFactory.OpenReader(stream); var entries = 0; while (reader.MoveToNextEntry()) { @@ -760,7 +760,7 @@ public class ZipArchiveTests : ArchiveTests }; var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.uncompressed.zip"); using var stream = File.OpenRead(zipPath); - var reader = ReaderFactory.Open(stream); + var reader = ReaderFactory.OpenReader(stream); var x = 0; while (reader.MoveToNextEntry()) { @@ -777,7 +777,7 @@ public class ZipArchiveTests : ArchiveTests var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.UnicodePathExtra.zip"); using (var stream = File.OpenRead(zipPath)) { - var reader = ReaderFactory.Open( + var reader = ReaderFactory.OpenReader( stream, new ReaderOptions { @@ -792,7 +792,7 @@ public class ZipArchiveTests : ArchiveTests } using (var stream = File.OpenRead(zipPath)) { - var reader = ReaderFactory.Open( + var reader = ReaderFactory.OpenReader( stream, new ReaderOptions { @@ -810,7 +810,7 @@ public class ZipArchiveTests : ArchiveTests [Fact] public void TestDataDescriptorRead() { - using var archive = ArchiveFactory.Open( + using var archive = ArchiveFactory.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.datadescriptors.zip") ); var firstEntry = archive.Entries.First(); @@ -822,7 +822,7 @@ public class ZipArchiveTests : ArchiveTests [Fact] public void Zip_EntryCommentAfterEntryRead() { - using var archive = ZipArchive.Open( + using var archive = ZipArchive.OpenArchive( Path.Combine(TEST_ARCHIVES_PATH, "Zip.EntryComment.zip") ); var firstEntry = (ZipArchiveEntry)archive.Entries.First(); @@ -834,7 +834,7 @@ public class ZipArchiveTests : ArchiveTests [Fact] public void Zip_FilePermissions() { - using var archive = ArchiveFactory.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.644.zip")); + using var archive = ArchiveFactory.OpenArchive(Path.Combine(TEST_ARCHIVES_PATH, "Zip.644.zip")); var firstEntry = archive.Entries.First(); const int S_IFREG = 0x8000; diff --git a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs index 471e8e31..d2dd3b08 100644 --- a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs @@ -131,7 +131,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests CompressionLevel = compressionLevel, }; - using (var writer = WriterFactory.OpenAsync(zipStream, ArchiveType.Zip, writerOptions)) + using (var writer = WriterFactory.OpenAsyncWriter(zipStream, ArchiveType.Zip, writerOptions)) { await writer.WriteAsync( $"{compressionType}_level_{compressionLevel}_{sizeMb}MiB.txt", @@ -152,7 +152,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests // Verify the archive zipStream.Position = 0; - using var archive = ZipArchive.Open(zipStream); + using var archive = ZipArchive.OpenArchive(zipStream); var entry = archive.Entries.Single(e => !e.IsDirectory); using var entryStream = await entry.OpenEntryStreamAsync(); @@ -209,7 +209,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests // Verify the archive zipStream.Position = 0; - using var archive = ZipArchive.Open(zipStream); + using var archive = ZipArchive.OpenArchive(zipStream); var entry = archive.Entries.Single(e => !e.IsDirectory); using var entryStream = await entry.OpenEntryStreamAsync(); @@ -248,7 +248,7 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests ) { zipStream.Position = 0; - using var archive = ZipArchive.Open(zipStream); + using var archive = ZipArchive.OpenArchive(zipStream); foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) { diff --git a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcTests.cs b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcTests.cs index 0e66edc2..fb9226be 100644 --- a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcTests.cs @@ -125,7 +125,7 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests CompressionLevel = compressionLevel, }; - using (var writer = WriterFactory.Open(zipStream, ArchiveType.Zip, writerOptions)) + using (var writer = WriterFactory.OpenWriter(zipStream, ArchiveType.Zip, writerOptions)) { writer.Write( $"{compressionType}_level_{compressionLevel}_{sizeMb}MiB.txt", @@ -146,7 +146,7 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests // Verify the archive zipStream.Position = 0; - using var archive = ZipArchive.Open(zipStream); + using var archive = ZipArchive.OpenArchive(zipStream); var entry = archive.Entries.Single(e => !e.IsDirectory); using var entryStream = entry.OpenEntryStream(); @@ -201,7 +201,7 @@ public class ZipTypesLevelsWithCrcRatioTests : ArchiveTests // Verify the archive zipStream.Position = 0; - using var archive = ZipArchive.Open(zipStream); + using var archive = ZipArchive.OpenArchive(zipStream); var entry = archive.Entries.Single(e => !e.IsDirectory); using var entryStream = entry.OpenEntryStream(); diff --git a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs index 7c2d85aa..c7bdf399 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs @@ -20,7 +20,7 @@ public class ZipReaderAsyncTests : ReaderTests { var path = Path.Combine(TEST_ARCHIVES_PATH, "PrePostHeaders.zip"); using Stream stream = new ForwardOnlyStream(File.OpenRead(path)); - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); var count = 0; while (await reader.MoveToNextEntryAsync()) { @@ -65,7 +65,7 @@ public class ZipReaderAsyncTests : ReaderTests using Stream stream = new ForwardOnlyStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ); - await using var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); var x = 0; while (await reader.MoveToNextEntryAsync()) { @@ -121,7 +121,7 @@ public class ZipReaderAsyncTests : ReaderTests using ( Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip")) ) - using (IReader baseReader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (IReader baseReader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) { IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) @@ -145,7 +145,7 @@ public class ZipReaderAsyncTests : ReaderTests using var stream = new TestStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ); - await using (var reader = ReaderFactory.OpenAsync(new AsyncOnlyStream(stream))) + await using (var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream))) { while (await reader.MoveToNextEntryAsync()) { @@ -169,7 +169,7 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ) ); - await using var reader = ReaderFactory.OpenAsync(stream); + await using var reader = ReaderFactory.OpenAsyncReader(stream); while (await reader.MoveToNextEntryAsync()) { if (!reader.Entry.IsDirectory) @@ -193,7 +193,7 @@ public class ZipReaderAsyncTests : ReaderTests ) ) using ( - IReader baseReader = ZipReader.Open(stream, new ReaderOptions { Password = "test" }) + IReader baseReader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" }) ) { IAsyncReader reader = (IAsyncReader)baseReader; @@ -220,7 +220,7 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip")) ) ) - using (IReader baseReader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (IReader baseReader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) { IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) @@ -247,7 +247,7 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "zipcrypto.zip")) ) ) - using (IReader baseReader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (IReader baseReader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) { IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) diff --git a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs index fc87dc57..a5dfc4ca 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs @@ -22,7 +22,7 @@ public class ZipReaderTests : ReaderTests { var path = Path.Combine(TEST_ARCHIVES_PATH, "PrePostHeaders.zip"); using Stream stream = new ForwardOnlyStream(File.OpenRead(path)); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); var count = 0; while (reader.MoveToNextEntry()) { @@ -62,7 +62,7 @@ public class ZipReaderTests : ReaderTests using Stream stream = new ForwardOnlyStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); var x = 0; while (reader.MoveToNextEntry()) { @@ -111,7 +111,7 @@ public class ZipReaderTests : ReaderTests using ( Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip")) ) - using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (var reader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) { while (reader.MoveToNextEntry()) { @@ -134,7 +134,7 @@ public class ZipReaderTests : ReaderTests using var stream = new TestStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ); - using (var reader = ReaderFactory.Open(stream)) + using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -156,7 +156,7 @@ public class ZipReaderTests : ReaderTests using var stream = new TestStream( File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.dd.zip")) ); - var reader = ReaderFactory.Open(stream); + var reader = ReaderFactory.OpenReader(stream); while (reader.MoveToNextEntry()) { if (!reader.Entry.IsDirectory) @@ -179,7 +179,7 @@ public class ZipReaderTests : ReaderTests Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.WinzipAES.zip") ) ) - using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (var reader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) { while (reader.MoveToNextEntry()) { @@ -204,7 +204,7 @@ public class ZipReaderTests : ReaderTests Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip") ) ) - using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (var reader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) { while (reader.MoveToNextEntry()) { @@ -226,7 +226,7 @@ public class ZipReaderTests : ReaderTests { var count = 0; using (Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "zipcrypto.zip"))) - using (var reader = ZipReader.Open(stream, new ReaderOptions { Password = "test" })) + using (var reader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) { while (reader.MoveToNextEntry()) { @@ -256,7 +256,7 @@ public class ZipReaderTests : ReaderTests using var memory = new MemoryStream(); Stream stream = new TestStream(memory, read: true, write: true, seek: false); - using (var zipWriter = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate)) + using (var zipWriter = WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate)) { zipWriter.Write(expected[0].Item1, new MemoryStream(expected[0].Item2)); zipWriter.Write(expected[1].Item1, new MemoryStream(expected[1].Item2)); @@ -265,7 +265,7 @@ public class ZipReaderTests : ReaderTests stream = new MemoryStream(memory.ToArray()); File.WriteAllBytes(Path.Combine(SCRATCH_FILES_PATH, "foo.zip"), memory.ToArray()); - using IReader zipReader = ZipReader.Open( + using IReader zipReader = ZipReader.OpenReader( SharpCompressStream.Create(stream, leaveOpen: true, throwOnDispose: true) ); var i = 0; @@ -297,7 +297,7 @@ public class ZipReaderTests : ReaderTests using Stream stream = File.OpenRead( Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.issue86.zip") ); - using var reader = ZipReader.Open(stream); + using var reader = ZipReader.OpenReader(stream); foreach (var key in keys) { reader.MoveToNextEntry(); @@ -319,7 +319,7 @@ public class ZipReaderTests : ReaderTests var keys = new[] { "version", "sizehint", "data/0/metadata", "data/0/records" }; using var fileStream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "test_477.zip")); - using var reader = ZipReader.Open(fileStream); + using var reader = ZipReader.OpenReader(fileStream); foreach (var key in keys) { reader.MoveToNextEntry(); @@ -333,7 +333,7 @@ public class ZipReaderTests : ReaderTests { var count = 0; using var fileStream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Issue_685.zip")); - using var reader = ZipReader.Open(fileStream); + using var reader = ZipReader.OpenReader(fileStream); while (reader.MoveToNextEntry()) { count++; @@ -347,7 +347,7 @@ public class ZipReaderTests : ReaderTests { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.uncompressed.zip"); using var stream = File.OpenRead(zipPath); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); while (reader.MoveToNextEntry()) { var target = new MemoryStream(); @@ -360,7 +360,7 @@ public class ZipReaderTests : ReaderTests { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "Zip.uncompressed.zip"); using var stream = File.OpenRead(zipPath); - using var reader = ReaderFactory.Open(stream); + using var reader = ReaderFactory.OpenReader(stream); while (reader.MoveToNextEntry()) { } } @@ -371,10 +371,10 @@ public class ZipReaderTests : ReaderTests { var zipPath = Path.Combine(TEST_ARCHIVES_PATH, "64bitstream.zip.7z"); using var stream = File.OpenRead(zipPath); - var archive = ArchiveFactory.Open(stream); + var archive = ArchiveFactory.OpenArchive(stream); var reader = archive.ExtractAllEntries(); reader.MoveToNextEntry(); - var zipReader = ZipReader.Open(reader.OpenEntryStream()); + var zipReader = ZipReader.OpenReader(reader.OpenEntryStream()); var x = 0; while (zipReader.MoveToNextEntry()) { @@ -387,7 +387,7 @@ public class ZipReaderTests : ReaderTests [Fact] public void Zip_Uncompressed_Encrypted_Read() { - using var reader = ReaderFactory.Open( + using var reader = ReaderFactory.OpenReader( Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.encrypted.zip"), new ReaderOptions { Password = "test" } ); @@ -415,7 +415,7 @@ public class ZipReaderTests : ReaderTests var readerKeys = new List(); using (var stream = File.OpenRead(path)) - using (var reader = ZipReader.Open(stream)) + using (var reader = ZipReader.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -424,7 +424,7 @@ public class ZipReaderTests : ReaderTests } var archiveKeys = new List(); - using (var archive = Archives.Zip.ZipArchive.Open(path)) + using (var archive = Archives.Zip.ZipArchive.OpenArchive(path)) { foreach (var entry in archive.Entries) { diff --git a/tests/SharpCompress.Test/Zip/ZipWriterDirectoryTests.cs b/tests/SharpCompress.Test/Zip/ZipWriterDirectoryTests.cs index a3b2788e..14bc143c 100644 --- a/tests/SharpCompress.Test/Zip/ZipWriterDirectoryTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipWriterDirectoryTests.cs @@ -22,7 +22,7 @@ public class ZipWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = ZipArchive.Open(memoryStream); + using var archive = ZipArchive.OpenArchive(memoryStream); var entries = archive.Entries.ToList(); Assert.Single(entries); @@ -42,7 +42,7 @@ public class ZipWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = ZipArchive.Open(memoryStream); + using var archive = ZipArchive.OpenArchive(memoryStream); var entries = archive.Entries.ToList(); Assert.Single(entries); @@ -62,7 +62,7 @@ public class ZipWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = ZipArchive.Open(memoryStream); + using var archive = ZipArchive.OpenArchive(memoryStream); var entries = archive.Entries.ToList(); Assert.Single(entries); @@ -82,7 +82,7 @@ public class ZipWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = ZipArchive.Open(memoryStream); + using var archive = ZipArchive.OpenArchive(memoryStream); Assert.Empty(archive.Entries); } @@ -101,7 +101,7 @@ public class ZipWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = ZipArchive.Open(memoryStream); + using var archive = ZipArchive.OpenArchive(memoryStream); var entries = archive.Entries.OrderBy(e => e.Key).ToList(); Assert.Equal(3, entries.Count); @@ -132,7 +132,7 @@ public class ZipWriterDirectoryTests : TestBase } memoryStream.Position = 0; - using var archive = ZipArchive.Open(memoryStream); + using var archive = ZipArchive.OpenArchive(memoryStream); var entries = archive.Entries.OrderBy(e => e.Key).ToList(); Assert.Equal(3, entries.Count); diff --git a/tests/SharpCompress.Test/Zip/ZipWriterTests.cs b/tests/SharpCompress.Test/Zip/ZipWriterTests.cs index 82e7caa2..70c2632d 100644 --- a/tests/SharpCompress.Test/Zip/ZipWriterTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipWriterTests.cs @@ -21,7 +21,7 @@ public class ZipWriterTests : WriterTests ArchiveEncoding = new ArchiveEncoding { Default = new UTF8Encoding(false) }, }; - using (var writer = WriterFactory.Open(memoryStream, ArchiveType.Zip, options)) + using (var writer = WriterFactory.OpenWriter(memoryStream, ArchiveType.Zip, options)) { writer.Write("test-folder/zero-byte-file.txt", Stream.Null); } @@ -39,7 +39,7 @@ public class ZipWriterTests : WriterTests ArchiveEncoding = new ArchiveEncoding { Default = new UTF8Encoding(false) }, }; - using (var writer = WriterFactory.Open(memoryStream, ArchiveType.Zip, options)) + using (var writer = WriterFactory.OpenWriter(memoryStream, ArchiveType.Zip, options)) { writer.Write("test-empty-folder/", Stream.Null); } From 2756b1f6f858c727b5fbc92b061c5102304bb7bb Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 15 Jan 2026 11:55:56 +0000 Subject: [PATCH 31/36] more refactor --- .../Archives/GZip/GZipArchive.cs | 4 ++-- src/SharpCompress/Archives/Rar/RarArchive.cs | 4 ++-- src/SharpCompress/Archives/Tar/TarArchive.cs | 4 ++-- src/SharpCompress/Archives/Zip/ZipArchive.cs | 4 ++-- src/SharpCompress/Factories/AceFactory.cs | 4 ++-- src/SharpCompress/Factories/ArcFactory.cs | 4 ++-- src/SharpCompress/Factories/ArjFactory.cs | 4 ++-- src/SharpCompress/Factories/GZipFactory.cs | 4 ++-- src/SharpCompress/Factories/RarFactory.cs | 4 ++-- src/SharpCompress/Factories/TarFactory.cs | 4 ++-- src/SharpCompress/Factories/ZipFactory.cs | 4 ++-- .../Readers/Ace/AceReader.Factory.cs | 22 ++++++++++--------- src/SharpCompress/Readers/Ace/AceReader.cs | 4 ++-- .../Readers/Arc/ArcReader.Factory.cs | 20 ++++++++--------- src/SharpCompress/Readers/Arc/ArcReader.cs | 2 +- .../Readers/Arj/ArjReader.Factory.cs | 20 ++++++++--------- src/SharpCompress/Readers/Arj/ArjReader.cs | 4 ++-- .../Readers/GZip/GZipReader.Factory.cs | 20 ++++++++--------- src/SharpCompress/Readers/GZip/GZipReader.cs | 6 ++--- src/SharpCompress/Readers/IReaderOpenable.cs | 12 +++++----- .../Readers/Rar/RarReader.Factory.cs | 12 +++++----- src/SharpCompress/Readers/Rar/RarReader.cs | 20 ++++++++--------- .../Readers/Tar/TarReader.Factory.cs | 20 ++++++++--------- src/SharpCompress/Readers/Tar/TarReader.cs | 6 ++--- .../Readers/Zip/ZipReader.Factory.cs | 20 ++++++++--------- src/SharpCompress/Readers/Zip/ZipReader.cs | 4 ++-- src/SharpCompress/Writers/WriterFactory.cs | 10 ++++----- tests/SharpCompress.Test/ArchiveTests.cs | 12 +++++----- tests/SharpCompress.Test/GZip/AsyncTests.cs | 10 ++++----- .../GZip/GZipReaderAsyncTests.cs | 4 ++-- .../GZip/GZipWriterAsyncTests.cs | 2 +- .../SevenZip/SevenZipArchiveAsyncTests.cs | 10 ++++----- .../Streams/ZLibBaseStreamAsyncTests.cs | 4 ++-- .../Streams/ZlibBaseStreamTests.cs | 4 ++-- tests/SharpCompress.Test/TestBase.cs | 4 ++-- tests/SharpCompress.Test/WriterTests.cs | 12 +++++----- 36 files changed, 155 insertions(+), 153 deletions(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 757c1bc2..7d4c345d 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -138,13 +138,13 @@ public partial class GZipArchive : AbstractWritableArchive CreateReaderForSolidExtractionAsync() { var stream = Volumes.Single().Stream; stream.Position = 0; - return new((IAsyncReader)GZipReader.Open(stream)); + return new((IAsyncReader)GZipReader.OpenReader(stream)); } } diff --git a/src/SharpCompress/Archives/Rar/RarArchive.cs b/src/SharpCompress/Archives/Rar/RarArchive.cs index d67dd15c..08e89686 100644 --- a/src/SharpCompress/Archives/Rar/RarArchive.cs +++ b/src/SharpCompress/Archives/Rar/RarArchive.cs @@ -87,12 +87,12 @@ public partial class RarArchive : AbstractArchive, I volume.Stream.Position = 0; return volume.Stream; }); - return (RarReader)RarReader.Open(streams, ReaderOptions); + return (RarReader)RarReader.OpenReader(streams, ReaderOptions); } var stream = Volumes.First().Stream; stream.Position = 0; - return (RarReader)RarReader.Open(stream, ReaderOptions); + return (RarReader)RarReader.OpenReader(stream, ReaderOptions); } public override bool IsSolid => Volumes.First().IsSolidArchive; diff --git a/src/SharpCompress/Archives/Tar/TarArchive.cs b/src/SharpCompress/Archives/Tar/TarArchive.cs index af06e47d..16eddbeb 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.cs @@ -180,13 +180,13 @@ public partial class TarArchive : AbstractWritableArchive CreateReaderForSolidExtractionAsync() { var stream = Volumes.Single().Stream; stream.Position = 0; - return new((IAsyncReader)TarReader.Open(stream)); + return new((IAsyncReader)TarReader.OpenReader(stream)); } } diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.cs b/src/SharpCompress/Archives/Zip/ZipArchive.cs index 5f1c84d8..9e2d80e1 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.cs @@ -244,13 +244,13 @@ public partial class ZipArchive : AbstractWritableArchive CreateReaderForSolidExtractionAsync() { var stream = Volumes.Single().Stream; stream.Position = 0; - return new((IAsyncReader)ZipReader.Open(stream)); + return new((IAsyncReader)ZipReader.OpenReader(stream)); } } diff --git a/src/SharpCompress/Factories/AceFactory.cs b/src/SharpCompress/Factories/AceFactory.cs index 82ebe82e..ce775277 100644 --- a/src/SharpCompress/Factories/AceFactory.cs +++ b/src/SharpCompress/Factories/AceFactory.cs @@ -30,7 +30,7 @@ namespace SharpCompress.Factories ) => AceHeader.IsArchive(stream); public IReader OpenReader(Stream stream, ReaderOptions? options) => - AceReader.Open(stream, options); + AceReader.OpenReader(stream, options); public IAsyncReader OpenReaderAsync( Stream stream, @@ -39,7 +39,7 @@ namespace SharpCompress.Factories ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)AceReader.Open(stream, options); + return (IAsyncReader)AceReader.OpenReader(stream, options); } public override ValueTask IsArchiveAsync( diff --git a/src/SharpCompress/Factories/ArcFactory.cs b/src/SharpCompress/Factories/ArcFactory.cs index acd361c8..9ca2b5f7 100644 --- a/src/SharpCompress/Factories/ArcFactory.cs +++ b/src/SharpCompress/Factories/ArcFactory.cs @@ -42,7 +42,7 @@ namespace SharpCompress.Factories } public IReader OpenReader(Stream stream, ReaderOptions? options) => - ArcReader.Open(stream, options); + ArcReader.OpenReader(stream, options); public IAsyncReader OpenReaderAsync( Stream stream, @@ -51,7 +51,7 @@ namespace SharpCompress.Factories ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)ArcReader.Open(stream, options); + return (IAsyncReader)ArcReader.OpenReader(stream, options); } public override ValueTask IsArchiveAsync( diff --git a/src/SharpCompress/Factories/ArjFactory.cs b/src/SharpCompress/Factories/ArjFactory.cs index 245fea5f..577bca52 100644 --- a/src/SharpCompress/Factories/ArjFactory.cs +++ b/src/SharpCompress/Factories/ArjFactory.cs @@ -33,7 +33,7 @@ namespace SharpCompress.Factories } public IReader OpenReader(Stream stream, ReaderOptions? options) => - ArjReader.Open(stream, options); + ArjReader.OpenReader(stream, options); public IAsyncReader OpenReaderAsync( Stream stream, @@ -42,7 +42,7 @@ namespace SharpCompress.Factories ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)ArjReader.Open(stream, options); + return (IAsyncReader)ArjReader.OpenReader(stream, options); } public override ValueTask IsArchiveAsync( diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index c7898b68..3ce26f86 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -154,7 +154,7 @@ public class GZipFactory /// public IReader OpenReader(Stream stream, ReaderOptions? options) => - GZipReader.Open(stream, options); + GZipReader.OpenReader(stream, options); /// public IAsyncReader OpenReaderAsync( @@ -164,7 +164,7 @@ public class GZipFactory ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)GZipReader.Open(stream, options); + return (IAsyncReader)GZipReader.OpenReader(stream, options); } /// diff --git a/src/SharpCompress/Factories/RarFactory.cs b/src/SharpCompress/Factories/RarFactory.cs index 7cc09e3c..4acbe4bc 100644 --- a/src/SharpCompress/Factories/RarFactory.cs +++ b/src/SharpCompress/Factories/RarFactory.cs @@ -113,7 +113,7 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade /// public IReader OpenReader(Stream stream, ReaderOptions? options) => - RarReader.Open(stream, options); + RarReader.OpenReader(stream, options); /// public IAsyncReader OpenReaderAsync( @@ -123,7 +123,7 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)RarReader.Open(stream, options); + return (IAsyncReader)RarReader.OpenReader(stream, options); } #endregion diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index 35827ea0..346a51d8 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -276,7 +276,7 @@ public class TarFactory /// public IReader OpenReader(Stream stream, ReaderOptions? options) => - TarReader.Open(stream, options); + TarReader.OpenReader(stream, options); /// public IAsyncReader OpenReaderAsync( @@ -286,7 +286,7 @@ public class TarFactory ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)TarReader.Open(stream, options); + return (IAsyncReader)TarReader.OpenReader(stream, options); } #endregion diff --git a/src/SharpCompress/Factories/ZipFactory.cs b/src/SharpCompress/Factories/ZipFactory.cs index 8487fa5d..97b47f17 100644 --- a/src/SharpCompress/Factories/ZipFactory.cs +++ b/src/SharpCompress/Factories/ZipFactory.cs @@ -200,7 +200,7 @@ public class ZipFactory /// public IReader OpenReader(Stream stream, ReaderOptions? options) => - ZipReader.Open(stream, options); + ZipReader.OpenReader(stream, options); /// public IAsyncReader OpenReaderAsync( @@ -210,7 +210,7 @@ public class ZipFactory ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)ZipReader.Open(stream, options); + return (IAsyncReader)ZipReader.OpenReader(stream, options); } #endregion diff --git a/src/SharpCompress/Readers/Ace/AceReader.Factory.cs b/src/SharpCompress/Readers/Ace/AceReader.Factory.cs index e65c18de..ad3cc3bd 100644 --- a/src/SharpCompress/Readers/Ace/AceReader.Factory.cs +++ b/src/SharpCompress/Readers/Ace/AceReader.Factory.cs @@ -7,7 +7,7 @@ namespace SharpCompress.Readers.Ace; public partial class AceReader : IReaderOpenable { - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -15,39 +15,41 @@ public partial class AceReader : IReaderOpenable { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IAsyncReader)Open(new FileInfo(path), readerOptions); + return (IAsyncReader)OpenReader(new FileInfo(path), readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(stream, readerOptions); + return (IAsyncReader)OpenReader(stream, readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(fileInfo, readerOptions); + return (IAsyncReader)OpenReader(fileInfo, readerOptions); } - public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + public static IReader OpenReader(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions); + return OpenReader(new FileInfo(filePath), readerOptions); } - public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); - return Open(fileInfo.OpenRead(), readerOptions); + return OpenReader(fileInfo.OpenRead(), readerOptions); } + + } #endif diff --git a/src/SharpCompress/Readers/Ace/AceReader.cs b/src/SharpCompress/Readers/Ace/AceReader.cs index 12ad92d3..8a783509 100644 --- a/src/SharpCompress/Readers/Ace/AceReader.cs +++ b/src/SharpCompress/Readers/Ace/AceReader.cs @@ -50,7 +50,7 @@ namespace SharpCompress.Readers.Ace /// The stream containing the ACE archive. /// Reader options. /// An AceReader instance. - public static IReader Open(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new SingleVolumeAceReader(stream, options ?? new ReaderOptions()); @@ -62,7 +62,7 @@ namespace SharpCompress.Readers.Ace /// /// /// - public static IReader Open(IEnumerable streams, ReaderOptions? options = null) + public static IReader OpenReader(IEnumerable streams, ReaderOptions? options = null) { streams.NotNull(nameof(streams)); return new MultiVolumeAceReader(streams, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/Arc/ArcReader.Factory.cs b/src/SharpCompress/Readers/Arc/ArcReader.Factory.cs index e8052ddc..22ec2666 100644 --- a/src/SharpCompress/Readers/Arc/ArcReader.Factory.cs +++ b/src/SharpCompress/Readers/Arc/ArcReader.Factory.cs @@ -7,7 +7,7 @@ namespace SharpCompress.Readers.Arc; public partial class ArcReader : IReaderOpenable { - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -15,39 +15,39 @@ public partial class ArcReader : IReaderOpenable { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IAsyncReader)Open(new FileInfo(path), readerOptions); + return (IAsyncReader)OpenReader(new FileInfo(path), readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(stream, readerOptions); + return (IAsyncReader)OpenReader(stream, readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(fileInfo, readerOptions); + return (IAsyncReader)OpenReader(fileInfo, readerOptions); } - public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + public static IReader OpenReader(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions); + return OpenReader(new FileInfo(filePath), readerOptions); } - public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); - return Open(fileInfo.OpenRead(), readerOptions); + return OpenReader(fileInfo.OpenRead(), readerOptions); } } #endif diff --git a/src/SharpCompress/Readers/Arc/ArcReader.cs b/src/SharpCompress/Readers/Arc/ArcReader.cs index a797ce2d..c99e8096 100644 --- a/src/SharpCompress/Readers/Arc/ArcReader.cs +++ b/src/SharpCompress/Readers/Arc/ArcReader.cs @@ -22,7 +22,7 @@ namespace SharpCompress.Readers.Arc /// /// /// - public static IReader Open(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new ArcReader(stream, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/Arj/ArjReader.Factory.cs b/src/SharpCompress/Readers/Arj/ArjReader.Factory.cs index 9d9cefc0..f0f7b01b 100644 --- a/src/SharpCompress/Readers/Arj/ArjReader.Factory.cs +++ b/src/SharpCompress/Readers/Arj/ArjReader.Factory.cs @@ -7,7 +7,7 @@ namespace SharpCompress.Readers.Arj; public partial class ArjReader : IReaderOpenable { - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -15,39 +15,39 @@ public partial class ArjReader : IReaderOpenable { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IAsyncReader)Open(new FileInfo(path), readerOptions); + return (IAsyncReader)OpenReader(new FileInfo(path), readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(stream, readerOptions); + return (IAsyncReader)OpenReader(stream, readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(fileInfo, readerOptions); + return (IAsyncReader)OpenReader(fileInfo, readerOptions); } - public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + public static IReader OpenReader(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions); + return OpenReader(new FileInfo(filePath), readerOptions); } - public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); - return Open(fileInfo.OpenRead(), readerOptions); + return OpenReader(fileInfo.OpenRead(), readerOptions); } } #endif diff --git a/src/SharpCompress/Readers/Arj/ArjReader.cs b/src/SharpCompress/Readers/Arj/ArjReader.cs index f7d8d6e5..94a89185 100644 --- a/src/SharpCompress/Readers/Arj/ArjReader.cs +++ b/src/SharpCompress/Readers/Arj/ArjReader.cs @@ -27,7 +27,7 @@ namespace SharpCompress.Readers.Arj /// /// /// - public static IReader Open(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new SingleVolumeArjReader(stream, options ?? new ReaderOptions()); @@ -39,7 +39,7 @@ namespace SharpCompress.Readers.Arj /// /// /// - public static IReader Open(IEnumerable streams, ReaderOptions? options = null) + public static IReader OpenReader(IEnumerable streams, ReaderOptions? options = null) { streams.NotNull(nameof(streams)); return new MultiVolumeArjReader(streams, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs b/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs index 02344555..60629816 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.Factory.cs @@ -7,7 +7,7 @@ namespace SharpCompress.Readers.GZip; public partial class GZipReader : IReaderOpenable { - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -15,39 +15,39 @@ public partial class GZipReader : IReaderOpenable { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IAsyncReader)Open(new FileInfo(path), readerOptions); + return (IAsyncReader)OpenReader(new FileInfo(path), readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(stream, readerOptions); + return (IAsyncReader)OpenReader(stream, readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(fileInfo, readerOptions); + return (IAsyncReader)OpenReader(fileInfo, readerOptions); } - public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + public static IReader OpenReader(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions); + return OpenReader(new FileInfo(filePath), readerOptions); } - public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); - return Open(fileInfo.OpenRead(), readerOptions); + return OpenReader(fileInfo.OpenRead(), readerOptions); } } #endif diff --git a/src/SharpCompress/Readers/GZip/GZipReader.cs b/src/SharpCompress/Readers/GZip/GZipReader.cs index f7c3cf15..9fec5993 100644 --- a/src/SharpCompress/Readers/GZip/GZipReader.cs +++ b/src/SharpCompress/Readers/GZip/GZipReader.cs @@ -12,7 +12,7 @@ public partial class GZipReader : AbstractReader public override GZipVolume Volume { get; } - #region Open + #region OpenReader /// /// Opens a GZipReader for Non-seeking usage with a single volume @@ -20,13 +20,13 @@ public partial class GZipReader : AbstractReader /// /// /// - public static IReader Open(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new GZipReader(stream, options ?? new ReaderOptions()); } - #endregion Open + #endregion OpenReader protected override IEnumerable GetEntries(Stream stream) => GZipEntry.GetEntries(stream, Options); diff --git a/src/SharpCompress/Readers/IReaderOpenable.cs b/src/SharpCompress/Readers/IReaderOpenable.cs index c55874cb..7d0e9d54 100644 --- a/src/SharpCompress/Readers/IReaderOpenable.cs +++ b/src/SharpCompress/Readers/IReaderOpenable.cs @@ -6,25 +6,25 @@ namespace SharpCompress.Readers; public interface IReaderOpenable { - public static abstract IReader Open(string filePath, ReaderOptions? readerOptions = null); + public static abstract IReader OpenReader(string filePath, ReaderOptions? readerOptions = null); - public static abstract IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null); + public static abstract IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null); - public static abstract IReader Open(Stream stream, ReaderOptions? readerOptions = null); + public static abstract IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null); - public static abstract IAsyncReader OpenAsync( + public static abstract IAsyncReader OpenAsyncReader( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ); - public static abstract IAsyncReader OpenAsync( + public static abstract IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ); - public static abstract IAsyncReader OpenAsync( + public static abstract IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Readers/Rar/RarReader.Factory.cs b/src/SharpCompress/Readers/Rar/RarReader.Factory.cs index 35a1be5d..775b3764 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.Factory.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.Factory.cs @@ -7,7 +7,7 @@ namespace SharpCompress.Readers.Rar; public partial class RarReader : IReaderOpenable { - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -15,27 +15,27 @@ public partial class RarReader : IReaderOpenable { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IAsyncReader)Open(new FileInfo(path), readerOptions); + return (IAsyncReader)OpenReader(new FileInfo(path), readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(stream, readerOptions); + return (IAsyncReader)OpenReader(stream, readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(fileInfo, readerOptions); + return (IAsyncReader)OpenReader(fileInfo, readerOptions); } } #endif diff --git a/src/SharpCompress/Readers/Rar/RarReader.cs b/src/SharpCompress/Readers/Rar/RarReader.cs index 1a127b69..34c64d1c 100644 --- a/src/SharpCompress/Readers/Rar/RarReader.cs +++ b/src/SharpCompress/Readers/Rar/RarReader.cs @@ -40,27 +40,27 @@ public abstract partial class RarReader : AbstractReader volume; - public static IReader Open(string filePath, ReaderOptions? options = null) + public static IReader OpenReader(string filePath, ReaderOptions? options = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), options); + return OpenReader(new FileInfo(filePath), options); } - public static IReader Open(FileInfo fileInfo, ReaderOptions? options = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? options = null) { options ??= new ReaderOptions { LeaveStreamOpen = false }; - return Open(fileInfo.OpenRead(), options); + return OpenReader(fileInfo.OpenRead(), options); } - public static IReader Open(IEnumerable filePaths, ReaderOptions? options = null) + public static IReader OpenReader(IEnumerable filePaths, ReaderOptions? options = null) { - return Open(filePaths.Select(x => new FileInfo(x)), options); + return OpenReader(filePaths.Select(x => new FileInfo(x)), options); } - public static IReader Open(IEnumerable fileInfos, ReaderOptions? options = null) + public static IReader OpenReader(IEnumerable fileInfos, ReaderOptions? options = null) { options ??= new ReaderOptions { LeaveStreamOpen = false }; - return Open(fileInfos.Select(x => x.OpenRead()), options); + return OpenReader(fileInfos.Select(x => x.OpenRead()), options); } /// @@ -69,7 +69,7 @@ public abstract partial class RarReader : AbstractReader /// /// - public static IReader Open(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new SingleVolumeRarReader(stream, options ?? new ReaderOptions()); @@ -81,7 +81,7 @@ public abstract partial class RarReader : AbstractReader /// /// - public static IReader Open(IEnumerable streams, ReaderOptions? options = null) + public static IReader OpenReader(IEnumerable streams, ReaderOptions? options = null) { streams.NotNull(nameof(streams)); return new MultiVolumeRarReader(streams, options ?? new ReaderOptions()); diff --git a/src/SharpCompress/Readers/Tar/TarReader.Factory.cs b/src/SharpCompress/Readers/Tar/TarReader.Factory.cs index b009afe4..9a85c4af 100644 --- a/src/SharpCompress/Readers/Tar/TarReader.Factory.cs +++ b/src/SharpCompress/Readers/Tar/TarReader.Factory.cs @@ -7,7 +7,7 @@ namespace SharpCompress.Readers.Tar; public partial class TarReader : IReaderOpenable { - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -15,39 +15,39 @@ public partial class TarReader : IReaderOpenable { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IAsyncReader)Open(new FileInfo(path), readerOptions); + return (IAsyncReader)OpenReader(new FileInfo(path), readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(stream, readerOptions); + return (IAsyncReader)OpenReader(stream, readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(fileInfo, readerOptions); + return (IAsyncReader)OpenReader(fileInfo, readerOptions); } - public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + public static IReader OpenReader(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions); + return OpenReader(new FileInfo(filePath), readerOptions); } - public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); - return Open(fileInfo.OpenRead(), readerOptions); + return OpenReader(fileInfo.OpenRead(), readerOptions); } } #endif diff --git a/src/SharpCompress/Readers/Tar/TarReader.cs b/src/SharpCompress/Readers/Tar/TarReader.cs index 54444954..7f53961f 100644 --- a/src/SharpCompress/Readers/Tar/TarReader.cs +++ b/src/SharpCompress/Readers/Tar/TarReader.cs @@ -45,7 +45,7 @@ public partial class TarReader : AbstractReader }; } - #region Open + #region OpenReader /// /// Opens a TarReader for Non-seeking usage with a single volume @@ -53,7 +53,7 @@ public partial class TarReader : AbstractReader /// /// /// - public static IReader Open(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); options = options ?? new ReaderOptions(); @@ -115,7 +115,7 @@ public partial class TarReader : AbstractReader return new TarReader(rewindableStream, options, CompressionType.None); } - #endregion Open + #endregion OpenReader protected override IEnumerable GetEntries(Stream stream) => TarEntry.GetEntries( diff --git a/src/SharpCompress/Readers/Zip/ZipReader.Factory.cs b/src/SharpCompress/Readers/Zip/ZipReader.Factory.cs index c244e5de..289059fc 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.Factory.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.Factory.cs @@ -7,7 +7,7 @@ namespace SharpCompress.Readers.Zip; public partial class ZipReader : IReaderOpenable { - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -15,39 +15,39 @@ public partial class ZipReader : IReaderOpenable { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IAsyncReader)Open(new FileInfo(path), readerOptions); + return (IAsyncReader)OpenReader(new FileInfo(path), readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(stream, readerOptions); + return (IAsyncReader)OpenReader(stream, readerOptions); } - public static IAsyncReader OpenAsync( + public static IAsyncReader OpenAsyncReader( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IAsyncReader)Open(fileInfo, readerOptions); + return (IAsyncReader)OpenReader(fileInfo, readerOptions); } - public static IReader Open(string filePath, ReaderOptions? readerOptions = null) + public static IReader OpenReader(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), readerOptions); + return OpenReader(new FileInfo(filePath), readerOptions); } - public static IReader Open(FileInfo fileInfo, ReaderOptions? readerOptions = null) + public static IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null) { fileInfo.NotNull(nameof(fileInfo)); - return Open(fileInfo.OpenRead(), readerOptions); + return OpenReader(fileInfo.OpenRead(), readerOptions); } } #endif diff --git a/src/SharpCompress/Readers/Zip/ZipReader.cs b/src/SharpCompress/Readers/Zip/ZipReader.cs index ec5f729b..f91266cf 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.cs @@ -45,13 +45,13 @@ public partial class ZipReader : AbstractReader /// /// /// - public static IReader Open(Stream stream, ReaderOptions? options = null) + public static IReader OpenReader(Stream stream, ReaderOptions? options = null) { stream.NotNull(nameof(stream)); return new ZipReader(stream, options ?? new ReaderOptions()); } - public static IReader Open(Stream stream, ReaderOptions? options, IEnumerable entries) + public static IReader OpenReader(Stream stream, ReaderOptions? options, IEnumerable entries) { stream.NotNull(nameof(stream)); return new ZipReader(stream, options ?? new ReaderOptions(), entries); diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index 0237e985..8d36c14d 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -8,24 +8,24 @@ namespace SharpCompress.Writers; public static class WriterFactory { - public static IWriter Open( + public static IWriter OpenWriter( string filePath, ArchiveType archiveType, WriterOptions writerOptions ) { filePath.NotNullOrEmpty(nameof(filePath)); - return Open(new FileInfo(filePath), archiveType, writerOptions); + return OpenWriter(new FileInfo(filePath), archiveType, writerOptions); } - public static IWriter Open( + public static IWriter OpenWriter( FileInfo fileInfo, ArchiveType archiveType, WriterOptions writerOptions ) { fileInfo.NotNull(nameof(fileInfo)); - return Open(fileInfo.OpenWrite(), archiveType, writerOptions); + return OpenWriter(fileInfo.OpenWrite(), archiveType, writerOptions); } public static IAsyncWriter OpenAsyncWriter( @@ -55,7 +55,7 @@ public static class WriterFactory ); } - public static IWriter Open(Stream stream, ArchiveType archiveType, WriterOptions writerOptions) + public static IWriter OpenWriter(Stream stream, ArchiveType archiveType, WriterOptions writerOptions) { var factory = Factories .Factory.Factories.OfType() diff --git a/tests/SharpCompress.Test/ArchiveTests.cs b/tests/SharpCompress.Test/ArchiveTests.cs index e392f9d3..84a3596d 100644 --- a/tests/SharpCompress.Test/ArchiveTests.cs +++ b/tests/SharpCompress.Test/ArchiveTests.cs @@ -43,7 +43,7 @@ public class ArchiveTests : ReaderTests foreach (var path in testArchives) { using ( - var stream = SharpCompressStream.CreateArchive( + var stream = SharpCompressStream.Create( File.OpenRead(path), leaveOpen: true, throwOnDispose: true @@ -123,7 +123,7 @@ public class ArchiveTests : ReaderTests foreach (var path in testArchives) { using ( - var stream = SharpCompressStream.CreateArchive( + var stream = SharpCompressStream.Create( File.OpenRead(path), leaveOpen: true, throwOnDispose: true @@ -380,7 +380,7 @@ public class ArchiveTests : ReaderTests { writerOptions.CompressionLevel = compressionLevel.Value; } - return WriterFactory.OpenArchive(stream, ArchiveType.Zip, writerOptions); + return WriterFactory.OpenWriter(stream, ArchiveType.Zip, writerOptions); } protected static IAsyncWriter CreateWriterWithLevelAsync( @@ -394,7 +394,7 @@ public class ArchiveTests : ReaderTests { writerOptions.CompressionLevel = compressionLevel.Value; } - return WriterFactory.OpenAsyncArchive( + return WriterFactory.OpenAsyncWriter( new AsyncOnlyStream(stream), ArchiveType.Zip, writerOptions @@ -615,7 +615,7 @@ public class ArchiveTests : ReaderTests foreach (var path in testArchives) { using ( - var stream = SharpCompressStream.CreateArchive( + var stream = SharpCompressStream.Create( File.OpenRead(path), leaveOpen: true, throwOnDispose: true @@ -661,7 +661,7 @@ public class ArchiveTests : ReaderTests // Open with a pre-wrapped stream using (var fileStream = File.OpenRead(testArchive)) - using (var wrappedStream = SharpCompressStream.CreateArchive(fileStream, bufferSize: 32768)) + using (var wrappedStream = SharpCompressStream.Create(fileStream, bufferSize: 32768)) await using ( var archive = await ArchiveFactory.OpenAsyncArchive(new AsyncOnlyStream(wrappedStream)) ) diff --git a/tests/SharpCompress.Test/GZip/AsyncTests.cs b/tests/SharpCompress.Test/GZip/AsyncTests.cs index a35b295f..fc6109b3 100644 --- a/tests/SharpCompress.Test/GZip/AsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/AsyncTests.cs @@ -26,7 +26,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = ReaderFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); await reader.WriteAllToDirectoryAsync( SCRATCH_FILES_PATH, @@ -51,7 +51,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = ReaderFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { @@ -106,7 +106,7 @@ public class AsyncTests : TestBase await using (var stream = File.Create(outputPath)) #endif using ( - var writer = WriterFactory.OpenAsyncArchive( + var writer = WriterFactory.OpenAsyncWriter( new AsyncOnlyStream(stream), ArchiveType.Zip, CompressionType.Deflate @@ -141,7 +141,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = ReaderFactory.OpenAsyncArchive( + await using var reader = ReaderFactory.OpenAsyncReader( new AsyncOnlyStream(stream), cancellationToken: cts.Token ); @@ -195,7 +195,7 @@ public class AsyncTests : TestBase #else await using var stream = File.OpenRead(testArchive); #endif - await using var reader = ReaderFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { diff --git a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs index 8f47f8ab..175a8990 100644 --- a/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipReaderAsyncTests.cs @@ -22,7 +22,7 @@ public class GZipReaderAsyncTests : ReaderTests { //read only as GZip item using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - await using var reader = ReaderFactory.OpenAsyncArchive(new AsyncOnlyStream(stream)); + await using var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(stream)); while (await reader.MoveToNextEntryAsync()) { Assert.NotEqual(0, reader.Entry.Size); @@ -70,7 +70,7 @@ public class GZipReaderAsyncTests : ReaderTests bufferSize: options.BufferSize ); using var testStream = new TestStream(protectedStream); - await using (var reader = ReaderFactory.OpenAsyncArchive(testStream, options, default)) + await using (var reader = ReaderFactory.OpenAsyncReader(testStream, options, default)) { await UseReaderAsync(reader, expectedCompression); protectedStream.ThrowOnDispose = false; diff --git a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs index 80a59a59..d23c379a 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterAsyncTests.cs @@ -24,7 +24,7 @@ public class GZipWriterAsyncTests : WriterTests ) ) using ( - var writer = WriterFactory.OpenAsyncArchive( + var writer = WriterFactory.OpenAsyncWriter( new AsyncOnlyStream(stream), ArchiveType.GZip, CompressionType.GZip diff --git a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveAsyncTests.cs index a7920f0b..ccd7866d 100644 --- a/tests/SharpCompress.Test/SevenZip/SevenZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/SevenZip/SevenZipArchiveAsyncTests.cs @@ -29,7 +29,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.CreateArchive(targetPath); + await using var targetStream = File.Create(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } @@ -54,7 +54,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.CreateArchive(targetPath); + await using var targetStream = File.Create(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } @@ -79,7 +79,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.CreateArchive(targetPath); + await using var targetStream = File.Create(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } @@ -104,7 +104,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.CreateArchive(targetPath); + await using var targetStream = File.Create(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } @@ -129,7 +129,7 @@ public class SevenZipArchiveAsyncTests : ArchiveTests } using var sourceStream = await entry.OpenEntryStreamAsync(CancellationToken.None); - await using var targetStream = File.CreateArchive(targetPath); + await using var targetStream = File.Create(targetPath); await sourceStream.CopyToAsync(targetStream, CancellationToken.None); } diff --git a/tests/SharpCompress.Test/Streams/ZLibBaseStreamAsyncTests.cs b/tests/SharpCompress.Test/Streams/ZLibBaseStreamAsyncTests.cs index 39c9f92b..a29477da 100644 --- a/tests/SharpCompress.Test/Streams/ZLibBaseStreamAsyncTests.cs +++ b/tests/SharpCompress.Test/Streams/ZLibBaseStreamAsyncTests.cs @@ -86,7 +86,7 @@ public class ZLibBaseStreamAsyncTests private async ValueTask CompressAsync(Stream input, Stream output, int compressionLevel) { using var zlibStream = new ZlibStream( - SharpCompressStream.CreateArchive(output, leaveOpen: true), + SharpCompressStream.Create(output, leaveOpen: true), CompressionMode.Compress, (CompressionLevel)compressionLevel ); @@ -97,7 +97,7 @@ public class ZLibBaseStreamAsyncTests private async ValueTask DecompressAsync(Stream input, Stream output) { using var zlibStream = new ZlibStream( - SharpCompressStream.CreateArchive(input, leaveOpen: true), + SharpCompressStream.Create(input, leaveOpen: true), CompressionMode.Decompress ); await zlibStream.CopyToAsync(output).ConfigureAwait(false); diff --git a/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs b/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs index 7a5d547e..5808078c 100644 --- a/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs +++ b/tests/SharpCompress.Test/Streams/ZlibBaseStreamTests.cs @@ -80,7 +80,7 @@ public class ZLibBaseStreamTests private void Compress(Stream input, Stream output, int compressionLevel) { using var zlibStream = new ZlibStream( - SharpCompressStream.CreateArchive(output, leaveOpen: true), + SharpCompressStream.Create(output, leaveOpen: true), CompressionMode.Compress, (CompressionLevel)compressionLevel ); @@ -91,7 +91,7 @@ public class ZLibBaseStreamTests private void Decompress(Stream input, Stream output) { using var zlibStream = new ZlibStream( - SharpCompressStream.CreateArchive(input, leaveOpen: true), + SharpCompressStream.Create(input, leaveOpen: true), CompressionMode.Decompress ); zlibStream.CopyTo(output); diff --git a/tests/SharpCompress.Test/TestBase.cs b/tests/SharpCompress.Test/TestBase.cs index 5a5ea765..e2a98f13 100644 --- a/tests/SharpCompress.Test/TestBase.cs +++ b/tests/SharpCompress.Test/TestBase.cs @@ -228,8 +228,8 @@ public class TestBase : IAsyncDisposable //don't compare the order. OS X reads files from the file system in a different order therefore makes the archive ordering different var archive1Entries = new List(); var archive2Entries = new List(); - using (var archive1 = ReaderFactory.OpenArchive(File.OpenRead(file1), readerOptions)) - using (var archive2 = ReaderFactory.OpenArchive(File.OpenRead(file2), readerOptions)) + using (var archive1 = ReaderFactory.OpenReader(File.OpenRead(file1), readerOptions)) + using (var archive2 = ReaderFactory.OpenReader(File.OpenRead(file2), readerOptions)) { while (archive1.MoveToNextEntry()) { diff --git a/tests/SharpCompress.Test/WriterTests.cs b/tests/SharpCompress.Test/WriterTests.cs index bb16ac03..45a41e7f 100644 --- a/tests/SharpCompress.Test/WriterTests.cs +++ b/tests/SharpCompress.Test/WriterTests.cs @@ -29,7 +29,7 @@ public class WriterTests : TestBase writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using var writer = WriterFactory.OpenArchive(stream, _type, writerOptions); + using var writer = WriterFactory.OpenWriter(stream, _type, writerOptions); writer.WriteAll(ORIGINAL_FILES_PATH, "*", SearchOption.AllDirectories); } CompareArchivesByPath( @@ -43,8 +43,8 @@ public class WriterTests : TestBase readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using var reader = ReaderFactory.OpenArchive( - SharpCompressStream.CreateArchive(stream, leaveOpen: true), + using var reader = ReaderFactory.OpenReader( + SharpCompressStream.Create(stream, leaveOpen: true), readerOptions ); reader.WriteAllToDirectory( @@ -73,7 +73,7 @@ public class WriterTests : TestBase writerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - using var writer = WriterFactory.OpenAsyncArchive(stream, _type, writerOptions); + using var writer = WriterFactory.OpenAsyncWriter(stream, _type, writerOptions); await writer.WriteAllAsync( ORIGINAL_FILES_PATH, "*", @@ -92,8 +92,8 @@ public class WriterTests : TestBase readerOptions.ArchiveEncoding.Default = encoding ?? Encoding.Default; - await using var reader = ReaderFactory.OpenAsyncArchive( - new AsyncOnlyStream(SharpCompressStream.CreateArchive(stream, leaveOpen: true)), + await using var reader = ReaderFactory.OpenAsyncReader( + new AsyncOnlyStream(SharpCompressStream.Create(stream, leaveOpen: true)), readerOptions, cancellationToken ); From 779fba5debd3815486b936a94b5cd1ffe5f084c7 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 15 Jan 2026 12:06:54 +0000 Subject: [PATCH 32/36] finish the open refactor? --- src/SharpCompress/Archives/ArchiveFactory.cs | 5 +++- src/SharpCompress/Factories/AceFactory.cs | 2 +- src/SharpCompress/Factories/ArcFactory.cs | 2 +- src/SharpCompress/Factories/ArjFactory.cs | 2 +- src/SharpCompress/Factories/Factory.cs | 2 +- src/SharpCompress/Factories/GZipFactory.cs | 2 +- src/SharpCompress/Factories/RarFactory.cs | 2 +- src/SharpCompress/Factories/TarFactory.cs | 2 +- src/SharpCompress/Factories/ZipFactory.cs | 2 +- .../Readers/Ace/AceReader.Factory.cs | 2 -- src/SharpCompress/Readers/IReaderFactory.cs | 12 ++++++-- src/SharpCompress/Readers/IReaderOpenable.cs | 5 +++- src/SharpCompress/Readers/ReaderFactory.cs | 4 +-- src/SharpCompress/Readers/Zip/ZipReader.cs | 6 +++- src/SharpCompress/Writers/WriterFactory.cs | 13 +++++++-- .../GZip/GZipWriterTests.cs | 10 +++++-- .../SharpCompress.Test/ProgressReportTests.cs | 5 +++- .../Rar/RarArchiveAsyncTests.cs | 18 +++++++++--- .../SharpCompress.Test/Rar/RarArchiveTests.cs | 8 ++++-- .../Rar/RarReaderAsyncTests.cs | 5 +++- .../SharpCompress.Test/Rar/RarReaderTests.cs | 26 +++++++++++++---- tests/SharpCompress.Test/ReaderTests.cs | 5 +++- .../Tar/TarArchiveAsyncTests.cs | 28 +++++++++++++++---- .../SharpCompress.Test/Zip/ZipArchiveTests.cs | 12 ++++++-- .../Zip/ZipMemoryArchiveWithCrcAsyncTests.cs | 4 ++- .../Zip/ZipReaderAsyncTests.cs | 26 ++++++++++++++--- .../SharpCompress.Test/Zip/ZipReaderTests.cs | 12 ++++++-- 27 files changed, 173 insertions(+), 49 deletions(-) diff --git a/src/SharpCompress/Archives/ArchiveFactory.cs b/src/SharpCompress/Archives/ArchiveFactory.cs index d94786e7..a9800acd 100644 --- a/src/SharpCompress/Archives/ArchiveFactory.cs +++ b/src/SharpCompress/Archives/ArchiveFactory.cs @@ -81,7 +81,10 @@ public static class ArchiveFactory return factory.OpenAsyncArchive(fileInfo, options, cancellationToken); } - public static IArchive OpenArchive(IEnumerable fileInfos, ReaderOptions? options = null) + public static IArchive OpenArchive( + IEnumerable fileInfos, + ReaderOptions? options = null + ) { fileInfos.NotNull(nameof(fileInfos)); var filesArray = fileInfos.ToArray(); diff --git a/src/SharpCompress/Factories/AceFactory.cs b/src/SharpCompress/Factories/AceFactory.cs index ce775277..6691f132 100644 --- a/src/SharpCompress/Factories/AceFactory.cs +++ b/src/SharpCompress/Factories/AceFactory.cs @@ -32,7 +32,7 @@ namespace SharpCompress.Factories public IReader OpenReader(Stream stream, ReaderOptions? options) => AceReader.OpenReader(stream, options); - public IAsyncReader OpenReaderAsync( + public IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Factories/ArcFactory.cs b/src/SharpCompress/Factories/ArcFactory.cs index 9ca2b5f7..9ed581ae 100644 --- a/src/SharpCompress/Factories/ArcFactory.cs +++ b/src/SharpCompress/Factories/ArcFactory.cs @@ -44,7 +44,7 @@ namespace SharpCompress.Factories public IReader OpenReader(Stream stream, ReaderOptions? options) => ArcReader.OpenReader(stream, options); - public IAsyncReader OpenReaderAsync( + public IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Factories/ArjFactory.cs b/src/SharpCompress/Factories/ArjFactory.cs index 577bca52..c58314d2 100644 --- a/src/SharpCompress/Factories/ArjFactory.cs +++ b/src/SharpCompress/Factories/ArjFactory.cs @@ -35,7 +35,7 @@ namespace SharpCompress.Factories public IReader OpenReader(Stream stream, ReaderOptions? options) => ArjReader.OpenReader(stream, options); - public IAsyncReader OpenReaderAsync( + public IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Factories/Factory.cs b/src/SharpCompress/Factories/Factory.cs index a77eec2e..cd6da6a7 100644 --- a/src/SharpCompress/Factories/Factory.cs +++ b/src/SharpCompress/Factories/Factory.cs @@ -133,7 +133,7 @@ public abstract class Factory : IFactory ) { ((IStreamStack)stream).StackSeek(pos); - return (true, readerFactory.OpenReaderAsync(stream, options, cancellationToken)); + return (true, readerFactory.OpenAsyncReader(stream, options, cancellationToken)); } } diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index 3ce26f86..6e92a90d 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -157,7 +157,7 @@ public class GZipFactory GZipReader.OpenReader(stream, options); /// - public IAsyncReader OpenReaderAsync( + public IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Factories/RarFactory.cs b/src/SharpCompress/Factories/RarFactory.cs index 4acbe4bc..40f9216c 100644 --- a/src/SharpCompress/Factories/RarFactory.cs +++ b/src/SharpCompress/Factories/RarFactory.cs @@ -116,7 +116,7 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade RarReader.OpenReader(stream, options); /// - public IAsyncReader OpenReaderAsync( + public IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index 346a51d8..3ba7687e 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -279,7 +279,7 @@ public class TarFactory TarReader.OpenReader(stream, options); /// - public IAsyncReader OpenReaderAsync( + public IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Factories/ZipFactory.cs b/src/SharpCompress/Factories/ZipFactory.cs index 97b47f17..ba244107 100644 --- a/src/SharpCompress/Factories/ZipFactory.cs +++ b/src/SharpCompress/Factories/ZipFactory.cs @@ -203,7 +203,7 @@ public class ZipFactory ZipReader.OpenReader(stream, options); /// - public IAsyncReader OpenReaderAsync( + public IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Readers/Ace/AceReader.Factory.cs b/src/SharpCompress/Readers/Ace/AceReader.Factory.cs index ad3cc3bd..653ff021 100644 --- a/src/SharpCompress/Readers/Ace/AceReader.Factory.cs +++ b/src/SharpCompress/Readers/Ace/AceReader.Factory.cs @@ -49,7 +49,5 @@ public partial class AceReader : IReaderOpenable fileInfo.NotNull(nameof(fileInfo)); return OpenReader(fileInfo.OpenRead(), readerOptions); } - - } #endif diff --git a/src/SharpCompress/Readers/IReaderFactory.cs b/src/SharpCompress/Readers/IReaderFactory.cs index ca38b2ba..52e7d35e 100644 --- a/src/SharpCompress/Readers/IReaderFactory.cs +++ b/src/SharpCompress/Readers/IReaderFactory.cs @@ -6,13 +6,21 @@ namespace SharpCompress.Readers; public interface IReaderFactory : Factories.IFactory { /// - /// Opens a Reader for Non-seeking usage + /// Opens a Reader for Non-seeking usage. /// /// /// /// IReader OpenReader(Stream stream, ReaderOptions? options); - IAsyncReader OpenReaderAsync( + + /// + /// Opens a Reader for Non-seeking usage asynchronously. + /// + /// + /// + /// + /// + IAsyncReader OpenAsyncReader( Stream stream, ReaderOptions? options, CancellationToken cancellationToken diff --git a/src/SharpCompress/Readers/IReaderOpenable.cs b/src/SharpCompress/Readers/IReaderOpenable.cs index 7d0e9d54..a421a49e 100644 --- a/src/SharpCompress/Readers/IReaderOpenable.cs +++ b/src/SharpCompress/Readers/IReaderOpenable.cs @@ -8,7 +8,10 @@ public interface IReaderOpenable { public static abstract IReader OpenReader(string filePath, ReaderOptions? readerOptions = null); - public static abstract IReader OpenReader(FileInfo fileInfo, ReaderOptions? readerOptions = null); + public static abstract IReader OpenReader( + FileInfo fileInfo, + ReaderOptions? readerOptions = null + ); public static abstract IReader OpenReader(Stream stream, ReaderOptions? readerOptions = null); diff --git a/src/SharpCompress/Readers/ReaderFactory.cs b/src/SharpCompress/Readers/ReaderFactory.cs index e104e67d..acd4e96b 100644 --- a/src/SharpCompress/Readers/ReaderFactory.cs +++ b/src/SharpCompress/Readers/ReaderFactory.cs @@ -139,7 +139,7 @@ public static class ReaderFactory if (testedFactory.IsArchive(bStream)) { ((IStreamStack)bStream).StackSeek(pos); - return readerFactory.OpenReaderAsync(bStream, options, cancellationToken); + return readerFactory.OpenAsyncReader(bStream, options, cancellationToken); } } ((IStreamStack)bStream).StackSeek(pos); @@ -155,7 +155,7 @@ public static class ReaderFactory if (factory is IReaderFactory readerFactory && factory.IsArchive(bStream)) { ((IStreamStack)bStream).StackSeek(pos); - return readerFactory.OpenReaderAsync(bStream, options, cancellationToken); + return readerFactory.OpenAsyncReader(bStream, options, cancellationToken); } } diff --git a/src/SharpCompress/Readers/Zip/ZipReader.cs b/src/SharpCompress/Readers/Zip/ZipReader.cs index f91266cf..6c3df181 100644 --- a/src/SharpCompress/Readers/Zip/ZipReader.cs +++ b/src/SharpCompress/Readers/Zip/ZipReader.cs @@ -51,7 +51,11 @@ public partial class ZipReader : AbstractReader return new ZipReader(stream, options ?? new ReaderOptions()); } - public static IReader OpenReader(Stream stream, ReaderOptions? options, IEnumerable entries) + public static IReader OpenReader( + Stream stream, + ReaderOptions? options, + IEnumerable entries + ) { stream.NotNull(nameof(stream)); return new ZipReader(stream, options ?? new ReaderOptions(), entries); diff --git a/src/SharpCompress/Writers/WriterFactory.cs b/src/SharpCompress/Writers/WriterFactory.cs index 8d36c14d..3d482541 100644 --- a/src/SharpCompress/Writers/WriterFactory.cs +++ b/src/SharpCompress/Writers/WriterFactory.cs @@ -36,7 +36,12 @@ public static class WriterFactory ) { filePath.NotNullOrEmpty(nameof(filePath)); - return OpenAsyncWriter(new FileInfo(filePath), archiveType, writerOptions, cancellationToken); + return OpenAsyncWriter( + new FileInfo(filePath), + archiveType, + writerOptions, + cancellationToken + ); } public static IAsyncWriter OpenAsyncWriter( @@ -55,7 +60,11 @@ public static class WriterFactory ); } - public static IWriter OpenWriter(Stream stream, ArchiveType archiveType, WriterOptions writerOptions) + public static IWriter OpenWriter( + Stream stream, + ArchiveType archiveType, + WriterOptions writerOptions + ) { var factory = Factories .Factory.Factories.OfType() diff --git a/tests/SharpCompress.Test/GZip/GZipWriterTests.cs b/tests/SharpCompress.Test/GZip/GZipWriterTests.cs index c8d9deef..4d73e6e1 100644 --- a/tests/SharpCompress.Test/GZip/GZipWriterTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipWriterTests.cs @@ -21,7 +21,9 @@ public class GZipWriterTests : WriterTests FileAccess.Write ) ) - using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.GZip, CompressionType.GZip)) + using ( + var writer = WriterFactory.OpenWriter(stream, ArchiveType.GZip, CompressionType.GZip) + ) { writer.Write("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar")); } @@ -56,7 +58,11 @@ public class GZipWriterTests : WriterTests Assert.Throws(() => { using Stream stream = File.OpenWrite(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); - using var writer = WriterFactory.OpenWriter(stream, ArchiveType.GZip, CompressionType.BZip2); + using var writer = WriterFactory.OpenWriter( + stream, + ArchiveType.GZip, + CompressionType.BZip2 + ); }); [Fact] diff --git a/tests/SharpCompress.Test/ProgressReportTests.cs b/tests/SharpCompress.Test/ProgressReportTests.cs index a9308197..805cbcf8 100644 --- a/tests/SharpCompress.Test/ProgressReportTests.cs +++ b/tests/SharpCompress.Test/ProgressReportTests.cs @@ -541,7 +541,10 @@ public class ProgressReportTests : TestBase var readerOptions = new ReaderOptions { Progress = progress }; await using ( - var reader = ReaderFactory.OpenAsyncReader(new AsyncOnlyStream(archiveStream), readerOptions) + var reader = ReaderFactory.OpenAsyncReader( + new AsyncOnlyStream(archiveStream), + readerOptions + ) ) { while (await reader.MoveToNextEntryAsync()) diff --git a/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs index c7cb76c4..949fb0cc 100644 --- a/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarArchiveAsyncTests.cs @@ -151,7 +151,9 @@ public class RarArchiveAsyncTests : ArchiveTests public async ValueTask Rar_Jpg_ArchiveStreamRead_Async() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg")); - using (var archive = RarArchive.OpenArchive(stream, new ReaderOptions { LookForHeader = true })) + using ( + var archive = RarArchive.OpenArchive(stream, new ReaderOptions { LookForHeader = true }) + ) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -588,7 +590,9 @@ public class RarArchiveAsyncTests : ArchiveTests private void DoRar_IsFirstVolume_False(string notFirstFilename) { - using var archive = RarArchive.OpenArchive(Path.Combine(TEST_ARCHIVES_PATH, notFirstFilename)); + using var archive = RarArchive.OpenArchive( + Path.Combine(TEST_ARCHIVES_PATH, notFirstFilename) + ); Assert.True(archive.IsMultipartVolume()); Assert.False(archive.IsFirstVolume()); } @@ -697,7 +701,10 @@ public class RarArchiveAsyncTests : ArchiveTests ) { var paths = testArchives.Select(x => Path.Combine(TEST_ARCHIVES_PATH, x)); - using var archive = ArchiveFactory.OpenArchive(paths.Select(a => new FileInfo(a)), readerOptions); + using var archive = ArchiveFactory.OpenArchive( + paths.Select(a => new FileInfo(a)), + readerOptions + ); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { await entry.WriteToDirectoryAsync( @@ -714,7 +721,10 @@ public class RarArchiveAsyncTests : ArchiveTests ) { var paths = testArchives.Select(x => Path.Combine(TEST_ARCHIVES_PATH, x)); - using var archive = ArchiveFactory.OpenArchive(paths.Select(f => new FileInfo(f)), readerOptions); + using var archive = ArchiveFactory.OpenArchive( + paths.Select(f => new FileInfo(f)), + readerOptions + ); foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { await entry.WriteToDirectoryAsync( diff --git a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs index 2117153d..c87f27be 100644 --- a/tests/SharpCompress.Test/Rar/RarArchiveTests.cs +++ b/tests/SharpCompress.Test/Rar/RarArchiveTests.cs @@ -148,7 +148,9 @@ public class RarArchiveTests : ArchiveTests public void Rar_Jpg_ArchiveStreamRead() { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg")); - using (var archive = RarArchive.OpenArchive(stream, new ReaderOptions { LookForHeader = true })) + using ( + var archive = RarArchive.OpenArchive(stream, new ReaderOptions { LookForHeader = true }) + ) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -598,7 +600,9 @@ public class RarArchiveTests : ArchiveTests private void DoRar_IsFirstVolume_False(string notFirstFilename) { - using var archive = RarArchive.OpenArchive(Path.Combine(TEST_ARCHIVES_PATH, notFirstFilename)); + using var archive = RarArchive.OpenArchive( + Path.Combine(TEST_ARCHIVES_PATH, notFirstFilename) + ); Assert.True(archive.IsMultipartVolume()); Assert.False(archive.IsFirstVolume()); } diff --git a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs index f32e0886..ce168746 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderAsyncTests.cs @@ -279,7 +279,10 @@ public class RarReaderAsyncTests : ReaderTests { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) using ( - IReader baseReader = RarReader.OpenReader(stream, new ReaderOptions { LookForHeader = true }) + IReader baseReader = RarReader.OpenReader( + stream, + new ReaderOptions { LookForHeader = true } + ) ) { IAsyncReader reader = (IAsyncReader)baseReader; diff --git a/tests/SharpCompress.Test/Rar/RarReaderTests.cs b/tests/SharpCompress.Test/Rar/RarReaderTests.cs index 866d1d42..fd0be593 100644 --- a/tests/SharpCompress.Test/Rar/RarReaderTests.cs +++ b/tests/SharpCompress.Test/Rar/RarReaderTests.cs @@ -226,7 +226,12 @@ public class RarReaderTests : ReaderTests using ( var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.Audio_program.rar")) ) - using (var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true })) + using ( + var reader = ReaderFactory.OpenReader( + stream, + new ReaderOptions { LookForHeader = true } + ) + ) { while (reader.MoveToNextEntry()) { @@ -247,7 +252,9 @@ public class RarReaderTests : ReaderTests public void Rar_Jpg_Reader() { using (var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Rar.jpeg.jpg"))) - using (var reader = RarReader.OpenReader(stream, new ReaderOptions { LookForHeader = true })) + using ( + var reader = RarReader.OpenReader(stream, new ReaderOptions { LookForHeader = true }) + ) { while (reader.MoveToNextEntry()) { @@ -282,7 +289,10 @@ public class RarReaderTests : ReaderTests private void DoRar_Solid_Skip_Reader(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true }); + using var reader = ReaderFactory.OpenReader( + stream, + new ReaderOptions { LookForHeader = true } + ); while (reader.MoveToNextEntry()) { if (reader.Entry.Key.NotNull().Contains("jpg")) @@ -305,7 +315,10 @@ public class RarReaderTests : ReaderTests private void DoRar_Reader_Skip(string filename) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, filename)); - using var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true }); + using var reader = ReaderFactory.OpenReader( + stream, + new ReaderOptions { LookForHeader = true } + ); while (reader.MoveToNextEntry()) { if (reader.Entry.Key.NotNull().Contains("jpg")) @@ -325,7 +338,10 @@ public class RarReaderTests : ReaderTests using var stream = File.OpenRead( Path.Combine(TEST_ARCHIVES_PATH, "Rar.encrypted_filesOnly.rar") ); - using var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true }); + using var reader = ReaderFactory.OpenReader( + stream, + new ReaderOptions { LookForHeader = true } + ); while (reader.MoveToNextEntry()) { // diff --git a/tests/SharpCompress.Test/ReaderTests.cs b/tests/SharpCompress.Test/ReaderTests.cs index 039c4e51..489d87f7 100644 --- a/tests/SharpCompress.Test/ReaderTests.cs +++ b/tests/SharpCompress.Test/ReaderTests.cs @@ -190,7 +190,10 @@ public abstract class ReaderTests : TestBase protected void ReadForBufferBoundaryCheck(string fileName, CompressionType compressionType) { using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, fileName)); - using var reader = ReaderFactory.OpenReader(stream, new ReaderOptions { LookForHeader = true }); + using var reader = ReaderFactory.OpenReader( + stream, + new ReaderOptions { LookForHeader = true } + ); while (reader.MoveToNextEntry()) { diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index 8de94f65..050db885 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -33,7 +33,13 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 1: create a tar file containing a file with the test name using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var writer = WriterFactory.OpenAsyncWriter(stream, ArchiveType.Tar, CompressionType.None)) + using ( + var writer = WriterFactory.OpenAsyncWriter( + stream, + ArchiveType.Tar, + CompressionType.None + ) + ) using (Stream inputStream = new MemoryStream()) { var sw = new StreamWriter(inputStream); @@ -47,7 +53,9 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); await using ( - var archive2 = TarArchive.OpenAsyncArchive(new AsyncOnlyStream(File.OpenRead(unmodified))) + var archive2 = TarArchive.OpenAsyncArchive( + new AsyncOnlyStream(File.OpenRead(unmodified)) + ) ) { Assert.Equal(1, await archive2.EntriesAsync.CountAsync()); @@ -82,7 +90,13 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 1: create a tar file containing a file with a long name using (Stream stream = File.OpenWrite(Path.Combine(SCRATCH2_FILES_PATH, archive))) - using (var writer = WriterFactory.OpenAsyncWriter(stream, ArchiveType.Tar, CompressionType.None)) + using ( + var writer = WriterFactory.OpenAsyncWriter( + stream, + ArchiveType.Tar, + CompressionType.None + ) + ) using (Stream inputStream = new MemoryStream()) { var sw = new StreamWriter(inputStream); @@ -96,7 +110,9 @@ public class TarArchiveAsyncTests : ArchiveTests // Step 2: check if the written tar file can be read correctly var unmodified = Path.Combine(SCRATCH2_FILES_PATH, archive); await using ( - var archive2 = TarArchive.OpenAsyncArchive(new AsyncOnlyStream(File.OpenRead(unmodified))) + var archive2 = TarArchive.OpenAsyncArchive( + new AsyncOnlyStream(File.OpenRead(unmodified)) + ) ) { Assert.Equal(1, await archive2.EntriesAsync.CountAsync()); @@ -216,7 +232,9 @@ public class TarArchiveAsyncTests : ArchiveTests var numberOfEntries = 0; - await using (var archiveFactory = TarArchive.OpenAsyncArchive(new AsyncOnlyStream(memoryStream))) + await using ( + var archiveFactory = TarArchive.OpenAsyncArchive(new AsyncOnlyStream(memoryStream)) + ) { await foreach (var entry in archiveFactory.EntriesAsync) { diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index 562fc09d..1c0998df 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -618,7 +618,13 @@ public class ZipArchiveTests : ArchiveTests { MemoryStream stream = new NonSeekableMemoryStream(); - using (var zipWriter = WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate)) + using ( + var zipWriter = WriterFactory.OpenWriter( + stream, + ArchiveType.Zip, + CompressionType.Deflate + ) + ) { zipWriter.Write("foo.txt", new MemoryStream(Array.Empty())); zipWriter.Write("foo2.txt", new MemoryStream(new byte[10])); @@ -834,7 +840,9 @@ public class ZipArchiveTests : ArchiveTests [Fact] public void Zip_FilePermissions() { - using var archive = ArchiveFactory.OpenArchive(Path.Combine(TEST_ARCHIVES_PATH, "Zip.644.zip")); + using var archive = ArchiveFactory.OpenArchive( + Path.Combine(TEST_ARCHIVES_PATH, "Zip.644.zip") + ); var firstEntry = archive.Entries.First(); const int S_IFREG = 0x8000; diff --git a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs index d2dd3b08..5824ebec 100644 --- a/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipMemoryArchiveWithCrcAsyncTests.cs @@ -131,7 +131,9 @@ public class ZipTypesLevelsWithCrcRatioAsyncTests : ArchiveTests CompressionLevel = compressionLevel, }; - using (var writer = WriterFactory.OpenAsyncWriter(zipStream, ArchiveType.Zip, writerOptions)) + using ( + var writer = WriterFactory.OpenAsyncWriter(zipStream, ArchiveType.Zip, writerOptions) + ) { await writer.WriteAsync( $"{compressionType}_level_{compressionLevel}_{sizeMb}MiB.txt", diff --git a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs index c7bdf399..3aec7ea9 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderAsyncTests.cs @@ -121,7 +121,12 @@ public class ZipReaderAsyncTests : ReaderTests using ( Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.bzip2.pkware.zip")) ) - using (IReader baseReader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) + using ( + IReader baseReader = ZipReader.OpenReader( + stream, + new ReaderOptions { Password = "test" } + ) + ) { IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) @@ -193,7 +198,10 @@ public class ZipReaderAsyncTests : ReaderTests ) ) using ( - IReader baseReader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" }) + IReader baseReader = ZipReader.OpenReader( + stream, + new ReaderOptions { Password = "test" } + ) ) { IAsyncReader reader = (IAsyncReader)baseReader; @@ -220,7 +228,12 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.WinzipAES.zip")) ) ) - using (IReader baseReader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) + using ( + IReader baseReader = ZipReader.OpenReader( + stream, + new ReaderOptions { Password = "test" } + ) + ) { IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) @@ -247,7 +260,12 @@ public class ZipReaderAsyncTests : ReaderTests File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "zipcrypto.zip")) ) ) - using (IReader baseReader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) + using ( + IReader baseReader = ZipReader.OpenReader( + stream, + new ReaderOptions { Password = "test" } + ) + ) { IAsyncReader reader = (IAsyncReader)baseReader; while (await reader.MoveToNextEntryAsync()) diff --git a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs index a5dfc4ca..c3b53e11 100644 --- a/tests/SharpCompress.Test/Zip/ZipReaderTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipReaderTests.cs @@ -179,7 +179,9 @@ public class ZipReaderTests : ReaderTests Path.Combine(TEST_ARCHIVES_PATH, "Zip.lzma.WinzipAES.zip") ) ) - using (var reader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" })) + using ( + var reader = ZipReader.OpenReader(stream, new ReaderOptions { Password = "test" }) + ) { while (reader.MoveToNextEntry()) { @@ -256,7 +258,13 @@ public class ZipReaderTests : ReaderTests using var memory = new MemoryStream(); Stream stream = new TestStream(memory, read: true, write: true, seek: false); - using (var zipWriter = WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate)) + using ( + var zipWriter = WriterFactory.OpenWriter( + stream, + ArchiveType.Zip, + CompressionType.Deflate + ) + ) { zipWriter.Write(expected[0].Item1, new MemoryStream(expected[0].Item2)); zipWriter.Write(expected[1].Item1, new MemoryStream(expected[1].Item2)); From 85b28dfe68cc25f8a54b3f39ca8ec4b4f8ad27a1 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 15 Jan 2026 12:20:35 +0000 Subject: [PATCH 33/36] more refactoring --- src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs | 8 +++++--- src/SharpCompress/Archives/IArchiveOpenable.cs | 1 + src/SharpCompress/Archives/IWritableArchiveOpenable.cs | 10 ++++++++++ src/SharpCompress/Archives/Tar/TarArchive.Factory.cs | 8 +++++--- src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs | 8 +++++--- tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs | 2 +- tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs | 2 +- 7 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 src/SharpCompress/Archives/IWritableArchiveOpenable.cs diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs index 16092968..40771a23 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs @@ -16,8 +16,8 @@ namespace SharpCompress.Archives.GZip; public partial class GZipArchive #if NET8_0_OR_GREATER - : IArchiveOpenable, - IMultiArchiveOpenable + : IWritableArchiveOpenable, + IMultiArchiveOpenable #endif { public static IWritableAsyncArchive OpenAsyncArchive( @@ -141,7 +141,9 @@ public partial class GZipArchive return (IWritableAsyncArchive)OpenArchive(fileInfos, readerOptions); } - public static GZipArchive CreateArchive() => new(); + public static IWritableArchive CreateArchive() => new GZipArchive(); + + public static IWritableAsyncArchive CreateAsyncArchive() => new GZipArchive(); public static bool IsGZipFile(string filePath) => IsGZipFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/IArchiveOpenable.cs b/src/SharpCompress/Archives/IArchiveOpenable.cs index d884aed0..e5ae52b3 100644 --- a/src/SharpCompress/Archives/IArchiveOpenable.cs +++ b/src/SharpCompress/Archives/IArchiveOpenable.cs @@ -36,4 +36,5 @@ public interface IArchiveOpenable CancellationToken cancellationToken = default ); } + #endif diff --git a/src/SharpCompress/Archives/IWritableArchiveOpenable.cs b/src/SharpCompress/Archives/IWritableArchiveOpenable.cs new file mode 100644 index 00000000..a21f4e4a --- /dev/null +++ b/src/SharpCompress/Archives/IWritableArchiveOpenable.cs @@ -0,0 +1,10 @@ +#if NET8_0_OR_GREATER +namespace SharpCompress.Archives; + +public interface IWritableArchiveOpenable + : IArchiveOpenable +{ + public static abstract IWritableArchive CreateArchive(); + public static abstract IWritableAsyncArchive CreateAsyncArchive(); +} +#endif \ No newline at end of file diff --git a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs index 5ce2d944..27a32da9 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs @@ -16,8 +16,8 @@ namespace SharpCompress.Archives.Tar; public partial class TarArchive #if NET8_0_OR_GREATER - : IArchiveOpenable, - IMultiArchiveOpenable + : IWritableArchiveOpenable, + IMultiArchiveOpenable #endif { public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) @@ -165,5 +165,7 @@ public partial class TarArchive return false; } - public static TarArchive CreateArchive() => new(); + public static IWritableArchive CreateArchive() => new TarArchive(); + + public static IWritableAsyncArchive CreateAsyncArchive() => new TarArchive(); } diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs index 85110f12..18b48463 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs @@ -14,8 +14,8 @@ namespace SharpCompress.Archives.Zip; public partial class ZipArchive #if NET8_0_OR_GREATER - : IArchiveOpenable, - IMultiArchiveOpenable + : IWritableArchiveOpenable, + IMultiArchiveOpenable #endif { public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) @@ -266,7 +266,9 @@ public partial class ZipArchive } } - public static ZipArchive CreateArchive() => new(); + public static IWritableArchive CreateArchive() => new ZipArchive(); + + public static IWritableAsyncArchive CreateAsyncArchive() => new ZipArchive(); public static async ValueTask IsZipMultiAsync( Stream stream, diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index 050db885..a4dfa7a2 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -137,7 +137,7 @@ public class TarArchiveAsyncTests : ArchiveTests var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - using (var archive = TarArchive.CreateArchive()) + using (var archive = TarArchive.CreateAsyncArchive()) { archive.AddAllFromDirectory(ORIGINAL_FILES_PATH); var twopt = new TarWriterOptions(CompressionType.None, true); diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs index d16bec4f..961cfccf 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs @@ -167,7 +167,7 @@ public class ZipArchiveAsyncTests : ArchiveTests var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Zip.deflate.noEmptyDirs.zip"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using (ZipArchive archive = ZipArchive.CreateArchive()) + using (var archive = ZipArchive.CreateAsyncArchive()) { archive.DeflateCompressionLevel = CompressionLevel.BestSpeed; archive.AddAllFromDirectory(ORIGINAL_FILES_PATH); From 7fbd751d274edfdbdf1f0398150259ff17296974 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 15 Jan 2026 13:00:05 +0000 Subject: [PATCH 34/36] change tests to work --- src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs | 2 +- .../Archives/IWritableArchiveCommonExtensions.cs | 7 +++++++ src/SharpCompress/Archives/IWritableArchiveOpenable.cs | 2 +- src/SharpCompress/Archives/Tar/TarArchive.Factory.cs | 2 +- src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs | 2 +- tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs | 2 +- .../Zip/Zip64VersionConsistencyTests.cs | 10 +++++----- tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs | 2 +- 8 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs index 40771a23..4d413e63 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs @@ -17,7 +17,7 @@ namespace SharpCompress.Archives.GZip; public partial class GZipArchive #if NET8_0_OR_GREATER : IWritableArchiveOpenable, - IMultiArchiveOpenable + IMultiArchiveOpenable #endif { public static IWritableAsyncArchive OpenAsyncArchive( diff --git a/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs b/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs index 77a7f499..7f276ece 100644 --- a/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs +++ b/src/SharpCompress/Archives/IWritableArchiveCommonExtensions.cs @@ -34,6 +34,13 @@ public static class IWritableArchiveCommonExtensions public IArchiveEntry AddEntry(string key, string file) => writableArchive.AddEntry(key, new FileInfo(file)); + public IArchiveEntry AddEntry( + string key, + Stream source, + long size = 0, + DateTime? modified = null + ) => writableArchive.AddEntry(key, source, false, size, modified); + public IArchiveEntry AddEntry(string key, FileInfo fileInfo) { if (!fileInfo.Exists) diff --git a/src/SharpCompress/Archives/IWritableArchiveOpenable.cs b/src/SharpCompress/Archives/IWritableArchiveOpenable.cs index a21f4e4a..b0b761a2 100644 --- a/src/SharpCompress/Archives/IWritableArchiveOpenable.cs +++ b/src/SharpCompress/Archives/IWritableArchiveOpenable.cs @@ -7,4 +7,4 @@ public interface IWritableArchiveOpenable public static abstract IWritableArchive CreateArchive(); public static abstract IWritableAsyncArchive CreateAsyncArchive(); } -#endif \ No newline at end of file +#endif diff --git a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs index 27a32da9..f6e03f4f 100644 --- a/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs +++ b/src/SharpCompress/Archives/Tar/TarArchive.Factory.cs @@ -17,7 +17,7 @@ namespace SharpCompress.Archives.Tar; public partial class TarArchive #if NET8_0_OR_GREATER : IWritableArchiveOpenable, - IMultiArchiveOpenable + IMultiArchiveOpenable #endif { public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) diff --git a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs index 18b48463..bb51552e 100644 --- a/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/Zip/ZipArchive.Factory.cs @@ -15,7 +15,7 @@ namespace SharpCompress.Archives.Zip; public partial class ZipArchive #if NET8_0_OR_GREATER : IWritableArchiveOpenable, - IMultiArchiveOpenable + IMultiArchiveOpenable #endif { public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) diff --git a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs index a4dfa7a2..0e6ef3e6 100644 --- a/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Tar/TarArchiveAsyncTests.cs @@ -137,7 +137,7 @@ public class TarArchiveAsyncTests : ArchiveTests var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Tar.tar"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Tar.noEmptyDirs.tar"); - using (var archive = TarArchive.CreateAsyncArchive()) + await using (var archive = TarArchive.CreateAsyncArchive()) { archive.AddAllFromDirectory(ORIGINAL_FILES_PATH); var twopt = new TarWriterOptions(CompressionType.None, true); diff --git a/tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs b/tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs index b3a03342..fa26a76e 100644 --- a/tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs +++ b/tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs @@ -37,7 +37,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = true, }; - ZipArchive zipArchive = ZipArchive.CreateArchive(); + var zipArchive = ZipArchive.CreateArchive(); zipArchive.AddEntry("empty", new MemoryStream()); zipArchive.SaveTo(filename, writerOptions); @@ -141,7 +141,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = false, }; - ZipArchive zipArchive = ZipArchive.CreateArchive(); + var zipArchive = ZipArchive.CreateArchive(); zipArchive.AddEntry("empty", new MemoryStream()); zipArchive.SaveTo(filename, writerOptions); @@ -192,7 +192,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = false, }; - ZipArchive zipArchive = ZipArchive.CreateArchive(); + var zipArchive = ZipArchive.CreateArchive(); var data = new byte[100]; new Random(42).NextBytes(data); zipArchive.AddEntry("test.bin", new MemoryStream(data)); @@ -245,7 +245,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = false, }; - ZipArchive zipArchive = ZipArchive.CreateArchive(); + var zipArchive = ZipArchive.CreateArchive(); var data = new byte[100]; new Random(42).NextBytes(data); zipArchive.AddEntry("test.bin", new MemoryStream(data)); @@ -298,7 +298,7 @@ public class Zip64VersionConsistencyTests : WriterTests UseZip64 = true, }; - ZipArchive zipArchive = ZipArchive.CreateArchive(); + var zipArchive = ZipArchive.CreateArchive(); for (int i = 0; i < 5; i++) { var data = new byte[100]; diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs index 961cfccf..1d114f78 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveAsyncTests.cs @@ -167,7 +167,7 @@ public class ZipArchiveAsyncTests : ArchiveTests var scratchPath = Path.Combine(SCRATCH_FILES_PATH, "Zip.deflate.noEmptyDirs.zip"); var unmodified = Path.Combine(TEST_ARCHIVES_PATH, "Zip.deflate.noEmptyDirs.zip"); - using (var archive = ZipArchive.CreateAsyncArchive()) + await using (var archive = (ZipArchive)ZipArchive.CreateAsyncArchive()) { archive.DeflateCompressionLevel = CompressionLevel.BestSpeed; archive.AddAllFromDirectory(ORIGINAL_FILES_PATH); From bf7416753a0e42bab2d7080f632b5be26382a45f Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 15 Jan 2026 13:04:09 +0000 Subject: [PATCH 35/36] update docs --- docs/API.md | 38 +++++++++++++++++++------------------- docs/USAGE.md | 6 +++--- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/docs/API.md b/docs/API.md index ccf3ab9b..209378fd 100644 --- a/docs/API.md +++ b/docs/API.md @@ -40,17 +40,17 @@ using (var writer = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType. } // Specific writer -using (var archive = ZipArchive.Create()) -using (var archive = TarArchive.Create()) -using (var archive = GZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) +using (var archive = TarArchive.CreateArchive()) +using (var archive = GZipArchive.CreateArchive()) // With options -var options = new WriterOptions(CompressionType.Deflate) -{ +var options = new WriterOptions(CompressionType.Deflate) +{ CompressionLevel = 9, LeaveStreamOpen = false }; -using (var archive = ZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) { archive.SaveTo("output.zip", options); } @@ -91,7 +91,7 @@ using (var archive = ZipArchive.Open("file.zip")) } // Async extraction (requires IAsyncArchive) -using (var asyncArchive = await ZipArchive.OpenAsync("file.zip")) +using (var asyncArchive = await ZipArchive.OpenAsyncArchive("file.zip")) { await asyncArchive.WriteToDirectoryAsync( @"C:\output", @@ -122,7 +122,7 @@ foreach (var entry in archive.Entries) ### Creating Archives ```csharp -using (var archive = ZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) { // Add file archive.AddEntry("file.txt", @"C:\source\file.txt"); @@ -172,9 +172,9 @@ using (var reader = ReaderFactory.Open(stream)) } } -// Async variants (use OpenAsync to get IAsyncReader) +// Async variants (use OpenAsyncReader to get IAsyncReader) using (var stream = File.OpenRead("file.zip")) -using (var reader = await ReaderFactory.OpenAsync(stream)) +using (var reader = await ReaderFactory.OpenAsyncReader(stream)) { while (await reader.MoveToNextEntryAsync()) { @@ -363,7 +363,7 @@ cts.CancelAfter(TimeSpan.FromMinutes(5)); try { - using (var archive = await ZipArchive.OpenAsync("archive.zip")) + using (var archive = await ZipArchive.OpenAsyncArchive("archive.zip")) { await archive.WriteToDirectoryAsync( @"C:\output", @@ -381,23 +381,23 @@ catch (OperationCanceledException) ### Create with Custom Compression ```csharp -using (var archive = ZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) { archive.AddAllFromDirectory(@"D:\source"); // Fastest - archive.SaveTo("fast.zip", new WriterOptions(CompressionType.Deflate) - { - CompressionLevel = 1 + archive.SaveTo("fast.zip", new WriterOptions(CompressionType.Deflate) + { + CompressionLevel = 1 }); // Balanced (default) archive.SaveTo("normal.zip", CompressionType.Deflate); // Best compression - archive.SaveTo("best.zip", new WriterOptions(CompressionType.Deflate) - { - CompressionLevel = 9 + archive.SaveTo("best.zip", new WriterOptions(CompressionType.Deflate) + { + CompressionLevel = 9 }); } ``` @@ -406,7 +406,7 @@ using (var archive = ZipArchive.Create()) ```csharp using (var outputStream = new MemoryStream()) -using (var archive = ZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) { // Add content from memory using (var contentStream = new MemoryStream(Encoding.UTF8.GetBytes("Hello"))) diff --git a/docs/USAGE.md b/docs/USAGE.md index 8e636cab..a4351062 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -48,7 +48,7 @@ Also, look over the tests for more thorough [examples](https://github.com/adamha ### Create Zip Archive from multiple files ```C# -using(var archive = ZipArchive.Create()) +using(var archive = ZipArchive.CreateArchive()) { archive.AddEntry("file01.txt", "C:\\file01.txt"); archive.AddEntry("file02.txt", "C:\\file02.txt"); @@ -61,7 +61,7 @@ using(var archive = ZipArchive.Create()) ### Create Zip Archive from all files in a directory to a file ```C# -using (var archive = ZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) { archive.AddAllFromDirectory("D:\\temp"); archive.SaveTo("C:\\temp.zip", CompressionType.Deflate); @@ -72,7 +72,7 @@ using (var archive = ZipArchive.Create()) ```C# var memoryStream = new MemoryStream(); -using (var archive = ZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) { archive.AddAllFromDirectory("D:\\temp"); archive.SaveTo(memoryStream, new WriterOptions(CompressionType.Deflate) From b41296194f9daa342b5ca23f43a311ceb53d234e Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 15 Jan 2026 13:29:57 +0000 Subject: [PATCH 36/36] more updates to docs --- docs/API.md | 42 +++++++++++++++++++++--------------------- docs/ARCHITECTURE.md | 22 +++++++++++----------- docs/ENCODING.md | 26 +++++++++++++------------- docs/PERFORMANCE.md | 32 ++++++++++++++++---------------- docs/USAGE.md | 28 ++++++++++++++-------------- 5 files changed, 75 insertions(+), 75 deletions(-) diff --git a/docs/API.md b/docs/API.md index 209378fd..eb8f007a 100644 --- a/docs/API.md +++ b/docs/API.md @@ -8,17 +8,17 @@ Quick reference for commonly used SharpCompress APIs. ```csharp // Auto-detect format -using (var reader = ReaderFactory.Open(stream)) +using (var reader = ReaderFactory.OpenReader(stream)) { // Works with Zip, Tar, GZip, Rar, 7Zip, etc. } // Specific format - Archive API -using (var archive = ZipArchive.Open("file.zip")) -using (var archive = TarArchive.Open("file.tar")) -using (var archive = RarArchive.Open("file.rar")) -using (var archive = SevenZipArchive.Open("file.7z")) -using (var archive = GZipArchive.Open("file.gz")) +using (var archive = ZipArchive.OpenArchive("file.zip")) +using (var archive = TarArchive.OpenArchive("file.tar")) +using (var archive = RarArchive.OpenArchive("file.rar")) +using (var archive = SevenZipArchive.OpenArchive("file.7z")) +using (var archive = GZipArchive.OpenArchive("file.gz")) // With options var options = new ReaderOptions @@ -27,14 +27,14 @@ var options = new ReaderOptions LeaveStreamOpen = true, ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) } }; -using (var archive = ZipArchive.Open("encrypted.zip", options)) +using (var archive = ZipArchive.OpenArchive("encrypted.zip", options)) ``` ### Creating Archives ```csharp // Writer Factory -using (var writer = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate)) +using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate)) { // Write entries } @@ -63,7 +63,7 @@ using (var archive = ZipArchive.CreateArchive()) ### Reading/Extracting ```csharp -using (var archive = ZipArchive.Open("file.zip")) +using (var archive = ZipArchive.OpenArchive("file.zip")) { // Get all entries IEnumerable entries = archive.Entries; @@ -151,7 +151,7 @@ using (var archive = ZipArchive.CreateArchive()) ```csharp using (var stream = File.OpenRead("file.zip")) -using (var reader = ReaderFactory.Open(stream)) +using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -201,7 +201,7 @@ using (var reader = await ReaderFactory.OpenAsyncReader(stream)) ```csharp using (var stream = File.Create("output.zip")) -using (var writer = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate)) +using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate)) { // Write single file using (var fileStream = File.OpenRead("source.txt")) @@ -239,7 +239,7 @@ var options = new ReaderOptions Default = Encoding.GetEncoding(932) } }; -using (var archive = ZipArchive.Open("file.zip", options)) +using (var archive = ZipArchive.OpenArchive("file.zip", options)) { // ... } @@ -290,8 +290,8 @@ ArchiveType.Ace // For Tar archives with compression // Use WriterFactory to create compressed tar archives -using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.GZip)) // Tar.GZip -using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, CompressionType.BZip2)) // Tar.BZip2 +using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Tar, CompressionType.GZip)) // Tar.GZip +using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Tar, CompressionType.BZip2)) // Tar.BZip2 ``` ### Archive Types @@ -349,7 +349,7 @@ var progress = new Progress(report => }); var options = new ReaderOptions { Progress = progress }; -using (var archive = ZipArchive.Open("archive.zip", options)) +using (var archive = ZipArchive.OpenArchive("archive.zip", options)) { archive.WriteToDirectory(@"C:\output"); } @@ -425,7 +425,7 @@ using (var archive = ZipArchive.CreateArchive()) ### Extract Specific Files ```csharp -using (var archive = ZipArchive.Open("archive.zip")) +using (var archive = ZipArchive.OpenArchive("archive.zip")) { var filesToExtract = new[] { "file1.txt", "file2.txt" }; @@ -439,7 +439,7 @@ using (var archive = ZipArchive.Open("archive.zip")) ### List Archive Contents ```csharp -using (var archive = ZipArchive.Open("archive.zip")) +using (var archive = ZipArchive.OpenArchive("archive.zip")) { foreach (var entry in archive.Entries) { @@ -459,7 +459,7 @@ using (var archive = ZipArchive.Open("archive.zip")) ```csharp var stream = File.OpenRead("archive.zip"); -var archive = ZipArchive.Open(stream); +var archive = ZipArchive.OpenArchive(stream); archive.WriteToDirectory(@"C:\output"); // stream not disposed - leaked resource ``` @@ -468,7 +468,7 @@ archive.WriteToDirectory(@"C:\output"); ```csharp using (var stream = File.OpenRead("archive.zip")) -using (var archive = ZipArchive.Open(stream)) +using (var archive = ZipArchive.OpenArchive(stream)) { archive.WriteToDirectory(@"C:\output"); } @@ -479,7 +479,7 @@ using (var archive = ZipArchive.Open(stream)) ```csharp // Loading entire archive then iterating -using (var archive = ZipArchive.Open("large.zip")) +using (var archive = ZipArchive.OpenArchive("large.zip")) { var entries = archive.Entries.ToList(); // Loads all in memory foreach (var e in entries) @@ -494,7 +494,7 @@ using (var archive = ZipArchive.Open("large.zip")) ```csharp // Streaming iteration using (var stream = File.OpenRead("large.zip")) -using (var reader = ReaderFactory.Open(stream)) +using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index 0ba6f581..2a3bb985 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -76,7 +76,7 @@ Factory classes for auto-detecting archive format and creating appropriate reade - Format-specific: `ZipFactory.cs`, `TarFactory.cs`, `RarFactory.cs`, etc. **How It Works:** -1. `ReaderFactory.Open(stream)` probes stream signatures +1. `ReaderFactory.OpenReader(stream)` probes stream signatures 2. Identifies format by magic bytes 3. Creates appropriate reader instance 4. Returns generic `IReader` interface @@ -142,7 +142,7 @@ Stream wrappers and utilities. **Example:** ```csharp // User calls factory -using (var reader = ReaderFactory.Open(stream)) // Returns IReader +using (var reader = ReaderFactory.OpenReader(stream)) // Returns IReader { while (reader.MoveToNextEntry()) { @@ -175,7 +175,7 @@ CompressionType.LZMA // LZMA CompressionType.PPMd // PPMd // Writer uses strategy pattern -var archive = ZipArchive.Create(); +var archive = ZipArchive.CreateArchive(); archive.SaveTo("output.zip", CompressionType.Deflate); // Use Deflate archive.SaveTo("output.bz2", CompressionType.BZip2); // Use BZip2 ``` @@ -248,7 +248,7 @@ foreach (var entry in entries) } // Reader API - provides iterator -IReader reader = ReaderFactory.Open(stream); +IReader reader = ReaderFactory.OpenReader(stream); while (reader.MoveToNextEntry()) { // Forward-only iteration - one entry at a time @@ -381,7 +381,7 @@ public class NewFormatArchive : AbstractArchive private NewFormatHeader _header; private List _entries; - public static NewFormatArchive Open(Stream stream) + public static NewFormatArchive OpenArchive(Stream stream) { var archive = new NewFormatArchive(); archive._header = NewFormatHeader.Read(stream); @@ -442,8 +442,8 @@ public class NewFormatFactory : Factory, IArchiveFactory, IReaderFactory public static NewFormatFactory Instance { get; } = new(); - public IArchive CreateArchive(Stream stream) - => NewFormatArchive.Open(stream); + public IArchive CreateArchive(Stream stream) + => NewFormatArchive.OpenArchive(stream); public IReader CreateReader(Stream stream, ReaderOptions options) => new NewFormatReader(stream) { Options = options }; @@ -481,7 +481,7 @@ public class NewFormatTests : TestBase public void NewFormat_Extracts_Successfully() { var archivePath = Path.Combine(TEST_ARCHIVES_PATH, "archive.newformat"); - using (var archive = NewFormatArchive.Open(archivePath)) + using (var archive = NewFormatArchive.OpenArchive(archivePath)) { archive.WriteToDirectory(SCRATCH_FILES_PATH); // Assert extraction @@ -561,7 +561,7 @@ public class CustomStream : Stream ```csharp // Correct: Nested using blocks using (var fileStream = File.OpenRead("archive.zip")) -using (var archive = ZipArchive.Open(fileStream)) +using (var archive = ZipArchive.OpenArchive(fileStream)) { archive.WriteToDirectory(@"C:\output"); } @@ -570,7 +570,7 @@ using (var archive = ZipArchive.Open(fileStream)) // Correct: Using with options var options = new ReaderOptions { LeaveStreamOpen = true }; var stream = File.OpenRead("archive.zip"); -using (var archive = ZipArchive.Open(stream, options)) +using (var archive = ZipArchive.OpenArchive(stream, options)) { archive.WriteToDirectory(@"C:\output"); } @@ -641,7 +641,7 @@ public void Archive_Extraction_Works() var testArchive = Path.Combine(TEST_ARCHIVES_PATH, "test.zip"); // Act - using (var archive = ZipArchive.Open(testArchive)) + using (var archive = ZipArchive.OpenArchive(testArchive)) { archive.WriteToDirectory(SCRATCH_FILES_PATH); } diff --git a/docs/ENCODING.md b/docs/ENCODING.md index 1737ace9..8200e756 100644 --- a/docs/ENCODING.md +++ b/docs/ENCODING.md @@ -27,7 +27,7 @@ var options = new ReaderOptions } }; -using (var archive = ZipArchive.Open("japanese.zip", options)) +using (var archive = ZipArchive.OpenArchive("japanese.zip", options)) { foreach (var entry in archive.Entries) { @@ -51,7 +51,7 @@ var options = new ReaderOptions { ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) } }; -using (var archive = ZipArchive.Open("file.zip", options)) +using (var archive = ZipArchive.OpenArchive("file.zip", options)) { // Use archive with correct encoding } @@ -64,7 +64,7 @@ var options = new ReaderOptions ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) } }; using (var stream = File.OpenRead("file.zip")) -using (var reader = ReaderFactory.Open(stream, options)) +using (var reader = ReaderFactory.OpenReader(stream, options)) { while (reader.MoveToNextEntry()) { @@ -89,7 +89,7 @@ var options = new ReaderOptions Default = Encoding.GetEncoding(932) } }; -using (var archive = ZipArchive.Open("japanese.zip", options)) +using (var archive = ZipArchive.OpenArchive("japanese.zip", options)) { // Correctly decodes Japanese filenames } @@ -266,7 +266,7 @@ SharpCompress attempts to auto-detect encoding, but this isn't always reliable: ```csharp // Auto-detection (default) -using (var archive = ZipArchive.Open("file.zip")) // Uses UTF8 by default +using (var archive = ZipArchive.OpenArchive("file.zip")) // Uses UTF8 by default { // May show corrupted characters if archive uses different encoding } @@ -276,7 +276,7 @@ var options = new ReaderOptions { ArchiveEncoding = new ArchiveEncoding { Default = Encoding.GetEncoding(932) } }; -using (var archive = ZipArchive.Open("file.zip", options)) +using (var archive = ZipArchive.OpenArchive("file.zip", options)) { // Correct characters displayed } @@ -324,7 +324,7 @@ var options = new ReaderOptions } }; -using (var archive = ZipArchive.Open("mixed.zip", options)) +using (var archive = ZipArchive.OpenArchive("mixed.zip", options)) { foreach (var entry in archive.Entries) { @@ -388,7 +388,7 @@ var options = new ReaderOptions } }; -using (var archive = ZipArchive.Open("japanese_files.zip", options)) +using (var archive = ZipArchive.OpenArchive("japanese_files.zip", options)) { archive.WriteToDirectory(@"C:\output", new ExtractionOptions { @@ -410,7 +410,7 @@ var options = new ReaderOptions } }; -using (var archive = ZipArchive.Open("french_files.zip", options)) +using (var archive = ZipArchive.OpenArchive("french_files.zip", options)) { archive.WriteToDirectory(@"C:\output"); } @@ -428,7 +428,7 @@ var options = new ReaderOptions } }; -using (var archive = ZipArchive.Open("chinese_files.zip", options)) +using (var archive = ZipArchive.OpenArchive("chinese_files.zip", options)) { archive.WriteToDirectory(@"C:\output"); } @@ -445,7 +445,7 @@ var options = new ReaderOptions } }; -using (var archive = ZipArchive.Open("russian_files.zip", options)) +using (var archive = ZipArchive.OpenArchive("russian_files.zip", options)) { archive.WriteToDirectory(@"C:\output"); } @@ -463,7 +463,7 @@ var options = new ReaderOptions }; using (var stream = File.OpenRead("japanese.zip")) -using (var reader = ReaderFactory.Open(stream, options)) +using (var reader = ReaderFactory.OpenReader(stream, options)) { while (reader.MoveToNextEntry()) { @@ -484,7 +484,7 @@ When creating archives, SharpCompress uses UTF8 by default (recommended): ```csharp // Create with UTF8 (default, recommended) -using (var archive = ZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) { archive.AddAllFromDirectory(@"D:\my_files"); archive.SaveTo("output.zip", CompressionType.Deflate); diff --git a/docs/PERFORMANCE.md b/docs/PERFORMANCE.md index 03c12553..51b2f920 100644 --- a/docs/PERFORMANCE.md +++ b/docs/PERFORMANCE.md @@ -24,7 +24,7 @@ Choose the right API based on your use case: // - You need random access to entries // - Stream is seekable (file, MemoryStream) -using (var archive = ZipArchive.Open("archive.zip")) +using (var archive = ZipArchive.OpenArchive("archive.zip")) { // Random access - all entries available var specific = archive.Entries.FirstOrDefault(e => e.Key == "file.txt"); @@ -51,7 +51,7 @@ using (var archive = ZipArchive.Open("archive.zip")) // - Forward-only processing is acceptable using (var stream = File.OpenRead("large.zip")) -using (var reader = ReaderFactory.Open(stream)) +using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -129,7 +129,7 @@ For processing archives from downloads or pipes: ```csharp // Download stream (non-seekable) using (var httpStream = await httpClient.GetStreamAsync(url)) -using (var reader = ReaderFactory.Open(httpStream)) +using (var reader = ReaderFactory.OpenReader(httpStream)) { // Process entries as they arrive while (reader.MoveToNextEntry()) @@ -159,14 +159,14 @@ Choose based on your constraints: ```csharp // Download then extract (requires disk space) var archivePath = await DownloadFile(url, @"C:\temp\archive.zip"); -using (var archive = ZipArchive.Open(archivePath)) +using (var archive = ZipArchive.OpenArchive(archivePath)) { archive.WriteToDirectory(@"C:\output"); } // Stream during download (on-the-fly extraction) using (var httpStream = await httpClient.GetStreamAsync(url)) -using (var reader = ReaderFactory.Open(httpStream)) +using (var reader = ReaderFactory.OpenReader(httpStream)) { while (reader.MoveToNextEntry()) { @@ -198,7 +198,7 @@ Extracting File3 requires decompressing File1 and File2 first. **Random Extraction (Slow):** ```csharp -using (var archive = RarArchive.Open("solid.rar")) +using (var archive = RarArchive.OpenArchive("solid.rar")) { foreach (var entry in archive.Entries) { @@ -210,7 +210,7 @@ using (var archive = RarArchive.Open("solid.rar")) **Sequential Extraction (Fast):** ```csharp -using (var archive = RarArchive.Open("solid.rar")) +using (var archive = RarArchive.OpenArchive("solid.rar")) { // Method 1: Use WriteToDirectory (recommended) archive.WriteToDirectory(@"C:\output", new ExtractionOptions @@ -256,7 +256,7 @@ using (var archive = RarArchive.Open("solid.rar")) // Level 9 = Slowest, best compression // Write with different compression levels -using (var archive = ZipArchive.Create()) +using (var archive = ZipArchive.CreateArchive()) { archive.AddAllFromDirectory(@"D:\data"); @@ -293,7 +293,7 @@ using (var archive = ZipArchive.Create()) // Smaller block size = lower memory, faster // Larger block size = better compression, slower -using (var archive = TarArchive.Create()) +using (var archive = TarArchive.CreateArchive()) { archive.AddAllFromDirectory(@"D:\data"); @@ -313,7 +313,7 @@ LZMA compression is very powerful but memory-intensive: // - Better compression: larger dictionary // Preset via CompressionType -using (var archive = TarArchive.Create()) +using (var archive = TarArchive.CreateArchive()) { archive.AddAllFromDirectory(@"D:\data"); archive.SaveTo("archive.tar.xz", CompressionType.LZMA); // Default settings @@ -333,7 +333,7 @@ Async is beneficial when: ```csharp // Async extraction (non-blocking) -using (var archive = ZipArchive.Open("archive.zip")) +using (var archive = ZipArchive.OpenArchive("archive.zip")) { await archive.WriteToDirectoryAsync( @"C:\output", @@ -353,7 +353,7 @@ Async doesn't improve performance for: ```csharp // Sync extraction (simpler, same performance on fast I/O) -using (var archive = ZipArchive.Open("archive.zip")) +using (var archive = ZipArchive.OpenArchive("archive.zip")) { archive.WriteToDirectory( @"C:\output", @@ -373,7 +373,7 @@ cts.CancelAfter(TimeSpan.FromMinutes(5)); try { - using (var archive = ZipArchive.Open("archive.zip")) + using (var archive = ZipArchive.OpenArchive("archive.zip")) { await archive.WriteToDirectoryAsync( @"C:\output", @@ -408,14 +408,14 @@ catch (OperationCanceledException) // ✗ Slow - opens each archive separately foreach (var file in files) { - using (var archive = ZipArchive.Open("archive.zip")) + using (var archive = ZipArchive.OpenArchive("archive.zip")) { archive.WriteToDirectory(@"C:\output"); } } // ✓ Better - process multiple entries at once -using (var archive = ZipArchive.Open("archive.zip")) +using (var archive = ZipArchive.OpenArchive("archive.zip")) { archive.WriteToDirectory(@"C:\output"); } @@ -425,7 +425,7 @@ using (var archive = ZipArchive.Open("archive.zip")) ```csharp var sw = Stopwatch.StartNew(); -using (var archive = ZipArchive.Open("large.zip")) +using (var archive = ZipArchive.OpenArchive("large.zip")) { archive.WriteToDirectory(@"C:\output"); } diff --git a/docs/USAGE.md b/docs/USAGE.md index a4351062..52d690a3 100644 --- a/docs/USAGE.md +++ b/docs/USAGE.md @@ -90,7 +90,7 @@ Note: Extracting a solid rar or 7z file needs to be done in sequential order to `ExtractAllEntries` is primarily intended for solid archives (like solid Rar) or 7Zip archives, where sequential extraction provides the best performance. For general/simple extraction with any supported archive type, use `archive.WriteToDirectory()` instead. ```C# -using (var archive = RarArchive.Open("Test.rar")) +using (var archive = RarArchive.OpenArchive("Test.rar")) { // Simple extraction with RarArchive; this WriteToDirectory pattern works for all archive types archive.WriteToDirectory(@"D:\temp", new ExtractionOptions() @@ -104,7 +104,7 @@ using (var archive = RarArchive.Open("Test.rar")) ### Iterate over all files from a Rar file using RarArchive ```C# -using (var archive = RarArchive.Open("Test.rar")) +using (var archive = RarArchive.OpenArchive("Test.rar")) { foreach (var entry in archive.Entries.Where(entry => !entry.IsDirectory)) { @@ -126,7 +126,7 @@ var progress = new Progress(report => Console.WriteLine($"Extracting {report.EntryPath}: {report.PercentComplete}%"); }); -using (var archive = RarArchive.Open("archive.rar", new ReaderOptions { Progress = progress })) // Must be solid Rar or 7Zip +using (var archive = RarArchive.OpenArchive("archive.rar", new ReaderOptions { Progress = progress })) // Must be solid Rar or 7Zip { archive.WriteToDirectory(@"D:\output", new ExtractionOptions() { @@ -140,7 +140,7 @@ using (var archive = RarArchive.Open("archive.rar", new ReaderOptions { Progress ```C# using (Stream stream = File.OpenRead("Tar.tar.bz2")) -using (var reader = ReaderFactory.Open(stream)) +using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -161,7 +161,7 @@ using (var reader = ReaderFactory.Open(stream)) ```C# using (Stream stream = File.OpenRead("Tar.tar.bz2")) -using (var reader = ReaderFactory.Open(stream)) +using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -180,7 +180,7 @@ using (var reader = ReaderFactory.Open(stream)) ```C# using (Stream stream = File.OpenWrite("C:\\temp.tgz")) -using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, new WriterOptions(CompressionType.GZip) +using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Tar, new WriterOptions(CompressionType.GZip) { LeaveOpenStream = true })) @@ -199,7 +199,7 @@ opts.ArchiveEncoding.CustomDecoder = (data, x, y) => { return encoding.GetString(data); }; -var tr = SharpCompress.Archives.Zip.ZipArchive.Open("test.zip", opts); +var tr = SharpCompress.Archives.Zip.ZipArchive.OpenArchive("test.zip", opts); foreach(var entry in tr.Entries) { Console.WriteLine($"{entry.Key}"); @@ -213,7 +213,7 @@ foreach(var entry in tr.Entries) **Extract single entry asynchronously:** ```C# using (Stream stream = File.OpenRead("archive.zip")) -using (var reader = ReaderFactory.Open(stream)) +using (var reader = ReaderFactory.OpenReader(stream)) { while (reader.MoveToNextEntry()) { @@ -234,7 +234,7 @@ using (var reader = ReaderFactory.Open(stream)) **Extract all entries asynchronously:** ```C# using (Stream stream = File.OpenRead("archive.tar.gz")) -using (var reader = ReaderFactory.Open(stream)) +using (var reader = ReaderFactory.OpenReader(stream)) { await reader.WriteAllToDirectoryAsync( @"D:\temp", @@ -250,7 +250,7 @@ using (var reader = ReaderFactory.Open(stream)) **Open and process entry stream asynchronously:** ```C# -using (var archive = ZipArchive.Open("archive.zip")) +using (var archive = ZipArchive.OpenArchive("archive.zip")) { foreach (var entry in archive.Entries.Where(e => !e.IsDirectory)) { @@ -268,7 +268,7 @@ using (var archive = ZipArchive.Open("archive.zip")) **Write single file asynchronously:** ```C# using (Stream archiveStream = File.OpenWrite("output.zip")) -using (var writer = WriterFactory.Open(archiveStream, ArchiveType.Zip, CompressionType.Deflate)) +using (var writer = WriterFactory.OpenWriter(archiveStream, ArchiveType.Zip, CompressionType.Deflate)) { using (Stream fileStream = File.OpenRead("input.txt")) { @@ -280,7 +280,7 @@ using (var writer = WriterFactory.Open(archiveStream, ArchiveType.Zip, Compressi **Write entire directory asynchronously:** ```C# using (Stream stream = File.OpenWrite("backup.tar.gz")) -using (var writer = WriterFactory.Open(stream, ArchiveType.Tar, new WriterOptions(CompressionType.GZip))) +using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Tar, new WriterOptions(CompressionType.GZip))) { await writer.WriteAllAsync( @"D:\files", @@ -299,7 +299,7 @@ var cts = new CancellationTokenSource(); cts.CancelAfter(TimeSpan.FromMinutes(5)); using (Stream stream = File.OpenWrite("archive.zip")) -using (var writer = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType.Deflate)) +using (var writer = WriterFactory.OpenWriter(stream, ArchiveType.Zip, CompressionType.Deflate)) { try { @@ -316,7 +316,7 @@ using (var writer = WriterFactory.Open(stream, ArchiveType.Zip, CompressionType. **Extract from archive asynchronously:** ```C# -using (var archive = ZipArchive.Open("archive.zip")) +using (var archive = ZipArchive.OpenArchive("archive.zip")) { // Simple async extraction - works for all archive types await archive.WriteToDirectoryAsync(