Fix GZip write async

This commit is contained in:
Adam Hathcock
2026-05-13 11:33:21 +01:00
parent 6e59c7d7bb
commit b9ddd4294e
3 changed files with 46 additions and 1 deletions

View File

@@ -24,7 +24,7 @@ public class GZipArchiveEntry : GZipEntry, IArchiveEntry
return Parts.Single().GetCompressedStream().NotNull();
}
public async ValueTask<Stream> OpenEntryStreamAsync(
public virtual async ValueTask<Stream> OpenEntryStreamAsync(
CancellationToken cancellationToken = default
)
{

View File

@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.IO;
@@ -61,6 +63,14 @@ internal sealed class GZipWritableArchiveEntry : GZipArchiveEntry, IWritableArch
return SharpCompressStream.CreateNonDisposing(stream);
}
public override ValueTask<Stream> OpenEntryStreamAsync(
CancellationToken cancellationToken = default
)
{
cancellationToken.ThrowIfCancellationRequested();
return new(OpenEntryStream());
}
internal override void Close()
{
if (closeStream)

View File

@@ -6,6 +6,8 @@ using SharpCompress.Archives;
using SharpCompress.Archives.GZip;
using SharpCompress.Archives.Tar;
using SharpCompress.Common;
using SharpCompress.Compressors.Deflate;
using SharpCompress.Readers;
using SharpCompress.Test.Mocks;
using SharpCompress.Writers.GZip;
using Xunit;
@@ -181,4 +183,37 @@ public class GZipArchiveAsyncTests : ArchiveTests
await using var archive = await GZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream));
Assert.Equal(archive.Type, ArchiveType.GZip);
}
[Fact]
public async ValueTask GZip_Create_New_Async()
{
var scratchPath = Path.Combine(SCRATCH_FILES_PATH, Guid.NewGuid().ToString());
var filePath = Path.Combine(scratchPath, "test.gz");
if (!Directory.Exists(scratchPath))
{
Directory.CreateDirectory(scratchPath);
}
await using (var archive = (GZipArchive)await GZipArchive.CreateAsyncArchive())
{
await archive.AddEntryAsync("Tar.tar", Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"));
await archive.SaveToAsync(filePath, new GZipWriterOptions(CompressionLevel.BestSpeed));
}
var scratchPath2 = Path.Combine(SCRATCH_FILES_PATH, Guid.NewGuid().ToString());
if (!Directory.Exists(scratchPath2))
{
Directory.CreateDirectory(scratchPath2);
}
await using var archive2 = await GZipArchive.OpenAsyncArchive(
new AsyncOnlyStream(File.OpenRead(filePath))
);
await foreach (var entry in archive2.EntriesAsync)
{
await entry.WriteToDirectoryAsync(scratchPath2);
}
CompareFilesByPath(
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar"),
Path.Combine(scratchPath2, "Tar.tar")
);
}
}