• WP7 ZIP 压缩与解压缩


    今天我要做一个简单的 WP7 下的 zip 压缩一个文件与解压缩一个文件 如图:

    主要任务是将一个XML 文件压缩,然后在解压缩出来!

    WP7 平台下有一个开源工具 http://slsharpziplib.codeplex.com/

    这个开源工具已经支持大部分的压缩标准,其它标准可以看他的示例

    压缩代码

    private void Button_Zip_Tap(object sender, GestureEventArgs e)
            {
                try
                {
                    DelFile(SavePathZip);
                    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
                    {
                        using (var streamOut = new ZipOutputStream(store.OpenFile(SavePathZip, FileMode.Create)))
                        {
                            streamOut.SetLevel(9);
                            using (var streamIn = (App.GetResourceStream(new Uri(FilePath, UriKind.Relative)).Stream))
                            {
                                string newName = ZipEntry.CleanName(FilePath);
                                ZipEntry newEntry = new ZipEntry(newName);//必须设置此对象才能压缩否则报错
                                newEntry.Size = streamIn.Length;
                                newEntry.DateTime = DateTime.Now;
                                streamOut.PutNextEntry(newEntry);
                                streamIn.CopyTo(streamOut);
                                streamOut.Flush();
                                streamOut.Finish();
                                text.Text = "压缩成功!" + "     压缩率:" + ((float)streamOut.Length / (float)streamIn.Length) * 100 + "%";
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    text.Text = "压缩失败!" + ex.Message;
                }
            }
    

    解压缩代码

      

     try
    {
    DelFile(UnZipFilePath);
    using (var store = IsolatedStorageFile.GetUserStoreForApplication())
    {
    using (ZipInputStream streamIn = new ZipInputStream(store.OpenFile(SavePathZip, FileMode.Open)))
    {
    ZipEntry theEntry = streamIn.GetNextEntry();//必须调用这个才能 Read

    if (theEntry != null)
    {
    string fileName = theEntry.Name;
    if (fileName != String.Empty)
    {
    text.Text = new StreamReader(streamIn).ReadToEnd();
    }
    }
    }
    }
    }
    catch (Exception ex)
    {
    text.Text = "解压缩失败!" + ex.Message;
    }
    }

    做这个时候有个小问题就是TextBlock 不能完全显示 解压缩的文件,起初我还以为是读取的字节流有问题,结果发现问题在TextBlock 

    #WP7 TextBlock 问题和解决办法#目前WP7上TextBlock空间的宽和高上限为2048px,超过这个数值的内容将被截断。由于高宽有上限,所以TextBlock显示的字符长度还与字体大小有关。解决办法为截取字符然后在一个或多个TextBlock上显示。

    当然上面的例子为单个文件的压缩与解压缩!

    如果要做文件 的压缩 就要自己写个循环 压缩子文件夹下的所有文件

    源码下载:http://vdisk.weibo.com/s/1B9Y1/1324449062

  • 相关阅读:
    yk20192320
    JS常用方法 限制用户输入的方法
    话说最强悍的团队,博客园团队!
    输入框打开禁用自动填充功能
    老生常谈:Asp.net MVC 3+ Jquery UI Autocomplete实现百度效果
    节日logo
    第一篇:Asp.net MVP模式介绍
    VBS 类
    《JavaScript编程精解》简明读书心得上
    Three.js源码阅读笔记1
  • 原文地址:https://www.cnblogs.com/piaocz/p/2295805.html
Copyright © 2020-2023  润新知