diff --git a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs index 4b3835e0..cdbbc89d 100644 --- a/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs +++ b/src/SharpCompress/Archives/GZip/GZipArchiveEntry.cs @@ -14,6 +14,12 @@ namespace SharpCompress.Archives.GZip public virtual Stream OpenEntryStream() { + //this is to reset the stream to be read multiple times + var part = Parts.Single() as GZipFilePart; + if (part.GetRawStream().Position != part.EntryStartPosition) + { + part.GetRawStream().Position = part.EntryStartPosition; + } return Parts.Single().GetCompressedStream(); } diff --git a/src/SharpCompress/Common/GZip/GZipFilePart.cs b/src/SharpCompress/Common/GZip/GZipFilePart.cs index b0880992..f793195a 100644 --- a/src/SharpCompress/Common/GZip/GZipFilePart.cs +++ b/src/SharpCompress/Common/GZip/GZipFilePart.cs @@ -16,9 +16,12 @@ namespace SharpCompress.Common.GZip internal GZipFilePart(Stream stream) { ReadAndValidateGzipHeader(stream); + EntryStartPosition = stream.Position; this.stream = stream; } + internal long EntryStartPosition { get; } + internal DateTime? DateModified { get; private set; } internal override string FilePartName { get { return name; } } diff --git a/test/SharpCompress.Test/GZip/GZipArchiveTests.cs b/test/SharpCompress.Test/GZip/GZipArchiveTests.cs index c0b85745..784c270b 100644 --- a/test/SharpCompress.Test/GZip/GZipArchiveTests.cs +++ b/test/SharpCompress.Test/GZip/GZipArchiveTests.cs @@ -55,5 +55,47 @@ namespace SharpCompress.Test archive.SaveTo(Path.Combine(SCRATCH_FILES_PATH, "Tar.tar.gz")); } } + + + [Fact] + public void GZip_Archive_Multiple_Reads() + { + ResetScratch(); + var inputStream = new MemoryStream(); + using (var fileStream = File.Open(Path.Combine(TEST_ARCHIVES_PATH, "Tar.tar.gz"), FileMode.Open)) + { + fileStream.CopyTo(inputStream); + inputStream.Position = 0; + } + using (var archive = GZipArchive.Open(inputStream)) + { + var archiveEntry = archive.Entries.First(); + + MemoryStream tarStream; + using (var entryStream = archiveEntry.OpenEntryStream()) + { + tarStream = new MemoryStream(); + entryStream.CopyTo(tarStream); + } + var size = tarStream.Length; + using (var entryStream = archiveEntry.OpenEntryStream()) + { + tarStream = new MemoryStream(); + entryStream.CopyTo(tarStream); + } + Assert.Equal(size, tarStream.Length); + using (var entryStream = archiveEntry.OpenEntryStream()) + { + var result = SharpCompress.Archives.Tar.TarArchive.IsTarFile(entryStream); + } + Assert.Equal(size, tarStream.Length); + using (var entryStream = archiveEntry.OpenEntryStream()) + { + tarStream = new MemoryStream(); + entryStream.CopyTo(tarStream); + } + Assert.Equal(size, tarStream.Length); + } + } } }