mirror of
https://github.com/adamhathcock/sharpcompress.git
synced 2026-09-22 23:15:20 +00:00
When SeekableSharpCompressStream.StopRecording() was called (e.g. after the tar probe in GZipFactory.TryOpenReader), it cleared _recordedPosition without seeking back to it. This left the underlying FileStream at the advanced position reached during the probe, so the subsequent GZipReader would start reading from the wrong offset, producing Key=null and 0 extracted bytes. Fix: seek back to _recordedPosition before clearing it, matching the behavior of the non-seekable SharpCompressStream.StopRecording() which rewinds _logicalPosition. Also enhance the existing GZip_ReaderFactory_FlatGZip test to actually verify the extracted content (not just MoveToNextEntry returns true), and add an async variant. Closes #1391
60 lines
1.7 KiB
C#
60 lines
1.7 KiB
C#
using System.IO;
|
|
using System.IO.Compression;
|
|
using SharpCompress.Common;
|
|
using SharpCompress.IO;
|
|
using SharpCompress.Readers;
|
|
using SharpCompress.Readers.GZip;
|
|
using Xunit;
|
|
|
|
namespace SharpCompress.Test.GZip;
|
|
|
|
public class GZipReaderTests : ReaderTests
|
|
{
|
|
public GZipReaderTests() => UseExtensionInsteadOfNameToVerify = true;
|
|
|
|
[Fact]
|
|
public void GZip_Reader_Generic() => Read("Tar.tar.gz", CompressionType.GZip);
|
|
|
|
[Fact]
|
|
public void GZip_Reader_Generic2()
|
|
{
|
|
//read only as GZip itme
|
|
using Stream stream = File.OpenRead(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"));
|
|
using var reader = GZipReader.OpenReader(SharpCompressStream.CreateNonDisposing(stream));
|
|
while (reader.MoveToNextEntry()) // Crash here
|
|
{
|
|
Assert.NotEqual(0, reader.Entry.Size);
|
|
Assert.NotEqual(0, reader.Entry.Crc);
|
|
}
|
|
}
|
|
|
|
[Fact]
|
|
public void GZip_ReaderFactory_FlatGZip()
|
|
{
|
|
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))
|
|
{
|
|
gzip.Write(source, 0, source.Length);
|
|
}
|
|
|
|
using var stream = File.OpenRead(gzipPath);
|
|
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());
|
|
}
|
|
}
|