From e89fb211cec3d10b39b5b01159a2610f08032e37 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Sun, 25 Jan 2026 15:03:51 +0000 Subject: [PATCH] gzipwriter async --- .../Writers/AbstractWriter.Async.cs | 20 +++--------- .../Writers/GZip/GZipWriter.Async.cs | 31 +++++++++++++++++++ 2 files changed, 35 insertions(+), 16 deletions(-) create mode 100644 src/SharpCompress/Writers/GZip/GZipWriter.Async.cs diff --git a/src/SharpCompress/Writers/AbstractWriter.Async.cs b/src/SharpCompress/Writers/AbstractWriter.Async.cs index e2960ab9..c0d94de7 100644 --- a/src/SharpCompress/Writers/AbstractWriter.Async.cs +++ b/src/SharpCompress/Writers/AbstractWriter.Async.cs @@ -8,30 +8,18 @@ namespace SharpCompress.Writers; public abstract partial class AbstractWriter { - public virtual async ValueTask WriteAsync( + public abstract ValueTask WriteAsync( string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default - ) - { - // Default implementation calls synchronous version - // Derived classes should override for true async behavior - Write(filename, source, modificationTime); - await Task.CompletedTask.ConfigureAwait(false); - } + ); - public virtual async ValueTask WriteDirectoryAsync( + public abstract ValueTask WriteDirectoryAsync( string directoryName, DateTime? modificationTime, CancellationToken cancellationToken = default - ) - { - // Default implementation calls synchronous version - // Derived classes should override for true async behavior - WriteDirectory(directoryName, modificationTime); - await Task.CompletedTask.ConfigureAwait(false); - } + ); public ValueTask DisposeAsync() { diff --git a/src/SharpCompress/Writers/GZip/GZipWriter.Async.cs b/src/SharpCompress/Writers/GZip/GZipWriter.Async.cs new file mode 100644 index 00000000..5471deaa --- /dev/null +++ b/src/SharpCompress/Writers/GZip/GZipWriter.Async.cs @@ -0,0 +1,31 @@ +using System; +using System.IO; +using System.Threading; +using System.Threading.Tasks; +using SharpCompress.Compressors.Deflate; + +namespace SharpCompress.Writers.GZip; + +public partial class GZipWriter +{ + public override async ValueTask WriteAsync(string filename, Stream source, DateTime? modificationTime, CancellationToken cancellationToken = default) + { + if (_wroteToStream) + { + throw new ArgumentException("Can only write a single stream to a GZip file."); + } + var stream = (GZipStream)OutputStream; + stream.FileName = filename; + stream.LastModified = modificationTime; + var progressStream = WrapWithProgress(source, filename); +#if LEGACY_DOTNET + await progressStream.CopyToAsync(stream); + #else + await progressStream.CopyToAsync(stream, cancellationToken); +#endif + _wroteToStream = true; + } + + public override ValueTask WriteDirectoryAsync(string directoryName, DateTime? modificationTime, CancellationToken cancellationToken = default) => + throw new NotSupportedException("GZip archives do not support directory entries."); +}