Error when extracting files from split zip file

Aug 27, 2012 at 8:42 PM

I have created a set of zip files using dotnetzip library using the following

using (ZipFile zip = new ZipFile())
   if (chkEncryption.Checked)
      zip.Encryption = EncryptionAlgorithm.WinZipAes256;
      zip.Password = textPassword.Text;
   zip.Comment = "Archive";
   zip.MaxOutputSegmentSize = 2 * 1024 * 1024; //2MB
   zip.UseZip64WhenSaving = Zip64Option.Default;

It creates the zip files without any errors. I then try to extract all files from the zip file and get the following error while extracting.

"Bad MAC address"

I am extracting using the following code:

file.Extract(textExtractLoc.Text, ExtractExistingFileAction.OverwriteSilently);

I am using version I tried to create and extract using the tool provided in the package with the same error.

I did see that a similar issue was fixed in and closed, but looks like this is still existing. Can you help?






Aug 27, 2012 at 8:45 PM
Edited Aug 27, 2012 at 8:53 PM

Here is the exception

[Ionic.Zip.BadStateException] = {Ionic.Zip.BadStateException: The MAC does not match.
   at Ionic.Zip.WinZipAesCrypto.ReadAndVerifyMac(Stream s)
   at Ionic.Zip.ZipEntry.VerifyCrcAfterExtract(Int32 actualCrc32)
   at Ionic.Zip.ZipEntry.InternalExtract(String baseDir, Stream outstream, ...

The error occurs only when encryption is turned on.




Sep 5, 2012 at 4:59 PM

I'm also seeing this exact same issue with v1.9.1.8

Jan 16, 2013 at 1:25 PM

Same problem (I think) with - "The MAC does not match"

I need a fix for this, otherwise this DotNETZip Library is completely unusable for me. I need to programmatically split large data files.


Is there a way for me to get my hands on the source code, debug it and fix it for everyone? I'm new to the whole CodePlex / open source thing... but I do have 20 years MS software development experience and am sure I could fix it in a flash... Any pointers would be appreciated. I don't want to spend weeks on this as I have a job. But if I can get involved in an evening or 2 of my spare time... then I'd be interested.

Jan 17, 2013 at 12:26 AM

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!