最近在wp7平台做一个push email 的客户端,通过socket连接接受下来的邮件内容是一个zip压缩包,需要在客户端解压处理。
windows phone 7 没有zip相关的API支持,最后找到一个开源的zip lib包slsharpziplib
地址是:http://slsharpziplib.codeplex.com
解压的代码
/// <summary>
/// 解压zip包
/// </summary>
/// <param name="excludeFiles">压缩包中不需要解压处理的文件</param>
/// <param name="directoryName">解压到的目标路径</param>
/// <param name="sourceFile">待解压的zip包在isolatedStorage中的文件路径</param>
/// <returns></returns>
public bool UnZip(List<String> excludeFiles, string directoryName, string sourceFile = ReceiveMailDataState.MailDataTempFileName)
{
bool ret = true;
try
{
IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
using (IsolatedStorageFileStream fileStream = file.OpenFile(sourceFile, FileMode.Open, FileAccess.Read))
{
ZipInputStream s = new ZipInputStream(fileStream);
// StreamReader reader = new StreamReader(s);
BinaryReader reader = new BinaryReader(s);
ZipEntry theEntry;
while ((theEntry = s.GetNextEntry()) != null)
{
string fileName = theEntry.Name;
if (fileName != String.Empty)
{
//如果文件的压缩后大小为0那么说明这个文件是空的,因此不需要进行读出写入
if (theEntry.CompressedSize == 0)
continue;
if (!theEntry.IsFile)
continue;
else if (!theEntry.IsCompressionMethodSupported())
{
continue;
}
else if (!theEntry.CanDecompress)
{
continue;
}
if (excludeFiles != null && excludeFiles.Contains(fileName))
continue;
String fullFileName = directoryName + PathSeperater + fileName;
//Ensure path exist
String path = fullFileName.Substring(0, fullFileName.LastIndexOf(PathSeperater));
if (!file.DirectoryExists(path))
{
file.CreateDirectory(path);
}
using (IsolatedStorageFileStream stream = file.CreateFile(fullFileName))
{
BinaryWriter writer = new BinaryWriter(stream);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = reader.Read(data, 0, data.Length);
if (size > 0) { writer.Write(data, 0, size); }
else { break; }
}
writer.Close();
stream.Close();
}
}
}
s.Close();
}
}
catch (Exception ex) {
Debug.WriteLine(ex.Message);
ret = false;
}
return ret;
}
解压过程的时候抛出异常
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
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2020/05/16 03:41