Skip to content

Commit

Permalink
Merge pull request #891 from Morilli/fix-zip-datadescriptor-header
Browse files Browse the repository at this point in the history
Fix zip entry handling for entries with data descriptors
  • Loading branch information
adamhathcock authored Jan 28, 2025
2 parents 79ed965 + 37a2fa1 commit 8d63ab6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
18 changes: 1 addition & 17 deletions src/SharpCompress/Common/Zip/SeekableZipFilePart.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
using System.IO;
using SharpCompress.Common.Zip.Headers;
using SharpCompress.IO;

namespace SharpCompress.Common.Zip;

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()
{
Expand All @@ -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;
}
}
6 changes: 6 additions & 0 deletions src/SharpCompress/Common/Zip/SeekableZipHeaderFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions tests/SharpCompress.Test/Zip/ZipArchiveTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -808,4 +808,16 @@ 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);
}
}

0 comments on commit 8d63ab6

Please sign in to comment.