在 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 以查看原文

            原创粉丝点击