mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-22 15:04:43 +00:00
Merge pull request #1393 from adamhathcock/adam/flat-gzip-fix-stream-rewind
Fix SeekableSharpCompressStream.StopRecording() to rewind to recorded…
This commit is contained in:
@@ -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)
|
||||
{
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user