浅谈 ZipArchive 类

Microsoft .NET Framework 4.5 新增了 ZipArchive 类

Microsoft Windows 8 Consumer Preview 操作系统已经内置了 Microsoft .NET Framework 4.5,它新增了对 Zip 文件的支持,这是通过 System.IO.Compression 命名空间中新增的 ZipArchive、ZipFile 等类实现的。注意,Microsoft .NET Framework 2.0 中已经有的 GZipStream、DeflateStream 类只能处理单一的 Zip 流,不支持包含多个文件的 Zip 压缩包。

创建 Zip 压缩包

创建 Zip 压缩包非常简单,如下 ZipCreater.cs 所示:

复制代码
 1 using System;
 2 using System.IO.Compression;
 3 
 4 namespace Skyiv.Tester
 5 {
 6   sealed class ZipCreater
 7   {
 8     static void Main()
 9     {
10       using (var zip = ZipFile.Open("ZipCreater.zip", ZipArchiveMode.Create))
11       {
12         zip.CreateEntryFromFile(@"C:\work\ZipCreater.cs", "ZipCreater.cs");
13         zip.CreateEntryFromFile("ZipCreater.exe", "ZipCreater.exe");
14       }
15     }
16   }
17 }
复制代码

上述程序中,ZipFile 类的 Open 方法返回 类型为 ZipArchive 的变量 zip,然后调用 ZipArchvie 类的扩展方法 CreateEntryFromFile 来把文件加入到 Zip 压缩包中,该扩展方法在 ZipFileExtensions 类中定义。在 Microsoft Windows 8 Consumer Preview 操作系统中编译和运行:

C:\work> C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc ZipCreater.cs -r:System.IO.Compression.dll -r:System.IO.Compression.FileSystem.dll
Microsoft (R) Visual C# Compiler version 4.0.30319.17379
for Microsoft (R) .NET Framework 4.5
版权所有 (C) Microsoft Corporation。保留所有权利。

C:\work> ZipCreater

该程序的运行结果是生成一个如下所示的 Zip 压缩包 ZipCreater.zip:

ZipFile

将压缩包解压到指定目录

我们来看看 Unzip.cs:

复制代码
 1 using System;
 2 using System.IO.Compression;
 3 
 4 namespace Skyiv.Utils
 5 {
 6   sealed class Unzip
 7   {
 8     static void Main(string[] args)
 9     {
10       if (args.Length != 2)
11       {
12         Console.WriteLine("Usage: Unzip zip-file-name directory-name");
13         return;
14       }
15       try { ZipFile.ExtractToDirectory(args[0], args[1]); }
16       catch (Exception ex) { Console.Error.WriteLine(ex.Message); }
17     }
18   }
19 }
复制代码

上述程序的核心内容是第 15 行使用 ZipFile 类的静态方法 ExtractToDirectory 把压缩包中的所有文件解压到指定目录。编译和运行的结果如下所示:

C:\work> C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc Unzip.cs -r:System.IO.Compression.FileSystem.dll
Microsoft (R) Visual C# Compiler version 4.0.30319.17379
for Microsoft (R) .NET Framework 4.5
版权所有 (C) Microsoft Corporation。保留所有权利。

C:\work> Unzip ZipCreater.zip ZipCreater

将指定目录中的所有文件打包到压缩包中

再看看下面的 Zip.cs:

复制代码
 1 using System;
 2 using System.IO.Compression;
 3 
 4 namespace Skyiv.Utils
 5 {
 6   sealed class Zip
 7   {
 8     static void Main(string[] args)
 9     {
10       if (args.Length != 2)
11       {
12         Console.WriteLine("Usage: Zip zip-file-name directory-name");
13         return;
14       }
15       try { ZipFile.CreateFromDirectory(args[1], args[0]); }
16       catch (Exception ex) { Console.Error.WriteLine(ex.Message); }
17     }
18   }
19 }
复制代码

上述程序的核内容是第 15 行使用 ZipFile 类的静态方法 CreateFromDirectory 把指定目录中的所有文件打包成一个压缩包。编译和运行的结果如下所示:

C:\work> C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc Zip.cs -r:System.IO.Compression.FileSystem.dll
Microsoft (R) Visual C# Compiler version 4.0.30319.17379
for Microsoft (R) .NET Framework 4.5
版权所有 (C) Microsoft Corporation。保留所有权利。

C:\work> Zip ZipCreater.2.zip ZipCreater

直接读取网络上的压缩包的内容

我把前面生成的 ZipCreater.zip 文件上传到博客园,然后写一个程序直接从博客园的服务器上读取该压缩包中的 ZipCreater.cs 文件的内容显示在屏幕上:

复制代码
 1 using System;
 2 using System.IO;
 3 using System.IO.Compression;
 4 using System.Net;
 5 
 6 namespace Skyiv.Test
 7 {
 8   static class ZipTester
 9   {
10     static void Main()
11     {
12       using (var zip = new ZipArchive(new Uri(
13         "http://files.cnblogs.com/skyivben/ZipCreater.zip").GetHttpStream()))
14         zip.GetEntry("ZipCreater.cs").Open().CopyTo(Console.OpenStandardOutput());
15     }
16     
17     static Stream GetHttpStream(this Uri uri)
18     {
19       return ((HttpWebResponse)((HttpWebRequest)WebRequest.Create(uri)).GetResponse()).GetResponseStream();
20     }
21   }
22 }
复制代码

上述程序中:

  • 第 13 行通过调用在第 17 到 20 行定义的 GetHttpStream 扩展方法得到一个网络流,然后使用该网络流来构造一个 ZipArchive 类。
  • 第 14 行调用 ZipArchive 类的 GetEntry 方法得到一个 ZipArchiveEntry 类,然后调用 ZipArchiveEntry 类的 Open 方法就得到所需要的 Stream 类,直接调用Stream 类的 CopyTo 方法写入标准输出。

这个程序的运行结果如下所示:

ZipTester

注意,上述程序仅仅是从网络上读取所需的内容,并没有下载整个压缩包,更没有在本地硬盘上创建临时文件。

以上这些都是 Microsoft .NET Framework 4.5 自身就有的功能,没有使用第三方法的类库,比如 SharpZipLib 之类的东东。

遗憾的是,Microsoft .NET Framework 4.5 只能在 Windows 7、Windows 8、Windows Server 2008 和 Windows Server 8 等操作系统上安装。更早的 Windows 操作系统无法安装 Microsoft .NET Framework 4.5。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值