Click here to Skip to main content
16,019,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to check the given password is correct or not for read the ZIP file at run time, If its wrong password i have to show error alert message.I using DotNetZip DLL for my project.Please Give your suggestion friends.
Posted

AFAIK, the only way to test it is to do a trial extraction. If it extracts ok, the password is fine.
If it doesn't, the password is not.
 
Share this answer
 
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:

C#
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 { }
    }
}
 
Share this answer
 
Comments
Musa.AJ 11-Jun-12 3:06am    
Thanks..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900