【转】如何使用.NET 4.5中的ZipArchive
今天 在 http://www.infoq.com/cn/news/2012/06/Net-Zip 看到 .NET 4.5中提供的压缩类,在此做下备份。
http://dotnetzip.codeplex.com/
One of the missing feature of .NET framework was a support for Zip file manipulation such as reading the zip archive, adding files, extracting files, etc. and we were using some third party libraries such as excellent the DotNetZip. In .NET 4.5, we have an extensive support for manipulating .zip files.
First thing that you should do is to add System.IO.Compression assembly as reference to your project. You may also want to reference System.IO.Compression.FileSystem assembly to access three extension methods (from theZipFileExtensions class) for the ZipArchive class: CreateEntryFromFile, CreateEntryFromFile, and ExtractToDirectory. These extension methods enable you to compress and decompress the contents of the entry to a file.
Let’s cover the bits and pieces that we get from System.IO.Compression assembly at first. The below sample shows how to read a zip archive easily with ZipArchive class:
static void Main(string[] args) {
const string zipFilePath = @"C:\apps\Sample Pictures.zip";
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 );
}
}
In this sample, we are opening the zip archive and iterate through the collection of entries. When we run the application, we should see the list of files inside the zip archive:
It’s also so easy to add a new file to the zip archive:
static void Main(string[] args) {
const string zipFilePath = @"C:\apps\Sample Pictures.zip";
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update)) {
ZipArchiveEntry readMeEntry = archive.CreateEntry("ReadMe.txt");
using (StreamWriter writer = new StreamWriter(readMeEntry.Open())) {
writer.WriteLine("Lorem ipsum dolor sit amet...");
writer.Write("Proin rutrum, massa sed molestie porta, urna...");
}
foreach (var zipArchiveEntry in archive.Entries)
Console.WriteLine(
"FullName of the Zip Archive Entry: {0}", zipArchiveEntry.FullName
);
}
}
In this sample, we are adding a file named ReadMe.txt at the root of archive and then we are writing some text into that file.
Extracting files is into a folder is so easy as well. You need reference the System.IO.Compression.FileSystem assembly along with System.IO.Compression assembly as mentioned before for this sample:
static void Main(string[] args) {
const string zipFilePath = @"C:\apps\Sample Pictures.zip";
const string dirToExtract = @"C:\apps\Sample Pictures\";
using (FileStream zipFileToOpen = new FileStream(zipFilePath, FileMode.Open))
using (ZipArchive archive = new ZipArchive(zipFileToOpen, ZipArchiveMode.Update))
archive.ExtractToDirectory(dirToExtract);
}
There are some other handy APIs as well but it is so easy to discover them by yourself. Enjoy