OK... once I downloaded the source, downloaded WiX, Silverlight developer stuff!! and got it running, it took me a little while to step through the code. But I got there in the end...
Basically on line 946 of ZipEntry.Extract.cs (in the ZipDLL project), in a method named VerifyCrcAfterExtract, there is a call to WinZipAesCipherStream.ReadAndVerifyMac
The ReadAndVerifyMac method assumes the Zip file stream we have been reading from is positioned at the end of the encrypted data for the last file that has been read out (and decrypted and inflated most probably).
In my case, where I am only extracting one file from the Zip archive, the file stream was disposed of once the file data was readout. So the call below to ReadAndVerifyMac actually creates a whole new file stream that is positioned at the beginning of the
file - and as you'd expect the call to ReadAndVerifyMac reads the completely wrong data from the file and fails.
WinZipAesCipherStream wzs = _inputDecryptorStream as WinZipAesCipherStream;
_aesCrypto_forExtract.CalculatedMac = wzs.FinalAuthentication;
_aesCrypto_forExtract.ReadAndVerifyMac(this.ArchiveStream); // throws if MAC is bad
The call above to this.ArchiveStream recreates the file stream from scratch, as it has already been disposed of.
A quick and dirty work around is to seek to the correct position in the file by executing the following line of code before the call to ReadAndVerifyMac -
this.ArchiveStream.Position = __FileDataPosition + _CompressedFileDataSize;
but a much better solution, avoiding the need to open a new file stream, is to read out and store the MAC from the file after the last encrypted/compressed data is read, but before the file stream is disposed of. For me that would be done in a method called
private Int32 ExtractOne(Stream output)
in the file ZipEntry.Extract.cs, just before the end of the try block at line 1073.................
Anyway, I have my fix and only have to worry about my own bugs now!