.NET 4.5 中新提供的压缩类(转载)
Windows8 的开发已经如火如荼开始了,在 Windows8 中提供的 .NET Framework 已经更新到了 4.5 版,其中又增加了一些新的特性,对压缩文件的支持就是其中之一。
在 4.5 之前,处理压缩文件,我们经常需要使用第三方的类库 SharpZipLib, 现在可以直接实现了。
1.准备工作
首先做一下准备工作,需要确保你使用 .NET 4.5 版,可以在项目的属性窗口中检查一下。

然后,引用必须的程序集。

程序集有两个:System.IO.Compression 和 System.IO.Compression.FileSystem.
类似于对文件和目录的操作,对于压缩文件也提供了两种方式:ZipArchive 和 ZipFile,分别对应两个新增加的类 ZipArchive 和 ZipFile。这两个类都定义在命名空间 System.IO.Compression 中。
为了后面演示方便,我们定义一个表示压缩文件路径的常量。
const string zipFilePath = @"..\..\Sample.zip";
2. 使用 ZipArchive
先看ZipArchive的使用。
2.1 创建压缩文件
创建一个空的压缩文件,使用 ZipArchiveMode.Create 创建参数。
// 创建 Zip 文件
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Create))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Create))
{ }
使用 WinRaR 打开压缩文件,可以看到里面没有文件。

2.2 创建并添加文件
通常,在创建的同时,我们就会加入一些文件,下面的例子中,我们将当前的执行程序文件本身加到压缩文件中。

// 创建并添加被压缩文件
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Create))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Create))
{
System.Reflection.Assembly assemble = System.Reflection.Assembly.GetExecutingAssembly();
string path = assemble.Location;
string filename = System.IO.Path.GetFileName(path); ZipArchiveEntry readMeEntry = archive.CreateEntry(filename);
using (System.IO.Stream stream = readMeEntry.Open())
{
byte[] bytes = System.IO.File.ReadAllBytes(path);
stream.Write(bytes, 0, bytes.Length);
}
}

现在,打开压缩文件,可以看到文件已经被压缩进来了。

2.3 列出压缩文件内容
当然,也可以通过程序检查压缩文件的内容了。使用 Read 方式就可以了。

// 列出压缩压缩文件
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read))
{ foreach (var zipArchiveEntry in archive.Entries)
Console.WriteLine(
"FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName
);
}

2.4 提取压缩文件
当然可以从压缩文件中提取被压缩的内容了。

// 读取其中一个文件的内容
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Read))
{
// 解压某个文件
ZipArchiveEntry entry = archive.GetEntry("ZipArchiveSample.exe");
Console.WriteLine(entry.Name);
using (System.IO.Stream stream = entry.Open())
{
System.IO.Stream output = new FileStream("http://www.cnblogs.com/ZipArchiveSample.exe", FileMode.Create);
int b = -1;
while ((b = stream.ReadByte()) != -1)
{
output.WriteByte((byte) b);
}
output.Close();
} }

2.5 更新压缩文件
在压缩文件已经创建之后,还可以打开它,继续添加文件,这就称为更新了,使用 Update 模式。

// 向现有的压缩文件中添加文件
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update))
{
// 这里添加当前正在执行的程序文件本身
System.Reflection.Assembly assemble = System.Reflection.Assembly.GetExecutingAssembly();
string path = assemble.Location;
string filename = System.IO.Path.GetFileName( path); ZipArchiveEntry readMeEntry = archive.CreateEntry( filename );
using (System.IO.Stream stream = readMeEntry.Open() )
{
byte[] bytes = System.IO.File.ReadAllBytes(path);
stream.Write(bytes, 0, bytes.Length);
} foreach (var zipArchiveEntry in archive.Entries)
Console.WriteLine(
"FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName
);
}

现在压缩文件中又增加了一个,这可以一个怪异的文件,同一个文件被在压缩文件中添加了两次!

3. 使用 ZipFile
除了上边的基本方法之外,还有一些简单的使用方法。这涉及到另外一个类:ZipFile。
3.1 创建空压缩文件

