【问题标题】:UWP Ionic zip lib no compressionUWP Ionic zip lib 无压缩
【发布时间】:2018-01-25 03:26:50
【问题描述】:

尝试使用 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 doesn't know about Windows 1252, how to fix?

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

但是我现在在同一行上得到了以下异常。 System.TypeLoadException:'无法从程序集'mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e'加载类型'System.IO.File'。'

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

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

【问题讨论】:

    标签: c# uwp dotnetzip ionic-zip


    【解决方案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!");
    

    【讨论】:

    • 我已经尝试过使用这个库。不幸的是,它对我没有任何作用。从不创建 zip 文件。我确实使用了您提供的示例代码。什么都没做。
    • API 需要某些路径在 UWP 中无法访问的路径,即使您通过 FilePicker 选择了该路径。请尝试在您的应用程序中的路径。在 UWP 中处理文件或文件夹时,一条重要的规则是Skip the path: stick to the StorageFile
    猜你喜欢
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多