Skip to content

Commit

Permalink
Merge PR #363: Change ZipFile.ReadEntries to always look for the Zip6…
Browse files Browse the repository at this point in the history
…4 central directory

* Change ZipFile.ReadEntries to always look for the Zip64 central directory
* Add test for Zip64 preference
  • Loading branch information
Numpsy authored and piksel committed Aug 8, 2019
1 parent 34d7472 commit ffe5115
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
16 changes: 13 additions & 3 deletions src/ICSharpCode.SharpZipLib/Zip/ZipFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3408,6 +3408,7 @@ private void ReadEntries()
}

bool isZip64 = false;
bool requireZip64 = false;

// Check if zip64 header information is required.
if ((thisDiskNumber == 0xffff) ||
Expand All @@ -3417,13 +3418,22 @@ private void ReadEntries()
(centralDirSize == 0xffffffff) ||
(offsetOfCentralDir == 0xffffffff))
{
isZip64 = true;
requireZip64 = true;
}

long offset = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000);
if (offset < 0)
// #357 - always check for the existance of the Zip64 central directory.
long locatedZip64EndOfCentralDir = LocateBlockWithSignature(ZipConstants.Zip64CentralDirLocatorSignature, locatedEndOfCentralDir, 0, 0x1000);
if (locatedZip64EndOfCentralDir < 0)
{
if (requireZip64)
{
// This is only an error in cases where the Zip64 directory is required.
throw new ZipException("Cannot find Zip64 locator");
}
}
else
{
isZip64 = true;

// number of the disk with the start of the zip64 end of central directory 4 bytes
// relative offset of the zip64 end of central directory record 8 bytes
Expand Down
24 changes: 19 additions & 5 deletions test/ICSharpCode.SharpZipLib.Tests/Zip/ZipCorruptionHandling.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using ICSharpCode.SharpZipLib;
using ICSharpCode.SharpZipLib.Zip;
using NUnit.Framework;

namespace ICSharpCode.SharpZipLib.Tests.Zip
{
public class ZipCorruptionHandling
public class ZipCorruptionHandling
{

const string TestFileZeroCodeLength = "UEsDBBQA+AAIANwyZ0U5T8HwjQAAAL8AAAAIABUAbGltZXJpY2t" +
Expand Down Expand Up @@ -52,6 +49,23 @@ public void ZeroCodeLengthZipFile()
});
}

}
const string TestFileBadCDGoodCD64 = @"UEsDBC0AAAAIANhy+k4cj+r8//////////8IABQAdGVzdGZpbGUBABAAAAA
AAAAAAAAUAAAAAAAAACtJLS5Jy8xJVUjOzytJzSsp5gIAUEsBAjMALQAAAAgA2HL6ThyP6vz//////////wgAFAAAAAAAA
AAAAAAAAAAAAHRlc3RmaWxlAQAQABIAAAAAAAAAFAAAAAAAAABQSwUGAAAAAAEAAQBKAAAATgAAAAAA";

[Test]
[Category("Zip")]
public void CorruptCentralDirWithCorrectZip64CD()
{
var fileBytes = Convert.FromBase64String(TestFileBadCDGoodCD64);
using (var ms = new MemoryStream(fileBytes))
using (var zip = new ZipFile(ms))
{
Assert.AreEqual(1, zip.Count);
Assert.AreNotEqual(0, zip[0].Size, "Uncompressed file size read from corrupt CentralDir instead of CD64");
}
}

}

}

0 comments on commit ffe5115

Please sign in to comment.