ZipArchive For Windows 8 Apps

來源:互聯網
上載者:User

工作需求,整理:

ZipHelper.cs

 1     /// <summary> 2     /// Compression & decompression for a single file. 3     /// </summary> 4     public static class ZipHelper 5     { 6         /// <summary> 7         /// Compress a single file to a zipFile. 8         /// </summary> 9         /// <param name="srcFileName">the name of the specified source file</param>10         /// <param name="srcFilePath">the path of the specified source file</param>11         /// <param name="zipFilePath">the path of the specified zip file</param>12         public static void Compressing(string srcFileName,string srcFilePath, string zipFilePath)13         {14             var zipStream = FileHelper.OpenFile(zipFilePath).Result.OpenStreamForWriteAsync().Result;15             var srcFile = FileHelper.OpenFile(srcFilePath).Result;16             var streams = srcFile.OpenStreamForReadAsync().Result;17             var buffers =new byte[streams.Length];18             streams.Read(buffers, 0, buffers.Length);19             using (zipStream)20             {21                 using (var zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create))22                 {23                     var entry = zipArchive.CreateEntry(srcFileName);24                     using (var stream=entry.Open())25                     {26                         var bytes = buffers;27                         stream.Write(bytes,0,bytes.Length);28                     }29                 } 30             }31         }32 33 34         /// <summary>35         /// 解壓單個檔案36         /// </summary>37         /// <param name="zipFilePath">壓縮檔路徑</param>38         /// <param name="desFilePath">存放檔案路徑</param>39         public  static  void Decompressing(string zipFilePath,string desFilePath)40         {41             var zipStream = FileHelper.OpenFileStream(zipFilePath, FileAccessMode.ReadWrite).Result.AsStream();42             var desFileStream = FileHelper.OpenFileStream(desFilePath, FileAccessMode.ReadWrite).Result.AsStream();43             using (var zipArchive=new ZipArchive(zipStream,ZipArchiveMode.Read))44             {45                 ZipArchiveEntry entry = zipArchive.Entries[0];46                 using (var stream=entry.Open())47                 {48                     while (stream.ReadByte()!=-1)49                     {50                         desFileStream.WriteByte((byte)stream.ReadByte());51                     }52                 }53             }54             desFileStream.Dispose();55         }56     }
View Code

方法內部適當調整,可用於Windows Phone.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.