2017-04-21 41 views
2

我的應用程序是通用窗口平臺應用程序。我嘗試實現運行時組件來解壓縮壓縮文件夾。我的要求之一是可以處理超過260個字符的路徑。'無法在UWP上加載文件或程序集System.IO.Compression'

public static IAsyncActionWithProgress <string> Unzip(string zipPath, string destination) { 
    return AsyncInfo.Run <string> (async(_, progress) => { 
     var zipFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri(zipPath)); 

     try { 
      Stream stream = await zipFile.OpenStreamForReadAsync(); 
      ZipArchive archive = new ZipArchive(stream); 
      archive.ExtractToDirectory(destination); 
     } catch (Exception e) { 
      Debug.WriteLine(e.Message); 
     } 
    }); 
} 

我試圖執行我的方法得到下面的異常消息:

System.IO.FileNotFoundException: Could not load file or assembly 'System.IO.Compression, Version=4.1.1.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The system cannot find the file specified. 
    at Zip.Zip.<>c__DisplayClass0_0.<<Unzip>b__0>d.MoveNext() 
    at System.Runtime.CompilerServices.AsyncTaskMethodBuilder.Start[TStateMachine](TStateMachine& stateMachine) 
    at Zip.Zip.<>c__DisplayClass0_0.<Unzip>b__0(CancellationToken _, IProgress`1 progress) 
    at System.Thread (~4340) 

我試圖用的NuGet添加System.IO.Compression但我仍然得到同樣的錯誤。壓縮文件和目標文件夾存在。

我試圖在Visual Studio 2015而不是Visual Studio 2017上調試我的項目,發現我可以使用System.IO.Compression,但是對路徑長度有限制。

回答

0

首先,通過在最新的Visual Studio 2017中進行測試,您的代碼段可以在文件路徑默認情況下小於260個字符的情況下正常工作。如果使用比260個字符更長的路徑,visual studio 2017會拋出異常

System.IO.PathTooLongException:'文件名或擴展名太長。

它與Visual Studio 2015相同。所以請檢查您是否正確使用名稱空間。 using System.IO.Compression;

我的一個要求是可以處理超過260個字符的路徑。

其次,當您試圖通過GetFileFromApplicationUriAsync方法獲取文件時引發錯誤。所以這不是System.IO.Compression命名空間問題,你實際需要解決的是最大路徑長度限制,詳情請參考this article。有關文件路徑過長問題的更多詳細信息,請嘗試引用this thread。但他們可能不適合UWP應用程序。

在UWP應用程序中,您可以使用FileOpenPicker獲取文件並將文件傳遞給組件以讀取文件流。文件選擇器會將長文件名轉換爲像「xxx-UN〜1.ZIP」一樣的鏡頭以供讀取。

FileOpenPicker openpicker = new FileOpenPicker(); 
openpicker.FileTypeFilter.Add(".zip"); 
var zipfile = await openpicker.PickSingleFileAsync(); 
Stream stream = await zipfile.OpenStreamForReadAsync(); 
ZipArchive archive = new ZipArchive(stream); 
archive.ExtractToDirectory(destination); 

我建議您避免使用長文件路徑。

相關問題