wp7平台zip压缩处理

最近在wp7平台做一个push email 的客户端,通过socket连接接受下来的邮件内容是一个zip压缩包,需要在客户端解压处理。

windows phone 7 没有zip相关的API支持,最后找到一个开源的zip lib包slsharpziplib

地址是:http://slsharpziplib.codeplex.com

解压的代码

 1   /// <summary>
2 /// 解压zip包
3 /// </summary>
4 /// <param name="excludeFiles">压缩包中不需要解压处理的文件</param>
5 /// <param name="directoryName">解压到的目标路径</param>
6 /// <param name="sourceFile">待解压的zip包在isolatedStorage中的文件路径</param>
7 /// <returns></returns>
8 public bool UnZip(List<String> excludeFiles, string directoryName, string sourceFile = ReceiveMailDataState.MailDataTempFileName)
9 {
10 bool ret = true;
11 try
12 {
13 IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
14 using (IsolatedStorageFileStream fileStream = file.OpenFile(sourceFile, FileMode.Open, FileAccess.Read))
15 {
16 ZipInputStream s = new ZipInputStream(fileStream);
17 // StreamReader reader = new StreamReader(s);
18 BinaryReader reader = new BinaryReader(s);
19 ZipEntry theEntry;
20 while ((theEntry = s.GetNextEntry()) != null)
21 {
22 string fileName = theEntry.Name;
23 if (fileName != String.Empty)
24 {
25 //如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入
26 if (theEntry.CompressedSize == 0)
27 continue;
28 if (!theEntry.IsFile)
29 continue;
30 else if (!theEntry.IsCompressionMethodSupported())
31 {
32 continue;
33 }
34 else if (!theEntry.CanDecompress)
35 {
36 continue;
37 }
38 if (excludeFiles != null && excludeFiles.Contains(fileName))
39 continue;
40
41 String fullFileName = directoryName + PathSeperater + fileName;
42
43 //Ensure path exist
44 String path = fullFileName.Substring(0, fullFileName.LastIndexOf(PathSeperater));
45 if (!file.DirectoryExists(path))
46 {
47 file.CreateDirectory(path);
48 }
49
50 using (IsolatedStorageFileStream stream = file.CreateFile(fullFileName))
51 {
52 BinaryWriter writer = new BinaryWriter(stream);
53 int size = 2048;
54 byte[] data = new byte[2048];
55 while (true)
56 {
57 size = reader.Read(data, 0, data.Length);
58 if (size > 0) { writer.Write(data, 0, size); }
59 else { break; }
60 }
61 writer.Close();
62 stream.Close();
63 }
64 }
65 }
66 s.Close();
67 }
68 }
69 catch (Exception ex) {
70 Debug.WriteLine(ex.Message);
71 ret = false;
72 }
73 return ret;
74 }

解压过程的时候抛出异常

Library cannot extract this entry. Version required is (788).


解决办法:

在slsharpziplib for wp7的源代码中找到ZipEntry类,找到property Version,修改如下,取Version的低字节

public int Version
{
get
{
// Return recorded version if known.
if (_versionToExtract != 0)
{
return _versionToExtract & 0x00ff;
}

至于修改的原因,跟zip格式的结构有关,我将翻译一篇相关资料来解释,敬请期待。

修改完以后重新编译和使用这个包就解压成功了。

在cmd命令行中输入ISETool.exe ts de 23833d6c-1446-4097-a7f6-358c3294d9a3 "C:\Temp"

ISETool命令的用法请看我的文章IsolatedStorage工具使用介绍

把解压后的文件拷贝到C:\Temp目录下,可以看到原来的zip文件解压了

2011/12/02 13:01 <DIR> htmls
2011/12/02 13:01 <DIR> images
2011/12/02 13:01 472 index.xml
2011/12/02 13:01 <DIR> mimes

 

 

xiaobai

devxiaobai@gamil.com

posted on 2011-12-02 13:37  NN小白  阅读(1813)  评论(5编辑  收藏  举报