Ionic.Zip extract files and ignore password

10.1k views Asked by At

I am using Ionic.Zip (Compact Framework version) in my Pocket-PC project.
Extracting zipped files (using Ionic.Zip) are working properly. If I put password on the compressed file, it requires the password before extracting, but when I try this instance, password validation for extraction fails.

Example: This folder is about to compress.

\MyDevice\My Documents\My Pictures  

This folder contains two files ('Flower.jpg','Waterfall.jpg')
Compressing the file using this code:

public string Compress(string[] Paths, string SaveFileName, string Password, string CompressionType)
{
    try
    {
        using (ZipFile zip = new ZipFile())
        {
            if (string.IsNullOrEmpty(Password))
                zip.Password = Password;
            zip.CompressionLevel = Utility.GetCompressionLevel(CompressionType);
            foreach (string item in Paths)
            {
                if (Utility.IsDirectory(item))
                    zip.AddDirectory(item);
                else if (Utility.IsFile(item))
                    zip.AddFile(item);
            }
            if (!SaveFileName.Trim().ToLower().EndsWith(".zip"))
                if (SaveFileName.Trim().EndsWith("."))
                    SaveFileName += "zip";
                else
                    SaveFileName += ".zip";

            zip.Save(SaveFileName);
        }
        return Utility.GetResourceString("ZipSuccess");
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}

Extraction code:

public string Decompress(string ZipFilePath, string TargetPath, string Password, bool OverwriteExistingFiles)
{
    try
    {
        using (ZipFile decompress = ZipFile.Read(ZipFilePath))
        {
            if (!string.IsNullOrEmpty(Password))
                decompress.Password = Password;

            foreach (ZipEntry e in decompress)
            {
                e.Extract(TargetPath, OverwriteExistingFiles ? ExtractExistingFileAction.OverwriteSilently : ExtractExistingFileAction.DoNotOverwrite);
            }
        }
        return Utility.GetResourceString("ExtractSuccess");
    }
    catch (Exception ex)
    {
        return ex.Message;
    }
}  

Extracting the file in this location works great, because it requires password:

\MyDevice\My Documents\Personal  

But! When I extract the file on the same folder:

\MyDevice\My Documents\My Pictures  

It extract the files without requiring the password.
I think this is a bug. What can I do for this?
Hope someone could answer. Thanks!

1

There are 1 answers

1
Shane On BEST ANSWER

There is a bug in your Compress() method. When compressing your files, the Password property of the ZipFile instance is never set. Take a look at your logic that decides whether or not to assign the zip.Password property.

It reads:

if (string.IsNullOrEmpty(Password))
    zip.Password = Password;

As written, the zip.Password property will be set only if the Password parameter is null or an empty string. If the Password parameter is a non-blank string, the code skips zip.Password assignment statement.

The if-statement in your Compress() method is missing a not operator. It should read:

if ( ! string.IsNullOrEmpty(Password))
    zip.Password = Password