Fix version mismatch between Local File Header and Central Directory File Header in Zip archives#1023
Conversation
When UseZip64=true but files are small, ensure Central Directory File Header uses version 45 to match Local File Header. This fixes validation failures in System.IO.Packaging and other strict readers. Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Also fixes pre-existing version mismatch for advanced compression methods that require version 63. Added tests for LZMA and PPMd to verify version consistency. Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
adamhathcock
left a comment
There was a problem hiding this comment.
Version to extract should be written as Zip64 when using it. Fix others
There was a problem hiding this comment.
Pull Request Overview
This pull request fixes a version mismatch between Local File Header (LFH) and Central Directory File Header (CDFH) in ZIP archives when UseZip64=true is set with small files. The mismatch caused strict ZIP readers like System.IO.Packaging.ZipPackage to fail validation.
Key Changes:
- Modified version determination logic in
ZipCentralDirectoryEntry.Write()to check for Zip64 extension usage viaZip64HeaderOffset - Added explicit version 63 handling for advanced compression methods (LZMA, PPMd, BZip2, ZStandard)
- Added comprehensive test suite to verify version consistency across different scenarios
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
src/SharpCompress/Writers/Zip/ZipCentralDirectoryEntry.cs |
Updated version determination logic to prioritize compression method requirements, then Zip64 extension usage, then default to version 20 |
tests/SharpCompress.Test/Zip/Zip64VersionConsistencyTests.cs |
Added 5 new tests validating version consistency for Zip64 enabled/disabled scenarios and LZMA/PPMd compression methods |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
None (stored) compression only requires version 10/20, not version 63. Version 63 is specifically for advanced compression methods like LZMA, PPMd, BZip2, and ZStandard. Co-authored-by: adamhathcock <527620+adamhathcock@users.noreply.github.com>
Plan: Fix Zip64 Version Mismatch Between LFH and CDFH
Problem
When
UseZip64=truebut files are small (< 4GB), there's a version mismatch:Root Cause
ZipWriter.WriteHeader(): Version is set based on compression method and Zip64 usageZipCentralDirectoryEntry.Write(): Version was set to 45 only when actual sizes/offsets exceed uint.MaxValue, ignoring compression method requirementsSolution
ZipCentralDirectoryEntry.Write()to properly handle:ZipCompressionMethod.Nonefrom version 63 check (per review feedback)Changes Made
Modified
ZipCentralDirectoryEntry.cs:Added
Zip64VersionConsistencyTests.cs: Created 5 testsTest Results
This ensures archives created with
UseZip64=trueor advanced compression methods can be opened by System.IO.Packaging and other strict ZIP readers.Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.