gzipwriter async

This commit is contained in:
Adam Hathcock
2026-01-25 15:03:51 +00:00
parent 414cad1241
commit e89fb211ce
2 changed files with 35 additions and 16 deletions

View File

@@ -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()
{

View File

@@ -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.");
}