windows phone使用sharpcompress进行解压压缩文件

时间:2023-03-09 20:19:18
windows phone使用sharpcompress进行解压压缩文件

在做移动端时,当我们需要从服务器获得多个文件时,为了节约流量,服务器一般会返回一个压缩包,那我们就是下载完成后,在手机中进行解压到指定位置

SharpCompress就是可以在手机中进行解压一个类库(.net),在codeplex是开源,支持桌面端和移动端

点击下载最新版本       查看支持内容      API使用示例

下面我们看一下在windows phone中使用其进行解压ZIP包

        public async void DownloadAndUnCompress()
{
string strUrl = "http://qd.baidupcs.com/file/3ebe6e988584733fba61c04e9568bc73?fid=3860865449-250528-923682234289515&time=1401003314&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-sDvdhrr7p1EWhMUFNOaYlQbwLmE%3D&to=qb&fm=Q,B,U,nc&newver=1&expires=1401003914&rt=sh&r=347257022&logid=3996325361&sh=1&vuk=3860865449&fn=SharpCompress0.10.3.zip"; await Task.Run(() =>
{
WebClient wc = new WebClient();
wc.OpenReadAsync(new Uri(strUrl, UriKind.Absolute));
wc.DownloadProgressChanged += (s, dpce) =>
{
System.Diagnostics.Debug.WriteLine("已下载" + dpce.ProgressPercentage + "%");
}; wc.OpenReadCompleted += (s, orce) =>
{
if (orce.Error != null)
{
System.Diagnostics.Debug.WriteLine("下载出错");
return;
} using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
var zipArchive = ArchiveFactory.Open(orce.Result);//创建一个解压器
foreach (var entry in zipArchive.Entries)//提取里面每个实体,包括文件夹都算实体
{
//如果为文件夹先把文件夹创建出来
if (entry.IsDirectory)
{
if (!storage.DirectoryExists(entry.FilePath))
{
storage.CreateDirectory(entry.FilePath);
}
continue;
}
//entry.FilePath 为文件的全路径
using (IsolatedStorageFileStream isofilestream = storage.CreateFile(entry.FilePath))
{
//写入到独立存储空间中
entry.WriteTo(isofilestream);
}
}
zipArchive.Dispose();
System.Diagnostics.Debug.WriteLine("解压完成");
}
};
});
}

其它的如RAR、Tar、GZip、7Zip可参考codeplex官方文档