在 Windows Phone 7.5 上使用压缩
来源:互联网 发布:mac os 引导工具 编辑:程序博客网 时间:2024/05/16 17:58
我相信你们当中有很多人都知道 Windows Phone 上的开发是谁做的,但是其内部不支持压缩和解压缩。许多人都指向 CodePlex 上的 SharpCompress 项目,我最终也选择对它使用此方法。然而,其中缺少的重要一项就是介绍在手机上解压缩文件的任何文档。我为 winform 应用程序编写了大量代码,以使用 WriteAllToDirectory 方法在 Windows 上解压缩压缩的文件。但是,Windows Phone 的程序集中不存在此方法。此外,在将 Windows Phone 程序集用于 SharpCompress 时,您不能只提供可解压缩到的路径,而必须提供流对象。
此方法终于奏效了,但是操作过程与在 Windows 完整版上使用此方法时大不相同。下面是一些可帮助您完成此操作的小提示:
1. 枚举从 Reader 工厂中获取的 IReader 的条目集合:
//"sr" is a stream reader object from previous code that contains the byte array of the actual zip file
using (var reader = ReaderFactory.Open(sr))
{
while (reader.MoveToNextEntry())
{
//IsDirectory always returns false in everything I've seen so far
if (!reader.Entry.IsDirectory)
{
//process the entry, described next
}
else
{
//do not know of a scenario where we can end up here
Debug.WriteLine(reader.Entry.FilePath);
}
}
}
2. 为文件创建目录,然后获取可写入的 IsolatedStorageFileStream 实例:
//IsDirectory always returns false in everything I've seen so far
if (!reader.Entry.IsDirectory)
{
IsolatedStorageFileStream theFile = GetLocalFile(reader.Entry.FilePath);
}
private IsolatedStorageFileStream GetLocalFile(string fullPath)
{
IsolatedStorageFileStream fs = null;
try
{
//get isolated storage so we can create directories and decompress
IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
//trim everything from the last index of /, which is the file
//name, i.e. it will look like folder/folder/folder/filename.txt
string dirPath = fullPath.Substring(0, fullPath.LastIndexOf("/"));
//what's nice is you can create the full folder path in one call
if (!storage.DirectoryExists(dirPath))
storage.CreateDirectory(dirPath);
//now that all the directories exist, create a stream for the
// file – again, cool because you can just give the full path
fs = storage.CreateFile(fullPath);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
return fs;
}
3. 在拥有流后,就可以使用 WriteEntryTo 方法写出单个文件了:
if (!reader.Entry.IsDirectory)
{
IsolatedStorageFileStream theFile = GetLocalFile(reader.Entry.FilePath);
if (theFile != null)
reader.WriteEntryTo(theFile);
}
希望本文对您有所帮助,缺少文档是一件很麻烦的事情,因此我必须下一番苦功夫解决此问题。
这是一篇本地化的博客文章。请访问 Using Compression on Windows Phone 7.5 以查看原文
- 在 Windows Phone 7.5 上使用压缩
- windows phone:在Windows Phone上使用QQConnect OAuth2
- 在windows phone上画图
- 在Windows Phone 7上使用Silverlight Toolkit Chart
- Windows Phone 学习 LINQ在wp上的使用
- #Windows Phone:如何在Windows Phone App使用HTML5專案開發
- 在Windows Phone 7上使用Silverlight Toolkit Chart时遇到的错误
- 在Windows Phone上使用SQLite,并获取和编辑模拟器中的数据库文件
- 【技术直通车】在Windows Phone上使用SQLite,并获取和编辑模拟器中的数据库文件
- 如何在Windows XP上安装Windows Phone Developer Tools
- mysql压缩版在windows上配置
- 在windows phone 7上获取本地IP的方法
- 如何在Windows Phone 7上发送短信
- 如何在 Windows Phone 上执行页面导航
- 在 Windows Phone 上构建一个 Hello World 程序
- Windows上使用压缩包安装MYSQL
- 使用Windows Azure HDInsight Service在云上压缩大数据
- Windows Phone 使用FlurrySdk
- K近邻分类算法与kdTree实现的伪代码(构造kdTree 和 kdTree上搜索最近邻)
- Memcache的方法及其参数详解
- 在执行yiic webapp命令时,就报以下错误了: ‘"php.exe"’ 不是内部或外部命令,也不是可运行的程序或批处理文件。
- The birthday present
- 磁盘阵列卡
- 在 Windows Phone 7.5 上使用压缩
- 页面之间传值的几种方式
- 联想惠普争寰球PC第一:二研讨机构数据击架
- windows环境vlc-1.1.10编译过程
- smarty中的foreach循环
- Android 之 下拉框(Spinner)的使用
- 谁,又把青春消耗在暗恋里了呢?伤感的QQ空间日志
- tcpip详解笔记(19) persist定时器
- random_shuffle 和transform算法