// 删除压缩文件
System.IO.File.Delete(zipFilePath); // 使用 ZipFile 的静态方法创建压缩文件,要保证文件没有存在
using (ZipArchive zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
{
}

3.2 创建并添加文件
直接添加一个文件的方法。直接使用 CreateEntryFromFile 就可以了。

System.IO.File.Delete(zipFilePath); // 使用 CreateEntryFromFile 方法添加文件
// 使用 ZipFile 的静态方法创建压缩文件
using (ZipArchive zipArchive = ZipFile.Open(zipFilePath, ZipArchiveMode.Create))
{
System.Reflection.Assembly assemble = System.Reflection.Assembly.GetExecutingAssembly();
string path = assemble.Location;
string filename = System.IO.Path.GetFileName(path); zipArchive.CreateEntryFromFile(path, filename);
}

3.3 解压文件
将压缩文件解压到指定的目录中。
// 解压文件
ZipFile.ExtractToDirectory(zipFilePath, "../..");
3.4 压缩一个目录
还可以直接将一个目录中所有的文件都压缩到一个压缩文件中。
// 压缩指定目录中所有文件
System.IO.File.Delete(zipFilePath);
ZipFile.CreateFromDirectory(".", zipFilePath);
你是不是最喜欢这个方法?现在压缩文件中的文件更多了。

附录:
SharpZipLib 的下载地址: http://www.icsharpcode.net/OpenSource/SharpZipLib/
.NET 4.5 中新提供的压缩类(转载)的更多相关文章
- .NET 4.5 中新提供的压缩类
Windows8 的开发已经如火如荼开始了,在 Windows8 中提供的 .NET Framework 已经更新到了 4.5 版,其中又增加了一些新的特性,对压缩文件的支持就是其中之一. 在 4.5 ...
- 夯实Java基础(十四)——Java8新的日期处理类
1.前言 Java8之前处理日期一直是Java程序员比较头疼的问题,从Java 8之后,Java里面添加了许多的新特性,其中一个最常见也是最实用的便是日期处理的类——LocalDate.LocalDa ...
- UWP中新加的数据绑定方式x:Bind分析总结
UWP中新加的数据绑定方式x:Bind分析总结 0x00 UWP中的x:Bind 由之前有过WPF开发经验,所以在学习UWP的时候直接省略了XAML.数据绑定等几个看着十分眼熟的主题.学习过程中倒是也 ...
- Linux 2.6内核中新的锁机制--RCU
转自:http://www.ibm.com/developerworks/cn/linux/l-rcu/ 一. 引言 众所周知,为了保护共享数据,需要一些同步机制,如自旋锁(spinlock),读写锁 ...
- HTML5中新添加事件
HTML5中新添加了很多事件,但是由于他们的兼容问题不是很理想,应用实战性不是太强,所以在这里基本省略,咱们只分享应用广泛兼容不错的事件,日后随着兼容情况提升以后再陆续添加分享.今天为大家介绍的事件主 ...
- 关于新中新二代身份证读卡器DKQ-A16D的一些问题
今天拿到了新中新DKQ-A16D,随机光盘里有以下文件: 我遇到的问题是,如果直接打开\二代征SDK开发包\DLL\测试程序\C#_2008\WindowsFormsApplication1\目录下的 ...
- Java 8 中新的 Date 和 Time 类入门详解
这篇文章主要是java8中新的Date和Time API的实战.新的Date和Time类是java开发者社区千呼万唤始出来的.Java8 之前存在的Date类一直都受人诟病,很多人都会选择使用第三方的 ...
- pivot 与 unpivot 函数是SQL05新提供的2个函数
pivot 与 unpivot 函数是SQL05新提供的2个函数 ----------------------------------------------------------------- ...
- MySQL 8 中新的复制功能
MySQL 8 中新的复制功能使得操作更加方便,并帮助用户更好地观察复制过程中内部发生的情况. 使用 MySQL 5.7.17 获取 MySQL 组复制插件是一项巨大的工作.组复制是一个新的插件,通过 ...
随机推荐
- 【原创】SAP/Oracle 集团企业海外全球化实施注意事项: 一带一路本地化 (持续更新)
ABC集团SAP的系统平台已经扩展到全球一百来个国家和地区,SAP系统平台的全球实施项目中, 当时是需要支持当地的业务和法律法规的合规要求. 当时客户也是缺乏当地的资源以及对当地法律法规和业务实践的了 ...
- springboot-async
在项目中,当访问其他人的接口较慢或者做耗时任务时,不想程序一直卡在耗时任务上,想程序能够并行执行, 我们可以使用多线程来并行的处理任务,也可以使用spring提供的异步处理方式@Async. Spri ...
- Centos7.0下Nexus私服搭建
1.下载nexus wget https://sonatype-download.global.ssl.fastly.net/nexus/oss/nexus-2.11.2-03-bundle.tar. ...
- The client and server cannot communicate, because they do not possess a common algorithm
The client and server cannot communicate, because they do not possess a common algorithm This was re ...
- 蜕变成蝶~Linux设备驱动之按键设备驱动
在上述的驱动系列博客中,我们已经了解了关于阻塞和非阻塞.异步通知.轮询.内存和I/O口访问.并发控制等知识,按键设备驱动相对来说是比较简单的,本章内容可以加深我们对字符设备驱动架构.阻塞与非阻塞.中断 ...
- linux shell的here document用法(cat << EOF)
什么是Here Document?Here Document 是在Linux Shell 中的一种特殊的重定向方式,它的基本的形式如下cmd << delimiter Here Docu ...
- vs2008 使用百度编辑器
准备工作 百度编辑器官方下载,并将文件放到项目根目录下. 因为vs2008 只到Framework 3.5,所以需要将4.0的东西去掉. 1)下载.net framework 3.5版的 Newton ...
- centOS 安装gitlab-runner
1. curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sud ...
- MySQL练习题2
以下操作均在MySQL5.7数据库上实验无误 需要四张表 Student_new(Sid,Sname,Sage,Ssex)学生表 Sid:学号 Sname:学生姓名 Sage:学生年龄 Ssex:学生 ...
- .NET Core开发日志——OData
简述 OData,即Open Data Protocol,是由微软在2007年推出的一款开放协议,旨在通过简单.标准的方式创建和使用查询式及交互式RESTful API. 类库 在.NET Core中 ...