user1120193
user1120193

Reputation: 250

How do I know whether the zip file is password protected or not with dotnetzip?

now let's think we got a random zip file so how do I know that file is password protected? cause there ain't a method like

bool IsPasswordProtected(string fileName);

I am asking this cause there are couple of methods to extract entries from a zip file but still I have to use either Extract() or ExtractWithPassword() but to use this I have to know the file that I am going to extract is actually password protected or not. I know the password applies to the entries not to the zip file itself. I checked every methods in the documentation but I couldn't find a suitable method to solve this issue or did I miss something?

Thanks.!

Upvotes: 2

Views: 3898

Answers (4)

SuperDeve
SuperDeve

Reputation: 11

I have the same problem too.i tried the code from Francis Upton .it wasn't useful. You can try

ZipFile.CheckZipPassword(TemporaryFilePath, PassWord);

make the PassWord to null;like...

bool ExistPassWord = ZipFile.CheckZipPassword(TemporaryFilePath, null);

Upvotes: 0

AT07
AT07

Reputation: 98

ZipFile zip = ZipFile.Read(zipFileName);

         if (!password.Equals("")) 
         { 
             zip.Password = password; 
         }

         try 
         {
             if (Directory.Exists(outputDirectory)) 
             { 
                 Directory.Delete(outputDirectory); 
             }

             Directory.CreateDirectory(outputDirectory);
             zip.ExtractAll(outputDirectory);
             System.Windows.Forms.MessageBox.Show("Unzip process is complete.", "Information");
         }
         catch (BadPasswordException e) 
         {
             string value = "Type password for unzip";
             if (InputBox("Zip file was password protected.", "Password : ",ref value ) == System.Windows.Forms.DialogResult.OK) 
             {
                 ExtractFileToDirectory(filename, outputpath,value);
             }
         }    

And InputBox method is follow...

public DialogResult InputBox(string title, string promptText, ref string value)
    {
        Form form = new Form();
        System.Windows.Forms.Label label = new System.Windows.Forms.Label();
        System.Windows.Forms.TextBox textBox = new System.Windows.Forms.TextBox();
        System.Windows.Forms.Button buttonOk = new System.Windows.Forms.Button();
        System.Windows.Forms.Button buttonCancel = new System.Windows.Forms.Button();

        form.Text = title;
        label.Text = promptText;
        textBox.Text = value;

        buttonOk.Text = "OK";
        buttonCancel.Text = "Cancel";
        buttonOk.DialogResult = System.Windows.Forms.DialogResult.OK;
        buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;

        label.SetBounds(9, 20, 372, 13);
        textBox.SetBounds(12, 36, 372, 20);
        buttonOk.SetBounds(228, 72, 75, 23);
        buttonCancel.SetBounds(309, 72, 75, 23);

        label.AutoSize = true;
        textBox.Anchor = textBox.Anchor | AnchorStyles.Right;
        buttonOk.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;
        buttonCancel.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;

        form.ClientSize = new System.Drawing.Size(396, 107);
        form.Controls.AddRange(new System.Windows.Forms.Control[] { label, textBox, buttonOk, buttonCancel });
        form.ClientSize = new System.Drawing.Size(Math.Max(300, label.Right + 10), form.ClientSize.Height);
        form.FormBorderStyle = FormBorderStyle.FixedDialog;
        form.StartPosition = FormStartPosition.CenterScreen;
        form.MinimizeBox = false;
        form.MaximizeBox = false;
        form.AcceptButton = buttonOk;
        form.CancelButton = buttonCancel;

        DialogResult dialogResult = form.ShowDialog();
        value = textBox.Text;
        return dialogResult;
    }    

The code is useful when zip file have password. If zip file have password, input text dialog appear and request password for extract. Note that (outputDirectory is string that for extract zip file location). I think that code was very useful for you.

Upvotes: 0

Francis Upton IV
Francis Upton IV

Reputation: 19443

I would try the ZipEntry.Extract method and if it fails due to missing a password you will get an exception, and then try the ExtractWithPassword and see if that works. If it does not work, then fail with the original exception. Unfortunately the Password property is write-only.

According to the documentation ZipEntry.UsesEntryption is not the same as requiring a password.

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340198

Check the ZipEntry.UsesEncryption property.

Upvotes: 4

Related Questions