gzip using sync generator

This commit is contained in:
Adam Hathcock
2026-08-03 08:15:18 +01:00
parent 187055e673
commit c4fa31d615
20 changed files with 229 additions and 277 deletions

View File

@@ -240,4 +240,35 @@ public class GZipArchiveAsyncTests : ArchiveTests
Assert.True(entryStream.IsDisposed);
}
[Fact]
public async ValueTask GZip_Archive_SaveTo_Async_Preserves_Modification_Time()
{
var modificationTime = new DateTime(2024, 1, 2, 3, 4, 5);
using var destination = new MemoryStream();
await using (var archive = (GZipArchive)await GZipArchive.CreateAsyncArchive())
{
await archive.AddEntryAsync(
"contents.bin",
new MemoryStream(new byte[] { 1, 2, 3, 4 }),
size: 4,
modified: modificationTime
);
await archive.SaveToAsync(destination, new GZipWriterOptions());
}
destination.Position = 0;
await using var result = await GZipArchive.OpenAsyncArchive(destination);
var entry = await result.EntriesAsync.FirstAsync();
Assert.Equal(modificationTime, entry.LastModifiedTime);
}
[Fact]
public async ValueTask GZip_Archive_IsGZipFileAsync_ReturnsFalse_For_Truncated_Header()
{
using var stream = new MemoryStream(new byte[] { 0x1F, 0x8B, 8 });
Assert.False(await GZipArchive.IsGZipFileAsync(stream));
}
}

View File

@@ -1,6 +1,8 @@
using System.IO;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Providers;
using SharpCompress.Providers.System;
using SharpCompress.Test.Mocks;
using SharpCompress.Writers;
using SharpCompress.Writers.GZip;
@@ -91,4 +93,33 @@ public class GZipWriterAsyncTests : WriterTests
Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz")
);
}
[Fact]
public async ValueTask GZip_Writer_Async_With_System_Provider()
{
var contents = new byte[] { 1, 2, 3, 4 };
var providers = CompressionProviderRegistry.Default.With(
new SystemGZipCompressionProvider()
);
using var source = new MemoryStream(contents);
using var destination = new MemoryStream();
await using (
var writer = new GZipWriter(
destination,
new GZipWriterOptions { LeaveStreamOpen = true, Providers = providers }
)
)
{
await writer.WriteAsync("contents.bin", source);
}
destination.Position = 0;
using var compressed = new SystemGZipCompressionProvider().CreateDecompressStream(
destination
);
using var actual = new MemoryStream();
compressed.CopyTo(actual);
Assert.Equal(contents, actual.ToArray());
}
}

View File

@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.Writers.GZip;
using Xunit;
@@ -16,4 +17,15 @@ public class GZipWriterDirectoryTests : TestBase
Assert.Throws<NotSupportedException>(() => writer.WriteDirectory("test-dir", DateTime.Now));
}
[Fact]
public async ValueTask GZipWriter_WriteDirectoryAsync_ThrowsNotSupportedException()
{
using var memoryStream = new MemoryStream();
using var writer = new GZipWriter(memoryStream, new GZipWriterOptions());
await Assert.ThrowsAsync<NotSupportedException>(async () =>
await writer.WriteDirectoryAsync("test-dir", DateTime.Now)
);
}
}

View File

@@ -220,6 +220,21 @@ public class OptionsUsabilityTests : TestBase
Assert.Equal(19, source.CopyBufferSize);
}
[Fact]
public async Task GZipWriter_Uses_WriterOptions_BufferSize_Async()
{
await using var source = new TrackingReadStream(new byte[100]);
await using var destination = new MemoryStream();
await using var writer = new GZipWriter(
destination,
new GZipWriterOptions { BufferSize = 23 }
);
await writer.WriteAsync("buffer-size.txt", source, DateTime.Now);
Assert.Equal(23, source.CopyBufferSize);
}
[Fact]
public void TarWriter_Uses_WriterOptions_BufferSize_ForTransfer()
{