从隔离存储提取zip文件

问题描述:

我被困在这个问题 - 显然,我做错了什么。从隔离存储提取zip文件

首先,我通过Web客户端下载一个压缩文件,并将其存储到IsolatedStorage:

using (var isf = IsolatedStorageFile.GetUserStoreForApplication()) 
{ 
    if (!isf.DirectoryExists("AppData")) isf.CreateDirectory("AppData"); 
    using (StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream("AppData\\" + FileName, FileMode.OpenOrCreate, isf))) 
    { 
     sw.Write(new StreamReader(e.Result).ReadToEnd()); 
    } 
} 

接着,我中提取一个特定的文件从Web客户端响应(zip文件)的:

Uri fileUri = new Uri("content.txt", UriKind.Relative); 
StreamResourceInfo info = new StreamResourceInfo(e.Result, null); 
StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(info, fileUri); 

这按预期工作。后来,我想这个提取从IsolatedStorage zip文件中的“content.txt”:

using (IsolatedStorageFileStream isfs = isf.OpenFile("AppData\\" + FileName, FileMode.Open, FileAccess.Read)) 
{ 
    if (myIsolatedStorage.FileExists("AppData\\" + FileName)) 
    { 
     Uri fileUri = new Uri("content.txt", UriKind.Relative); 
     StreamResourceInfo info = new StreamResourceInfo(isfs, null); 
     StreamResourceInfo streamInfo = System.Windows.Application.GetResourceStream(info, fileUri); 
    } 
} 

虽然zip压缩包,可以发现,streamInfo始终为空。我究竟做错了什么?

+1

哪个zip文件?如果你确实想从'IsolatedStoage'中读取,你为什么要用'StreamResourceInfo'而不是'StreamReader'! – Anirudha 2012-07-31 14:42:06

+0

@Airirha zip文件位于isolatedstorage文件夹中:AppData \\“+ FileName 我正在使用StreamResourceInfo,因为它可以本地访问压缩的zip压缩文件 – Jasper 2012-08-01 07:28:07

不幸的是,与桌面.Net框架不同,Windows Phone 7.x框架不知道如何从压缩文件进行流式传输。实际上,在Windows Phone上,根本无法访问System.IO.Compression命名空间。

幸运的是,DotNetZip库适用于WP7应用程序。您需要使用Compact Framework二进制DLL,而不是Silverlight。 Visual Studio会抱怨添加引用时库可能不兼容,但它会工作得很好。

+0

我正在使用这种技术来避免任何第三方lib:http:// www.sharpgis.net/post/2009/04/22/REALLY-small-unzip-utility-for-Silverlight.aspx – Jasper 2012-08-01 07:29:32