diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs index 592425b2a..9d975788b 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs @@ -24,7 +24,7 @@ public virtual Stream OpenEntryStream() 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 8171b8724..740ef0edb 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 @@ public override Stream OpenEntryStream() 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 83043131d..4fec84194 100644 --- a/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs +++ b/tests/SharpCompress.Test/GZip/GZipArchiveAsyncTests.cs @@ -6,6 +6,8 @@ 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 async Task TestGzArchiveTypeGzip_Async() 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") + ); + } }