From f364b68e094fa44969848696d7d387c2e9bbc9e6 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sun, 25 Jan 2026 15:23:10 +0000 Subject: [PATCH] remove more buffer --- src/SharpCompress/Factories/AceFactory.cs | 8 +--- src/SharpCompress/Factories/ArcFactory.cs | 7 +--- src/SharpCompress/Factories/ArjFactory.cs | 8 +--- src/SharpCompress/Factories/Factory.cs | 9 +---- src/SharpCompress/Factories/GZipFactory.cs | 8 +--- src/SharpCompress/Factories/IFactory.cs | 8 +--- src/SharpCompress/Factories/RarFactory.cs | 8 +--- .../Factories/SevenZipFactory.cs | 8 +--- src/SharpCompress/Factories/TarFactory.cs | 7 +--- .../Factories/ZStandardFactory.cs | 8 +--- src/SharpCompress/Factories/ZipFactory.cs | 33 ++++++++++------ .../Writers/Zip/ZipWriter.Async.cs | 38 ++++++++++++++++++- 12 files changed, 75 insertions(+), 75 deletions(-) diff --git a/src/SharpCompress/Factories/AceFactory.cs b/src/SharpCompress/Factories/AceFactory.cs index 29885b16..aa0ed79e 100644 --- a/src/SharpCompress/Factories/AceFactory.cs +++ b/src/SharpCompress/Factories/AceFactory.cs @@ -23,16 +23,12 @@ namespace SharpCompress.Factories yield return "ace"; } - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) => AceHeader.IsArchive(stream); + public override bool IsArchive(Stream stream, string? password = null) => + AceHeader.IsArchive(stream); public override ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) => AceHeader.IsArchiveAsync(stream, cancellationToken); diff --git a/src/SharpCompress/Factories/ArcFactory.cs b/src/SharpCompress/Factories/ArcFactory.cs index 7cc84417..593fb03e 100644 --- a/src/SharpCompress/Factories/ArcFactory.cs +++ b/src/SharpCompress/Factories/ArcFactory.cs @@ -25,11 +25,7 @@ namespace SharpCompress.Factories yield return "arc"; } - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) + public override bool IsArchive(Stream stream, string? password = null) { //You may have to use some(paranoid) checks to ensure that you actually are //processing an ARC file, since other archivers also adopted the idea of putting @@ -65,7 +61,6 @@ namespace SharpCompress.Factories public override async ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) { diff --git a/src/SharpCompress/Factories/ArjFactory.cs b/src/SharpCompress/Factories/ArjFactory.cs index fd41e2fd..1377407b 100644 --- a/src/SharpCompress/Factories/ArjFactory.cs +++ b/src/SharpCompress/Factories/ArjFactory.cs @@ -23,16 +23,12 @@ namespace SharpCompress.Factories yield return "arj"; } - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) => ArjHeader.IsArchive(stream); + public override bool IsArchive(Stream stream, string? password = null) => + ArjHeader.IsArchive(stream); public override ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) => ArjHeader.IsArchiveAsync(stream, cancellationToken); diff --git a/src/SharpCompress/Factories/Factory.cs b/src/SharpCompress/Factories/Factory.cs index 157178e8..075863e3 100644 --- a/src/SharpCompress/Factories/Factory.cs +++ b/src/SharpCompress/Factories/Factory.cs @@ -53,16 +53,11 @@ public abstract class Factory : IFactory public abstract IEnumerable GetSupportedExtensions(); /// - public abstract bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ); + public abstract bool IsArchive(Stream stream, string? password = null); public abstract ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ); @@ -91,7 +86,7 @@ public abstract class Factory : IFactory { long pos = ((IStreamStack)stream).GetPosition(); - if (IsArchive(stream, options.Password, options.BufferSize)) + if (IsArchive(stream, options.Password)) { ((IStreamStack)stream).StackSeek(pos); reader = readerFactory.OpenReader(stream, options); diff --git a/src/SharpCompress/Factories/GZipFactory.cs b/src/SharpCompress/Factories/GZipFactory.cs index 7c571038..fe90c391 100644 --- a/src/SharpCompress/Factories/GZipFactory.cs +++ b/src/SharpCompress/Factories/GZipFactory.cs @@ -42,17 +42,13 @@ public class GZipFactory } /// - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) => GZipArchive.IsGZipFile(stream); + public override bool IsArchive(Stream stream, string? password = null) => + GZipArchive.IsGZipFile(stream); /// public override ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) => GZipArchive.IsGZipFileAsync(stream, cancellationToken); diff --git a/src/SharpCompress/Factories/IFactory.cs b/src/SharpCompress/Factories/IFactory.cs index a9dd4f5a..2f0b1ab0 100644 --- a/src/SharpCompress/Factories/IFactory.cs +++ b/src/SharpCompress/Factories/IFactory.cs @@ -38,23 +38,17 @@ public interface IFactory /// /// A stream, pointing to the beginning of the archive. /// optional password - bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ); + bool IsArchive(Stream stream, string? password = null); /// /// Returns true if the stream represents an archive of the format defined by this type asynchronously. /// /// A stream, pointing to the beginning of the archive. /// optional password - /// buffer size for reading /// cancellation token ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ); diff --git a/src/SharpCompress/Factories/RarFactory.cs b/src/SharpCompress/Factories/RarFactory.cs index 4142bc0b..11fe6cf8 100644 --- a/src/SharpCompress/Factories/RarFactory.cs +++ b/src/SharpCompress/Factories/RarFactory.cs @@ -31,17 +31,13 @@ public class RarFactory : Factory, IArchiveFactory, IMultiArchiveFactory, IReade } /// - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) => RarArchive.IsRarFile(stream); + public override bool IsArchive(Stream stream, string? password = null) => + RarArchive.IsRarFile(stream); /// public override ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) => RarArchive.IsRarFileAsync(stream, cancellationToken: cancellationToken); diff --git a/src/SharpCompress/Factories/SevenZipFactory.cs b/src/SharpCompress/Factories/SevenZipFactory.cs index 45149d7d..a371cce1 100644 --- a/src/SharpCompress/Factories/SevenZipFactory.cs +++ b/src/SharpCompress/Factories/SevenZipFactory.cs @@ -30,17 +30,13 @@ public class SevenZipFactory : Factory, IArchiveFactory, IMultiArchiveFactory } /// - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) => SevenZipArchive.IsSevenZipFile(stream); + public override bool IsArchive(Stream stream, string? password = null) => + SevenZipArchive.IsSevenZipFile(stream); /// public override ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) => SevenZipArchive.IsSevenZipFileAsync(stream, cancellationToken); diff --git a/src/SharpCompress/Factories/TarFactory.cs b/src/SharpCompress/Factories/TarFactory.cs index e9102282..0170a3f7 100644 --- a/src/SharpCompress/Factories/TarFactory.cs +++ b/src/SharpCompress/Factories/TarFactory.cs @@ -45,11 +45,7 @@ public class TarFactory } /// - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) + public override bool IsArchive(Stream stream, string? password = null) { var rewindableStream = new SharpCompressStream(stream); long pos = rewindableStream.GetPosition(); @@ -75,7 +71,6 @@ public class TarFactory public override async ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) { diff --git a/src/SharpCompress/Factories/ZStandardFactory.cs b/src/SharpCompress/Factories/ZStandardFactory.cs index e8918d70..e45b2454 100644 --- a/src/SharpCompress/Factories/ZStandardFactory.cs +++ b/src/SharpCompress/Factories/ZStandardFactory.cs @@ -21,16 +21,12 @@ internal class ZStandardFactory : Factory yield return "zstd"; } - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = 65536 - ) => ZStandardStream.IsZStandard(stream); + public override bool IsArchive(Stream stream, string? password = null) => + ZStandardStream.IsZStandard(stream); public override ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) => ZStandardStream.IsZStandardAsync(stream, cancellationToken); } diff --git a/src/SharpCompress/Factories/ZipFactory.cs b/src/SharpCompress/Factories/ZipFactory.cs index 9e2a928e..bc75b4ad 100644 --- a/src/SharpCompress/Factories/ZipFactory.cs +++ b/src/SharpCompress/Factories/ZipFactory.cs @@ -41,11 +41,7 @@ public class ZipFactory } /// - public override bool IsArchive( - Stream stream, - string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize - ) + public override bool IsArchive(Stream stream, string? password = null) { var startPosition = stream.CanSeek ? stream.Position : -1; @@ -53,10 +49,10 @@ public class ZipFactory if (stream is not SharpCompressStream) // wrap to provide buffer bef { - stream = new SharpCompressStream(stream, bufferSize: bufferSize); + stream = new SharpCompressStream(stream, bufferSize: ReaderOptions.DefaultBufferSize); } - if (ZipArchive.IsZipFile(stream, password, bufferSize)) + if (ZipArchive.IsZipFile(stream, password, ReaderOptions.DefaultBufferSize)) { return true; } @@ -71,7 +67,7 @@ public class ZipFactory stream.Position = startPosition; //test the zip (last) file of a multipart zip - if (ZipArchive.IsZipMulti(stream, password, bufferSize)) + if (ZipArchive.IsZipMulti(stream, password, ReaderOptions.DefaultBufferSize)) { return true; } @@ -85,7 +81,6 @@ public class ZipFactory public override async ValueTask IsArchiveAsync( Stream stream, string? password = null, - int bufferSize = ReaderOptions.DefaultBufferSize, CancellationToken cancellationToken = default ) { @@ -96,10 +91,17 @@ public class ZipFactory if (stream is not SharpCompressStream) // wrap to provide buffer bef { - stream = new SharpCompressStream(stream, bufferSize: bufferSize); + stream = new SharpCompressStream(stream, bufferSize: ReaderOptions.DefaultBufferSize); } - if (await ZipArchive.IsZipFileAsync(stream, password, bufferSize, cancellationToken)) + if ( + await ZipArchive.IsZipFileAsync( + stream, + password, + ReaderOptions.DefaultBufferSize, + cancellationToken + ) + ) { return true; } @@ -114,7 +116,14 @@ public class ZipFactory stream.Position = startPosition; //test the zip (last) file of a multipart zip - if (await ZipArchive.IsZipMultiAsync(stream, password, bufferSize, cancellationToken)) + if ( + await ZipArchive.IsZipMultiAsync( + stream, + password, + ReaderOptions.DefaultBufferSize, + cancellationToken + ) + ) { return true; } diff --git a/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs b/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs index 192d7e03..53c1af45 100644 --- a/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs +++ b/src/SharpCompress/Writers/Zip/ZipWriter.Async.cs @@ -10,6 +10,42 @@ namespace SharpCompress.Writers.Zip; public partial class ZipWriter { + /// + /// Asynchronously writes an entry to the ZIP archive. + /// + public override async ValueTask WriteAsync( + string entryPath, + Stream source, + DateTime? modificationTime, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + await WriteAsync( + entryPath, + source, + new ZipWriterEntryOptions { ModificationDateTime = modificationTime }, + cancellationToken + ) + .ConfigureAwait(false); + } + + /// + /// Asynchronously writes an entry to the ZIP archive with specified options. + /// + public async ValueTask WriteAsync( + string entryPath, + Stream source, + ZipWriterEntryOptions zipWriterEntryOptions, + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + using var output = WriteToStream(entryPath, zipWriterEntryOptions); + var progressStream = WrapWithProgress(source, entryPath); + await progressStream.CopyToAsync(output, 81920, cancellationToken).ConfigureAwait(false); + } + /// /// Asynchronously writes a directory entry to the ZIP archive. /// Uses synchronous implementation for directory entries as they are lightweight. @@ -20,7 +56,7 @@ public partial class ZipWriter CancellationToken cancellationToken = default ) { - // Synchronous implementation is sufficient for directory entries + cancellationToken.ThrowIfCancellationRequested(); WriteDirectory(directoryName, modificationTime); await Task.CompletedTask.ConfigureAwait(false); }