简体   繁体   English

DotNetZip解压缩时密码错误

[英]DotNetZip wrong password while unzipping

Scenario: upload file than try zip it using DotNetZip with password protection, password is generated with Membership.GeneratePassword() method. 方案:上传文件,然后尝试使用具有密码保护功能的DotNetZip对其进行压缩,并使用Membership.GeneratePassword()方法生成密码。 Everything is working fine except that sometimes user is not able to unzip files with generated password. 一切工作正常,除了有时用户无法使用生成的密码解压缩文件。 Wired thing is that this happens only sometimes let's say 1 out of 15 times. 有线的事情是,这种情况有时仅发生在15次中的1次。 Generate password: 生成密码:

public static String FilePassword()
{
    while (_filePassword.Length < 12)
    {
        _filePassword += string.Concat(Membership.GeneratePassword(1, 0).Where(char.IsLetterOrDigit));
    }

    return _filePassword;
}

Save file: 保存存档:

if (FileUploadControl.HasFile)
{

    fileName = Path.GetFileName(FileUploadControl.FileName);
    FileUploadControl.SaveAs(FileSavePath + fileName);

        // Archive uploaded file to zip.
        using (ZipFile zip = new ZipFile())
        {
            // File to be archived.
            var file = FileUploadControl.PostedFile;

            // Enable encryption of file
            zip.Encryption = EncryptionAlgorithm.PkzipWeak;

            // Set password.
            zip.Password = Settings.FilePassword();

            // Set temporary folder path for archive process.
            zip.TempFileFolder = tempPath;

            // Add file to archive with its path.
            zip.AddFile(FileSavePath + file.FileName, "");

            File objFile = new File(file.FileName, FileSavePath); 

            // Save zip file with the name as file ID.
            zip.Save(FileSavePath + file.FileName);
        }
}

I logged password while creating in method and also while protecting zip file with password, they are always matching, I cannot see what's wrong, why sometimes while unzipping file it shows wrong password. 我在创建方法时登录了密码,同时还在用密码保护zip文件时记录了密码,它们始终匹配,我看不到出什么问题了,为什么有时在解压缩文件时会显示错误的密码。

Why do you use the static global variable _filePassword in FilePassword() instead of one in scope? 为什么在FilePassword()使用静态全局变量_filePassword而不是作用域中的变量?

This way it could be modified from outside, or possible even still contain the last used value. 这样,可以从外部对其进行修改,甚至可能仍包含上次使用的值。 Also it isn't thread safe without lock . 同样,没有lock也不是线程安全的。

Settle with a local variable and it should be fine. 用局部变量结算,应该没问题。

public static String FilePassword()
{
    string retString = string.Empty;
    while (retString.Length < 12)
    {
        retString += string.Concat(Membership.GeneratePassword(1, 0).Where(char.IsLetterOrDigit));
    }    
    return retString;
}

You can log a return Value too. 您也可以记录一个返回值。


Sample for understanding 样品供理解

if (FileUploadControl.HasFile)
{

    fileName = Path.GetFileName(FileUploadControl.FileName);
    FileUploadControl.SaveAs(FileSavePath + fileName);
    string filePassword = Settings.FilePassword(); // Contains the Password

    using (ZipFile zip = new ZipFile())
    {
        var file = FileUploadControl.PostedFile;
        zip.Encryption = EncryptionAlgorithm.PkzipWeak;            
        zip.Password = filePassword; // <-- Set password for ZIP
        zip.TempFileFolder = tempPath;
        zip.AddFile(FileSavePath + file.FileName, "");
        File objFile = new File(file.FileName, FileSavePath);
        zip.Save(FileSavePath + file.FileName);
    }

    // Log this value!
    Log(filePassword);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM