From d2cfc1844cdca9e168a50ebc80920362e21f4283 Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Tue, 28 Jan 2025 01:49:49 +0100 Subject: [PATCH 1/4] add failing test --- tests/SharpCompress.Test/Zip/ZipArchiveTests.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index 4fb6d89a..cf376ec1 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -808,4 +808,14 @@ public void Zip_Forced_Ignores_UnicodePathExtra() Assert.Equal("きょきゅんきゃんきゅ_wav.frq", reader.Entry.Key); } } + + [Fact] + public void TestDataDescriptorRead() + { + using var archive = ArchiveFactory.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.datadescriptors.zip")); + var firstEntry = archive.Entries.First(); + Assert.Equal(199, firstEntry.Size); + using var _ = firstEntry.OpenEntryStream(); + Assert.Equal(199, firstEntry.Size); + } } From c14d18b9dfb961d5cd78bc42e89486c5a76f538f Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Tue, 28 Jan 2025 01:56:14 +0100 Subject: [PATCH 2/4] set local header data from directory header when flag is set As described in section 4.4.7-4.4.9 of the zip specification when this flag is set the correct values will be in the data descriptor record and in the directory header. --- src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs index b99d0e2b..816b0b67 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs @@ -149,6 +149,12 @@ DirectoryEntryHeader directoryEntryHeader { throw new InvalidOperationException(); } + if (FlagUtility.HasFlag(localEntryHeader.Flags, HeaderFlags.UsePostDataDescriptor)) + { + localEntryHeader.Crc = directoryEntryHeader.Crc; + localEntryHeader.CompressedSize = directoryEntryHeader.CompressedSize; + localEntryHeader.UncompressedSize = directoryEntryHeader.UncompressedSize; + } return localEntryHeader; } } From 91e672befb94ea8e529a836b44515917973b68bc Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Tue, 28 Jan 2025 02:06:47 +0100 Subject: [PATCH 3/4] remove old hack trying to fix a similar thing Introduced in af264cdc58c9d076bf83477cbdbfe7a5dad282b7; the test included in that commit passes still. --- .../Common/Zip/SeekableZipFilePart.cs | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-) diff --git a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs index e37008e5..54c4f527 100644 --- a/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs +++ b/src/SharpCompress/Common/Zip/SeekableZipFilePart.cs @@ -1,6 +1,5 @@ using System.IO; using SharpCompress.Common.Zip.Headers; -using SharpCompress.IO; namespace SharpCompress.Common.Zip; @@ -8,18 +7,13 @@ internal class SeekableZipFilePart : ZipFilePart { private bool _isLocalHeaderLoaded; private readonly SeekableZipHeaderFactory _headerFactory; - private readonly DirectoryEntryHeader _directoryEntryHeader; internal SeekableZipFilePart( SeekableZipHeaderFactory headerFactory, DirectoryEntryHeader header, Stream stream ) - : base(header, stream) - { - _headerFactory = headerFactory; - _directoryEntryHeader = header; - } + : base(header, stream) => _headerFactory = headerFactory; internal override Stream GetCompressedStream() { @@ -44,16 +38,6 @@ protected override Stream CreateBaseStream() { BaseStream.Position = Header.DataStartPosition.NotNull(); - if ( - (Header.CompressedSize == 0) - && FlagUtility.HasFlag(Header.Flags, HeaderFlags.UsePostDataDescriptor) - && _directoryEntryHeader.HasData - && (_directoryEntryHeader.CompressedSize != 0) - ) - { - return new ReadOnlySubStream(BaseStream, _directoryEntryHeader.CompressedSize); - } - return BaseStream; } } From f9a974c1fef3bb34c5c08f51699f599b5edeef5c Mon Sep 17 00:00:00 2001 From: Morilli <35152647+Morilli@users.noreply.github.com> Date: Tue, 28 Jan 2025 02:24:42 +0100 Subject: [PATCH 4/4] fix formatting --- tests/SharpCompress.Test/Zip/ZipArchiveTests.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs index cf376ec1..8419f58f 100644 --- a/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs +++ b/tests/SharpCompress.Test/Zip/ZipArchiveTests.cs @@ -812,7 +812,9 @@ public void Zip_Forced_Ignores_UnicodePathExtra() [Fact] public void TestDataDescriptorRead() { - using var archive = ArchiveFactory.Open(Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.datadescriptors.zip")); + using var archive = ArchiveFactory.Open( + Path.Combine(TEST_ARCHIVES_PATH, "Zip.none.datadescriptors.zip") + ); var firstEntry = archive.Entries.First(); Assert.Equal(199, firstEntry.Size); using var _ = firstEntry.OpenEntryStream();