From 399572d9d6d11d8ea200f9dd1f29b2bca8f2f9c1 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Tue, 10 Feb 2026 09:03:10 +0000 Subject: [PATCH] use specific writer options for writing archives --- .../Archives/AbstractWritableArchive.Async.cs | 14 +++++-- .../Archives/AbstractWritableArchive.cs | 35 +++++++--------- .../Archives/GZip/GZipArchive.Async.cs | 5 +-- .../Archives/GZip/GZipArchive.Factory.cs | 42 +++++++++---------- .../Archives/GZip/GZipArchive.cs | 8 ++-- .../Archives/IWritableArchive.cs | 10 +++-- .../Archives/IWritableArchiveOpenable.cs | 11 +++-- 7 files changed, 62 insertions(+), 63 deletions(-) diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.Async.cs b/src/SharpCompress/Archives/AbstractWritableArchive.Async.cs index 8c74004c..f48b5344 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.Async.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.Async.cs @@ -6,13 +6,13 @@ using System.Threading; using System.Threading.Tasks; using SharpCompress.Common; using SharpCompress.Common.Options; -using SharpCompress.Writers; namespace SharpCompress.Archives; -public abstract partial class AbstractWritableArchive +public abstract partial class AbstractWritableArchive where TEntry : IArchiveEntry where TVolume : IVolume + where TOptions : IWriterOptions { // Async property moved from main file private IAsyncEnumerable OldEntriesAsync => @@ -112,7 +112,7 @@ public abstract partial class AbstractWritableArchive public async ValueTask SaveToAsync( Stream stream, - IWriterOptions options, + TOptions options, CancellationToken cancellationToken = default ) { @@ -121,4 +121,12 @@ public abstract partial class AbstractWritableArchive await SaveToAsync(stream, options, OldEntriesAsync, newEntries, cancellationToken) .ConfigureAwait(false); } + + protected abstract ValueTask SaveToAsync( + Stream stream, + TOptions options, + IAsyncEnumerable oldEntries, + IEnumerable newEntries, + CancellationToken cancellationToken = default + ); } diff --git a/src/SharpCompress/Archives/AbstractWritableArchive.cs b/src/SharpCompress/Archives/AbstractWritableArchive.cs index 6943c5bd..ae58f6f0 100644 --- a/src/SharpCompress/Archives/AbstractWritableArchive.cs +++ b/src/SharpCompress/Archives/AbstractWritableArchive.cs @@ -11,18 +11,19 @@ using SharpCompress.Writers; namespace SharpCompress.Archives; -public abstract partial class AbstractWritableArchive +public abstract partial class AbstractWritableArchive : AbstractArchive, - IWritableArchive, - IWritableAsyncArchive + IWritableArchive, + IWritableAsyncArchive where TEntry : IArchiveEntry where TVolume : IVolume + where TOptions : IWriterOptions { private class RebuildPauseDisposable : IDisposable { - private readonly AbstractWritableArchive archive; + private readonly AbstractWritableArchive archive; - public RebuildPauseDisposable(AbstractWritableArchive archive) + public RebuildPauseDisposable(AbstractWritableArchive archive) { this.archive = archive; archive.pauseRebuilding = true; @@ -85,12 +86,12 @@ public abstract partial class AbstractWritableArchive } } - void IWritableArchive.RemoveEntry(IArchiveEntry entry) => RemoveEntry((TEntry)entry); + void IWritableArchive.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 IWritableArchive.AddEntry( string key, Stream source, bool closeStream, @@ -98,7 +99,7 @@ public abstract partial class AbstractWritableArchive DateTime? modified ) => AddEntry(key, source, closeStream, size, modified); - IArchiveEntry IWritableArchive.AddDirectoryEntry(string key, DateTime? modified) => + IArchiveEntry IWritableArchive.AddDirectoryEntry(string key, DateTime? modified) => AddDirectoryEntry(key, modified); public TEntry AddEntry( @@ -141,10 +142,10 @@ public abstract partial class AbstractWritableArchive return false; } - ValueTask IWritableAsyncArchive.RemoveEntryAsync(IArchiveEntry entry) => + ValueTask IWritableAsyncArchive.RemoveEntryAsync(IArchiveEntry entry) => RemoveEntryAsync((TEntry)entry); - async ValueTask IWritableAsyncArchive.AddEntryAsync( + async ValueTask IWritableAsyncArchive.AddEntryAsync( string key, Stream source, bool closeStream, @@ -153,7 +154,7 @@ public abstract partial class AbstractWritableArchive CancellationToken cancellationToken ) => await AddEntryAsync(key, source, closeStream, size, modified, cancellationToken); - async ValueTask IWritableAsyncArchive.AddDirectoryEntryAsync( + async ValueTask IWritableAsyncArchive.AddDirectoryEntryAsync( string key, DateTime? modified, CancellationToken cancellationToken @@ -175,7 +176,7 @@ public abstract partial class AbstractWritableArchive return entry; } - public void SaveTo(Stream stream, IWriterOptions options) + public void SaveTo(Stream stream, TOptions options) { //reset streams of new entries newEntries.Cast().ForEach(x => x.Stream.Seek(0, SeekOrigin.Begin)); @@ -211,19 +212,11 @@ public abstract partial class AbstractWritableArchive protected abstract void SaveTo( Stream stream, - IWriterOptions options, + TOptions options, IEnumerable oldEntries, IEnumerable newEntries ); - protected abstract ValueTask SaveToAsync( - Stream stream, - IWriterOptions options, - IAsyncEnumerable oldEntries, - IEnumerable newEntries, - CancellationToken cancellationToken = default - ); - public override void Dispose() { base.Dispose(); diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Async.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Async.cs index cc3e87d8..542db118 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.Async.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Async.cs @@ -6,7 +6,6 @@ using System.Threading.Tasks; using SharpCompress.Common; using SharpCompress.Common.GZip; using SharpCompress.Common.Options; -using SharpCompress.IO; using SharpCompress.Readers; using SharpCompress.Readers.GZip; using SharpCompress.Writers; @@ -25,13 +24,13 @@ public partial class GZipArchive ) { using var stream = fileInfo.Open(FileMode.Create, FileAccess.Write); - await SaveToAsync(stream, new WriterOptions(CompressionType.GZip), cancellationToken) + await SaveToAsync(stream, new GZipWriterOptions(CompressionType.GZip), cancellationToken) .ConfigureAwait(false); } protected override async ValueTask SaveToAsync( Stream stream, - IWriterOptions options, + GZipWriterOptions options, IAsyncEnumerable oldEntries, IEnumerable newEntries, CancellationToken cancellationToken = default diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs index e793cd9b..7b0892c8 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.Factory.cs @@ -5,23 +5,19 @@ 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 #if NET8_0_OR_GREATER - : IWritableArchiveOpenable, - IMultiArchiveOpenable + : IWritableArchiveOpenable, + IMultiArchiveOpenable, IWritableAsyncArchive> #endif { - public static IWritableAsyncArchive OpenAsyncArchive( + public static IWritableAsyncArchive OpenAsyncArchive( string path, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default @@ -29,19 +25,19 @@ public partial class GZipArchive { cancellationToken.ThrowIfCancellationRequested(); path.NotNullOrEmpty(nameof(path)); - return (IWritableAsyncArchive)OpenArchive( + return (IWritableAsyncArchive)OpenArchive( new FileInfo(path), readerOptions ?? new ReaderOptions() ); } - public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive(string filePath, ReaderOptions? readerOptions = null) { filePath.NotNullOrEmpty(nameof(filePath)); return OpenArchive(new FileInfo(filePath), readerOptions ?? new ReaderOptions()); } - public static IWritableArchive OpenArchive( + public static IWritableArchive OpenArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null ) @@ -56,7 +52,7 @@ public partial class GZipArchive ); } - public static IWritableArchive OpenArchive( + public static IWritableArchive OpenArchive( IEnumerable fileInfos, ReaderOptions? readerOptions = null ) @@ -72,7 +68,7 @@ public partial class GZipArchive ); } - public static IWritableArchive OpenArchive( + public static IWritableArchive OpenArchive( IEnumerable streams, ReaderOptions? readerOptions = null ) @@ -88,7 +84,7 @@ public partial class GZipArchive ); } - public static IWritableArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) + public static IWritableArchive OpenArchive(Stream stream, ReaderOptions? readerOptions = null) { stream.NotNull(nameof(stream)); @@ -102,49 +98,49 @@ public partial class GZipArchive ); } - public static IWritableAsyncArchive OpenAsyncArchive( + public static IWritableAsyncArchive OpenAsyncArchive( Stream stream, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)OpenArchive(stream, readerOptions); + return (IWritableAsyncArchive)OpenArchive(stream, readerOptions); } - public static IWritableAsyncArchive OpenAsyncArchive( + public static IWritableAsyncArchive OpenAsyncArchive( FileInfo fileInfo, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)OpenArchive(fileInfo, readerOptions); + return (IWritableAsyncArchive)OpenArchive(fileInfo, readerOptions); } - public static IWritableAsyncArchive OpenAsyncArchive( + public static IWritableAsyncArchive OpenAsyncArchive( IReadOnlyList streams, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)OpenArchive(streams, readerOptions); + return (IWritableAsyncArchive)OpenArchive(streams, readerOptions); } - public static IWritableAsyncArchive OpenAsyncArchive( + public static IWritableAsyncArchive OpenAsyncArchive( IReadOnlyList fileInfos, ReaderOptions? readerOptions = null, CancellationToken cancellationToken = default ) { cancellationToken.ThrowIfCancellationRequested(); - return (IWritableAsyncArchive)OpenArchive(fileInfos, readerOptions); + return (IWritableAsyncArchive)OpenArchive(fileInfos, readerOptions); } - public static IWritableArchive CreateArchive() => new GZipArchive(); + public static IWritableArchive CreateArchive() => new GZipArchive(); - public static IWritableAsyncArchive CreateAsyncArchive() => new GZipArchive(); + public static IWritableAsyncArchive CreateAsyncArchive() => new GZipArchive(); public static bool IsGZipFile(string filePath) => IsGZipFile(new FileInfo(filePath)); diff --git a/src/SharpCompress/Archives/GZip/GZipArchive.cs b/src/SharpCompress/Archives/GZip/GZipArchive.cs index 4ed4be43..6cc9bb54 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchive.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchive.cs @@ -2,8 +2,6 @@ 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.Common.Options; @@ -15,7 +13,7 @@ using SharpCompress.Writers.GZip; namespace SharpCompress.Archives.GZip; -public partial class GZipArchive : AbstractWritableArchive +public partial class GZipArchive : AbstractWritableArchive { private GZipArchive(SourceStream sourceStream) : base(ArchiveType.GZip, sourceStream) { } @@ -34,7 +32,7 @@ public partial class GZipArchive : AbstractWritableArchive oldEntries, IEnumerable newEntries ) diff --git a/src/SharpCompress/Archives/IWritableArchive.cs b/src/SharpCompress/Archives/IWritableArchive.cs index cb5c0209..f85453d3 100644 --- a/src/SharpCompress/Archives/IWritableArchive.cs +++ b/src/SharpCompress/Archives/IWritableArchive.cs @@ -16,7 +16,8 @@ public interface IWritableArchiveCommon IDisposable PauseEntryRebuilding(); } -public interface IWritableArchive : IArchive, IWritableArchiveCommon +public interface IWritableArchive : IArchive, IWritableArchiveCommon +where TOptions : IWriterOptions { IArchiveEntry AddEntry( string key, @@ -31,7 +32,7 @@ public interface IWritableArchive : IArchive, IWritableArchiveCommon /// /// Saves the archive to the specified stream using the given writer options. /// - void SaveTo(Stream stream, IWriterOptions options); + void SaveTo(Stream stream, TOptions options); /// /// Removes the specified entry from the archive. @@ -39,14 +40,15 @@ public interface IWritableArchive : IArchive, IWritableArchiveCommon void RemoveEntry(IArchiveEntry entry); } -public interface IWritableAsyncArchive : IAsyncArchive, IWritableArchiveCommon +public interface IWritableAsyncArchive : IAsyncArchive, IWritableArchiveCommon + where TOptions : IWriterOptions { /// /// Asynchronously saves the archive to the specified stream using the given writer options. /// ValueTask SaveToAsync( Stream stream, - IWriterOptions options, + TOptions options, CancellationToken cancellationToken = default ); diff --git a/src/SharpCompress/Archives/IWritableArchiveOpenable.cs b/src/SharpCompress/Archives/IWritableArchiveOpenable.cs index b0b761a2..f523e1d2 100644 --- a/src/SharpCompress/Archives/IWritableArchiveOpenable.cs +++ b/src/SharpCompress/Archives/IWritableArchiveOpenable.cs @@ -1,10 +1,13 @@ +using SharpCompress.Common.Options; + #if NET8_0_OR_GREATER namespace SharpCompress.Archives; -public interface IWritableArchiveOpenable - : IArchiveOpenable +public interface IWritableArchiveOpenable + : IArchiveOpenable, IWritableAsyncArchive> + where TOptions : IWriterOptions { - public static abstract IWritableArchive CreateArchive(); - public static abstract IWritableAsyncArchive CreateAsyncArchive(); + public static abstract IWritableArchive CreateArchive(); + public static abstract IWritableAsyncArchive CreateAsyncArchive(); } #endif