2017-04-21 194 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); 

我建议您避免使用长文件路径。

相关问题