【问题标题】:icsharpcode SharpZipLib is not setting password correctly on zip fileicsharpcode SharpZipLib 未在 zip 文件上正确设置密码
【发布时间】:2015-07-29 18:26:19
【问题描述】:

zip 文件似乎已在我的驱动器上成功创建,其中包含我在 filePaths[] 中指定的文件

问题在于,尽管将密码设置为“hello”,但当我尝试解压缩文件时,它会提示密码不正确。

/// <summary>
    /// Creates a zip file
    /// </summary>
    /// <param name="filePaths">An array of files to add to the zip file</param>
    /// <param name="password">Password of zip file. Set to null to ignore</param>
    /// <param name="outputName">The name of the outputted zip file (including full path) </param>
    /// <returns></returns>
    public string CreateZip(string[] filePaths, string password, string outputName)
    {
        outputName += ".zip";

        using (var file = ZipFile.Create(outputName))
        {
            file.BeginUpdate();

            filePaths.ToList().ForEach(x =>
            {
                if (Path.HasExtension(x))
                {
                    file.Add(x);
                }

                else if (!Path.HasExtension(x) && Directory.Exists(x))
                {
                    Directory.GetFiles(x, "*.*", SearchOption.AllDirectories).ToList().ForEach(file.Add);
                }
            });

            file.Password = password;

            file.CommitUpdate();
            file.Close();

        }

        return outputName;
    }

有趣的是,这个问题只发生在 zip 中的文件大小为 0 字节(它是一个空的 xml 文件)时。

如果我将内容添加到 .xml 文件并尝试使用我的函数对其进行压缩,它根本不会要求输入密码,尽管在代码中指定了密码。 (更有趣的是它在重置我的计算机后要求我输入密码,并且它工作正常,但随后删除所有文件并再次尝试它在解压缩时不要求输入密码)

【问题讨论】:

    标签: c# .net icsharpcode


    【解决方案1】:

    我测试了您的代码并重现了该问题。在提交之前将 UseZip64 设置为 On 对我有用:

    file.Password = password;
    
    file.UseZip64 = UseZip64.On;
    
    file.CommitUpdate();
    file.Close();
    

    当它是动态或关闭时,它会在空文件上给出相同的错误。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-01
    相关资源
    最近更新 更多