The System.IO.Compression namespace contains the following types of basic file and stream compression and decompression services, and these types can be used to read and modify the contents of compressed files, mainly in the following categories:
ZipFile
Ziparchive
Ziparchiveentry
Deflatestream
GZipStream
1. Use the ZipFile class to create and extract a compressed file with a. zip file name extension: The System.IO.Compression.FileSystem assembly must be referenced .
Using System;
Using System.IO;
Using System.IO.Compression;
Namespace Consoleapplication
{
class program
{
static void Main (string[] args)
{
string Startpath = @ "C:\example\start";
String zippath = @ "C:\example\result.zip";
String extractpath = @ "C:\example\extract";
Zipfile.createfromdirectory (Startpath, zippath);
Zipfile.extracttodirectory (Zippath, Extractpath);
}
}
2. Repeatedly archive and extract files with a. txt extension from the contents of the existing. zip file. It uses the Ziparchive class to access the existing. zip file and the Ziparchiveentry class to check each item of the compressed file, and you must reference the project's
System.IO.Compression.FileSystemAssembly.
Using System;
Using System.IO;
Using System.IO.Compression;
Namespace ConsoleApplication1
{
class program
{
static void Main (string[] args)
{
string Zippath = @ "C:\example\start.zip";
String extractpath = @ "C:\example\extract";
using (ziparchive archive = Zipfile.openread (Zippath))
{
foreach (Ziparchiveentry entry in archive.) Entries)
{
if (entry). Fullname.endswith (". txt", stringcomparison.ordinalignorecase))
{
entry. Extracttofile (Path.Combine, Extractpath, entry. FullName));}}}
3. Use the Ziparchive class to access the existing. zip file, and then add a new file to the compressed file. When you add to an existing. zip file, the new file gets the compression.
Namespace Consoleapplication
{
class program
{
static void Main (string[] args)
{
using ( FileStream Ziptoopen = new FileStream (@ "C:\users\exampleuser\release.zip", FileMode.Open))
{
using ( Ziparchive archive = new Ziparchive (Ziptoopen, ziparchivemode.update))
{
Ziparchiveentry readmeentry = Archive . Createentry ("Readme.txt");
using (StreamWriter writer = new StreamWriter (Readmeentry.open ()))
{
writer. WriteLine ("Information about this package.");
Writer. WriteLine ("========================");}}}
4. Compression and decompression of data using GZipStream and Deflatestream classes. They use the same compression algorithm. A compressed GZipStream object that writes a file extension of. GZ can be decompressed by using many common tools, in addition to the methods provided by GZipStream
public class Program {private static string directorypath = @ "C:\Temp";
public static void Main () {DirectoryInfo directoryselected = new DirectoryInfo (directorypath);
Compress (directoryselected); foreach (FileInfo filetodecompress in Directoryselected.getfiles ("*.gz")) {Decompress (Filetod
ecompress); } public static void Compress (DirectoryInfo directoryselected) {foreach (FileInfo Filetocompress in Directoryselected.getfiles ()) {using (FileStream Originalfilestream = Filet
Ocompress.openread ()) {if (File.getattributes (filetocompress.fullname) &
Fileattributes.hidden)!= Fileattributes.hidden & Filetocompress.extension!= ". Gz") {using (FileStream Compressedfilestream = File.create (filetocompress.fulLName + ". Gz")) {using (GZipStream Compressionstream = new GZipStream
(Compressedfilestream, compressionmode.compress))
{Originalfilestream.copyto (Compressionstream); } FileInfo info = new FileInfo (directorypath + "\" + Filetocompress.nam
E + ". gz"); Console.WriteLine ("Compressed {0} from {1} to {2} bytes.", Filetocompress.name, Filetocompress.len Gth. ToString (), info.
Length.tostring ());
}}} public static void decompress (FileInfo filetodecompress) { using (FileStream Originalfilestream = Filetodecompress.openread ()) {string Curren
Tfilename = Filetodecompress.fullname; String NewFileName= Currentfilename.remove (currentfilename.length-filetodecompress.extension.length); using (FileStream Decompressedfilestream = File.create (NewFileName)) {using (gzipstre
Am Decompressionstream = new GZipStream (Originalfilestream, compressionmode.decompress)) {
Decompressionstream.copyto (Decompressedfilestream);
Console.WriteLine ("decompressed: {0}", filetodecompress.name); }
}
}
}
http://blog.csdn.net/allsharps/article/details/7357328