Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am using ICSharpCode.SharpZipLib utility for unziping password protected zip files.
i'm using a loop which tries a couple of passwords given till one of them opens the files without going through the process of typing them.
but i have faced a problem. if the first password the program tries is wrong then the files in the zipped file are extracted but the files have a zero capacity.
and we face the access denied error which makes the continuation of the program to check other passwords impossible.
how can i deal with this problem?
i appreciate your help.
here is the unzip method:

public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
        {
            ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
            if (password != null && password != String.Empty)
                s.Password = password;
                
            ZipEntry theEntry;
            string tmpEntry = String.Empty;
            while ((theEntry = s.GetNextEntry()) != null)
            {
                string directoryName = outputFolder;
                string fileName = Path.GetFileName(theEntry.Name);
                // create directory 
                if (directoryName != "")
                {
                    Directory.CreateDirectory(directoryName);
                }
                if (fileName != String.Empty)
                {
                    if (theEntry.Name.IndexOf(".ini") < 0)
                    {
                        string fullPath = directoryName + "\\" + theEntry.Name;
                        fullPath = fullPath.Replace("\\ ", "\\");
                        string fullDirPath = Path.GetDirectoryName(fullPath);
                        if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
                        FileStream streamWriter = File.Create(fullPath);
                        int size = 2048;
                        byte[] data = new byte[2048];
                        while (true)
                        {
                            size = s.Read(data, 0, data.Length);
                            if (size > 0)
                            {
                                streamWriter.Write(data, 0, size);
                            }
                            else
                            {
                                break;
                            }
                        }
                        streamWriter.Close();
                    }
                }
            }
            s.Close();
            if (deleteZipFile)
                File.Delete(zipPathAndFile);
        }
    }
Posted
Updated 18-Aug-10 3:50am
v2
Comments
Andrew Rissing 18-Aug-10 9:50am    
Be sure to use 'pre' tags for long blocks of code, 'code' tags are used only for inline references to classes/methods/etc.

Are you checking to make sure the password is valid before trying to uncompress the zip file?

Try putting a try/catch block around the code to see where it's failing.
 
Share this answer
 
yes i did
the password did not match exception will occur.

finally i decide to use dotNetZip component from codeplex
 
Share this answer
 

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