2017-08-16 101 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 /)。 –