From b9ddd4294e9b4b4bbb7b82652ae0e92562b881aa Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Wed, 13 May 2026 11:33:21 +0100 Subject: [PATCH 1/4] Fix GZip write async --- .../Archives/GZip/GZipArchiveEntry.cs | 2 +- .../Archives/GZip/GZipWritableArchiveEntry.cs | 10 ++++++ .../GZip/GZipArchiveAsyncTests.cs | 35 +++++++++++++++++++ 3 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs index 592425b2..9d975788 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs @@ -24,7 +24,7 @@ public class GZipArchiveEntry : GZipEntry, IArchiveEntry return Parts.Single().GetCompressedStream().NotNull(); } - public async ValueTask OpenEntryStreamAsync( + public virtual async ValueTask OpenEntryStreamAsync( CancellationToken cancellationToken = default ) { diff --git a/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs index 8171b872..740ef0ed 100644 --- a/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipWritableArchiveEntry.cs @@ -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 OpenEntryStreamAsync( + CancellationToken cancellationToken = default + ) + { + cancellationToken.ThrowIfCancellationRequested(); + return new(OpenEntryStream()); + } + internal override void Close() { if (closeStream) diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index 83043131..4fec8419 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -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") + ); + } } From 564f136845a96b3d721221336d129dad3b8fb56c Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 14 May 2026 08:36:23 +0100 Subject: [PATCH 2/4] Potential fix for pull request finding Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com> --- tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index 4fec8419..e8b1a7a4 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -204,8 +204,9 @@ public class GZipArchiveAsyncTests : ArchiveTests Directory.CreateDirectory(scratchPath2); } + await using var fileStream = File.OpenRead(filePath); await using var archive2 = await GZipArchive.OpenAsyncArchive( - new AsyncOnlyStream(File.OpenRead(filePath)) + new AsyncOnlyStream(fileStream) ); await foreach (var entry in archive2.EntriesAsync) { From b681f960779f4cdc593ab1ee04fecf3d69e395ae Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 14 May 2026 08:46:06 +0100 Subject: [PATCH 3/4] windows fix --- tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index e8b1a7a4..8c139c15 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -177,7 +177,7 @@ public class GZipArchiveAsyncTests : ArchiveTests { #if NETFRAMEWORK using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); -#else + #else await using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); #endif await using var archive = await GZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream)); @@ -204,7 +204,11 @@ public class GZipArchiveAsyncTests : ArchiveTests Directory.CreateDirectory(scratchPath2); } +#if NETFRAMEWORK + using var fileStream = File.OpenRead(filePath); +#else await using var fileStream = File.OpenRead(filePath); +#endif await using var archive2 = await GZipArchive.OpenAsyncArchive( new AsyncOnlyStream(fileStream) ); From 4816f09e6c315e3a1489ca781c26440c90e1ce38 Mon Sep 17 00:00:00 2001 From: Adam Hathcock Date: Thu, 14 May 2026 08:48:18 +0100 Subject: [PATCH 4/4] fmt --- tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs index 8c139c15..e8ce2a9d 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -177,7 +177,7 @@ public class GZipArchiveAsyncTests : ArchiveTests { #if NETFRAMEWORK using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); - #else +#else await using var stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")); #endif await using var archive = await GZipArchive.OpenAsyncArchive(new AsyncOnlyStream(stream));