icsharpcode SharpZipLib 未在 zip 文件上正确设置密码

icsharpcode SharpZipLib is not setting password correctly on zip file

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 文件并尝试使用我的函数压缩它,它根本不要求输入密码,尽管在代码中指定了密码。 (更有趣的是它在重置我的电脑后要求我输入密码,并且它工作正常,但后来删除了所有文件并再次尝试它在解压缩时不要求密码)

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

file.Password = password;

file.UseZip64 = UseZip64.On;

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

当它是 Dynamic 或 Off 时,它会在空文件上给出相同的错误。