ZipArchive For Windows 8 Apps

工作需求,整理:

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.

转载于:https://www.cnblogs.com/denjuy/archive/2012/11/27/2790760.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值