Skip to content

Commit ffe5115

Browse files
Numpsypiksel
authored andcommitted
Merge PR #363: Change ZipFile.ReadEntries to always look for the Zip64 central directory
* Change ZipFile.ReadEntries to always look for the Zip64 central directory * Add test for Zip64 preference
1 parent 34d7472 commit ffe5115

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -3408,6 +3408,7 @@ private void ReadEntries()
34083408
}
34093409

34103410
bool isZip64 = false;
3411+
bool requireZip64 = false;
34113412

34123413
// Check if zip64 header information is required.
34133414
if ((thisDiskNumber == 0xffff) ||
@@ -3417,13 +3418,22 @@ private void ReadEntries()
34173418
(centralDirSize == 0xffffffff) ||
34183419
(offsetOfCentralDir == 0xffffffff))
34193420
{
3420-
isZip64 = true;
3421+
requireZip64 = true;
3422+
}
34213423

3422-
long offset = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000);
3423-
if (offset < 0)
3424+
// #357 - always check for the existance of the Zip64 central directory.
3425+
long locatedZip64EndOfCentralDir = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000);
3426+
if (locatedZip64EndOfCentralDir < 0)
3427+
{
3428+
if (requireZip64)
34243429
{
3430+
// This is only an error in cases where the Zip64 directory is required.
34253431
throw new ZipException("Cannot find Zip64 locator");
34263432
}
3433+
}
3434+
else
3435+
{
3436+
isZip64 = true;
34273437

34283438
// number of the disk with the start of the zip64 end of central directory 4 bytes
34293439
// relative offset of the zip64 end of central directory record 8 bytes

test/ICSharpCode.SharpZipLib.Tests/Zip/ZipCorruptionHandling.cs

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
using System;
22
using System.IO;
3-
using System.Runtime.InteropServices;
4-
using System.Text;
53
using System.Threading;
6-
using ICSharpCode.SharpZipLib;
74
using ICSharpCode.SharpZipLib.Zip;
85
using NUnit.Framework;
96

107
namespace ICSharpCode.SharpZipLib.Tests.Zip
118
{
12-
public class ZipCorruptionHandling
9+
public class ZipCorruptionHandling
1310
{
1411

1512
const string TestFileZeroCodeLength = "UEsDBBQA+AAIANwyZ0U5T8HwjQAAAL8AAAAIABUAbGltZXJpY2t" +
@@ -52,6 +49,23 @@ public void ZeroCodeLengthZipFile()
5249
});
5350
}
5451

55-
}
52+
const string TestFileBadCDGoodCD64 = @"UEsDBC0AAAAIANhy+k4cj+r8//////////8IABQAdGVzdGZpbGUBABAAAAA
53+
AAAAAAAAUAAAAAAAAACtJLS5Jy8xJVUjOzytJzSsp5gIAUEsBAjMALQAAAAgA2HL6ThyP6vz//////////wgAFAAAAAAAA
54+
AAAAAAAAAAAAHRlc3RmaWxlAQAQABIAAAAAAAAAFAAAAAAAAABQSwUGAAAAAAEAAQBKAAAATgAAAAAA";
55+
56+
[Test]
57+
[Category("Zip")]
58+
public void CorruptCentralDirWithCorrectZip64CD()
59+
{
60+
var fileBytes = Convert.FromBase64String(TestFileBadCDGoodCD64);
61+
using (var ms = new MemoryStream(fileBytes))
62+
using (var zip = new ZipFile(ms))
63+
{
64+
Assert.AreEqual(1, zip.Count);
65+
Assert.AreNotEqual(0, zip[0].Size, "Uncompressed file size read from corrupt CentralDir instead of CD64");
66+
}
67+
}
68+
69+
}
5670

5771
}

0 commit comments

Comments
 (0)