在 powershell 中使用 Dotnetzip 时出现问题,请考虑设置 UseZip64WhenSaving

Issue using Dotnetzip in powershell error consider setting UseZip64WhenSaving

我在 c# 中使用 dotnetzip 处理大文件没有问题。我需要压缩一些大文件到 power shell。我从 dotnetzip 文档中看到我可以在 ps 中使用它。 但我不断收到错误 压缩或未压缩大小,或偏移量超过最大值。考虑在 ZipFile 实例上设置 UseZip64WhenSaving 属性。 这是我的 PS 代码。如何在 PS 中设置 UseZip64WhenSaving?

[System.Reflection.Assembly]::LoadFrom("D:\mybigfiles\Ionic.Zip.dll");
$directoryToZip = "D:\mybigfiles\";
$zipfile =  new-object Ionic.Zip.ZipFile;
#$zipfile.UseZip64WhenSaving;
$e= $zipfile.AddEntry("mybig.csv", "This is a zipfile created from within powershell.")
$e= $zipfile.AddDirectory($directoryToZip, "home")
$zipfile.Save("D:\mybigfiles\big.zip");
$zipfile.Dispose();

工作 C# 代码。

            using (ZipFile zip = new ZipFile())
            {
                zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
                zip.AddFile(compressedFileName);
                zip.AddFile("\\server\bigfile\CM_Report_20220411200326.csv");
                zip.AddFile("\\server\bigfile\PM_Report_20220411200326.csv");
                zip.AddFile("\\server\bigfile\SCE_Report_20220411200326.csv");
                
            }```

与 C# 不同,PowerShell 喜欢隐式类型转换 - 当您将字符串值分配给 [=26= 时,它会隐式解析并将字符串值转换为其同源枚举值] 属性:

$zipfile.UseZip64WhenSaving = 'AsNecessary'

或者,确保您限定枚举类型名称:

#$zipfile.UseZip64WhenSaving = [Ionic.Zip.Zip64Option]::AsNecessary

还值得注意的是,所有 PowerShell 字符串文字的行为都类似于 C# 中的 verbatim 字符串 - 换句话说,\不是需要转义的特殊字符:

$directoryToZip = "D:\mybigfiles\"
# ...
$e = $zipfile.AddDirectory($directoryToZip, "home")
$zipfile.Save("D:\mybigfiles\big.zip")