1

尝试使用 Ionic zip 库在 UWP 中创建一个 zip 文件。我手动将 Ionic.Zip.dll 添加到项目中。之后,下面的代码给出了一个异常。

using (ZipFile zip = new ZipFile()) -------------> Exception on this line
            {

                zip.Password = "password";                
                zip.AddFile(file.Name);
                zip.Save();
            }

异常:System.ArgumentException:“IBM437”不是受支持的编码名称。有关定义自定义编码的信息,请参阅 Encoding.RegisterProvider 方法的文档。

在此问题上点击以下链接并修改了 project.json 以及以下代码行: .NET Core 不知道 Windows 1252,如何修复?

Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
var enc1252 = Encoding.GetEncoding(437);

但是我现在在同一行上得到了以下异常。System.TypeLoadException:“无法从程序集“mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e”加载类型“System.IO.File”。

不太确定出了什么问题。需要帮忙。

还有任何 UWP 可用的库可以帮助设置 zip 文件的密码吗?DotnetZip 和 CSharpZip 似乎都不支持 UWP 项目类型。

4

1 回答 1

1

我们无法通过 Ionic zip 库将密码添加到 ZipFile。默认的 System.IO.Compression 库也没有密码属性。

我们应该可以使用第三方 NuGet 包来添加密码,例如Chilkat.uwp。我们可以使用Zip.SetPassword方法来设置 zip 文件的密码。

例如:

Chilkat.Zip zip = new Chilkat.Zip();
bool success;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.TemporaryFolder;
string a = localFolder.Path + "\\sample.zip";
success = zip.NewZip(a);
if (success != true)
{
    Debug.WriteLine(zip.LastErrorText);
    return;
}
zip.SetPassword("secret");
zip.PasswordProtect = true;
bool saveExtraPath;
saveExtraPath = false;
StorageFolder appInstalledFolder = Windows.ApplicationModel.Package.Current.InstalledLocation;
StorageFolder assets = await appInstalledFolder.GetFolderAsync("Assets");
string filePath = assets.Path + "\\rainier.jpg";
success = await zip.AppendOneFileOrDirAsync(filePath, saveExtraPath);
bool success2 = await zip.WriteZipAndCloseAsync();
if (success != true)
{
    Debug.WriteLine(zip.LastErrorText);
    return;
}
Debug.WriteLine("Zip Created!");
于 2017-08-17T07:26:46.280 回答