We solved this problem by extending MemoryStream and overriding the Write() method.
According to the forum post
here [http://dotnetzip.codeplex.com], the DotNetZip code will throw an exception after trying the first few bytes of the ZipEntry if the password is incorrect.
Therefore, if the call to Extract() ever calls our Write() method, we know the password worked. Here's the code snippet:
public class ZipPasswordTester
{
public bool CheckPassword(Ionic.Zip.ZipEntry entry, string password)
{
try
{
using (var s = new PasswordCheckStream())
{
entry.ExtractWithPassword(s, password);
}
return true;
}
catch (Ionic.Zip.BadPasswordException)
{
return false;
}
catch (PasswordCheckStream.GoodPasswordException)
{
return true;
}
}
private class PasswordCheckStream : System.IO.MemoryStream
{
public override void Write(byte[] buffer, int offset, int count)
{
throw new GoodPasswordException();
}
public class GoodPasswordException : System.Exception { }
}
}