Merge pull request #1393 from adamhathcock/adam/flat-gzip-fix-stream-rewind

Fix SeekableSharpCompressStream.StopRecording() to rewind to recorded…
This commit is contained in:
Adam Hathcock
2026-07-31 16:31:43 +01:00
committed by GitHub
3 changed files with 46 additions and 1 deletions

View File

@@ -83,7 +83,16 @@ internal sealed partial class SeekableSharpCompressStream : SharpCompressStream
public override void StartRecording(int? minBufferSize = null) =>
_recordedPosition = _stream.Position;
public override void StopRecording() => _recordedPosition = null;
public override void StopRecording()
{
if (_recordedPosition.HasValue)
{
// Seek back to the recording anchor position, matching the behavior of the
// non-seekable SharpCompressStream.StopRecording() which rewinds _logicalPosition.
_stream.Seek(_recordedPosition.Value, SeekOrigin.Begin);
}
_recordedPosition = null;
}
protected override void Dispose(bool disposing)
{

View File

@@ -1,4 +1,5 @@
using System.IO;
using System.IO.Compression;
using System.Threading.Tasks;
using SharpCompress.Common;
using SharpCompress.IO;
@@ -37,4 +38,33 @@ public class GZipReaderAsyncTests : ReaderTests
}
}
}
[Fact]
public async ValueTask GZip_ReaderFactory_FlatGZip_Async()
{
var source = new byte[2048];
for (var i = 0; i < source.Length; i++)
{
source[i] = 0xFF;
}
var gzipPath = Path.Combine(SCRATCH_FILES_PATH, "Flat.bin.gz");
using (var output = File.Create(gzipPath))
using (var gzip = new GZipStream(output, CompressionMode.Compress))
{
await gzip.WriteAsync(source, 0, source.Length);
}
using Stream stream = File.OpenRead(gzipPath);
await using var reader = await ReaderFactory.OpenAsyncReader(stream);
Assert.IsType<GZipReader>(reader);
Assert.True(await reader.MoveToNextEntryAsync());
using var ms = new MemoryStream();
await reader.WriteEntryToAsync(ms);
Assert.Equal(source.Length, ms.Length);
Assert.Equal(source, ms.ToArray());
Assert.False(await reader.MoveToNextEntryAsync());
}
}

View File

@@ -48,6 +48,12 @@ public class GZipReaderTests : ReaderTests
using var reader = ReaderFactory.OpenReader(stream);
Assert.IsType<GZipReader>(reader);
Assert.True(reader.MoveToNextEntry());
using var ms = new MemoryStream();
reader.WriteEntryTo(ms);
Assert.Equal(source.Length, ms.Length);
Assert.Equal(source, ms.ToArray());
Assert.False(reader.MoveToNextEntry());
}
}