2017-08-16 110 views
1

尝试使用Ionic zip库在UWP中创建zip文件。我手动将Ionic.Zip.dll添加到项目中。这样做后,下面的代码给了一个例外。UWP Ionic zip lib无压缩

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,版本= 4.0.0.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e'加载类型'System.IO.File'。'

不太确定最新的错误。需要帮忙。

也有任何可用于UWP的库,它有助于为zip文件设置密码? DotnetZip和CSharpZip都似乎不支持UWP项目类型。

回答

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!"); 
+0

我已经尝试使用这个库。不幸的是,它对我没有任何帮助。永远不要创建一个zip文件。我确实使用了您提供的示例代码。没有做任何事情。 –

+0

API需要在UWP中无法访问某个路径的路径,即使您是由FilePicker选择的路径。请尝试在您的应用程序中的路径。在处理UWP中的文件或文件夹时,一条重要规则是[跳过路径:粘贴到StorageFile](https://blogs.msdn.microsoft.com/wsdevsol/2012/12/04/skip-the-path-坚持到最storagefile /)。 –