





















2011-11-25 13:36 无名365 阅读(392) 评论() 收藏 举报
This lesson is very easy. This lesson focuses on how to compress and decompress files programmatically using .NET Framework and C# -or any language that supports .NET of course.-
Currently .NET Framework supports two types of compression algorithms:
For a complete GZIP reference, see RFC 1952 (GZIP File Format Specification).
The most powerful compression tool now is WinRAR.
Fortunately, whether using Deflate or GZIP in .NET, code is the same; the only thing that needs to change is the class name.
Deflate and GZIP can be found in the namespace System.IO.Compression that resides on assembly System.dll. This namespace contains only three types, the two algorithm implementation classes DeflateStream and GZipStream -both inherit directly from System.IO.Stream class-, and an enumeration CompressionMode that defines the operation (compression or decompression).
The code for compression is very simple:
Code snippets in this lesson rely on the fact that you added two
using(Importsin VB) statements,System.IOandSystem.IO.Compression.
Collapse | Copy Code
string fileToBeCompressed =
"D:My Great Word Document.doc";
string zipFilename =
"D:CompressedGreatDocument.zip";
using (FileStream target =
new FileStream(zipFilename,
FileMode.Create, FileAccess.Write))
using (GZipStream alg =
new GZipStream(target, CompressionMode.Compress))
{
byte[] data = File.ReadAllBytes(fileToBeCompressed);
alg.Write(data, 0, data.Length);
alg.Flush();
}
If you are going to compress a file, then you must specify the CompressionMode.Compress option, and also you must specify a Stream that the data will be written to.
After creating the class, you can compress the data by using its Write() method.
If you intended to use the Deflate algorithm, just change the class name to
DeflateStream.
The code that decompresses a compressed file is very similar:
Collapse | Copy Code
string compressedFile =
"D:CompressedGreatDocument.zip";
string originalFileName =
"D:My Great Word Document.doc";
using (FileStream zipFile =
new FileStream(compressedFile,
FileMode.Open, FileAccess.Read))
using (FileStream originalFile =
new FileStream(originalFileName,
FileMode.Create, FileAccess.Write))
using (GZipStream alg =
new GZipStream(zipFile, CompressionMode.Decompress))
{
while (true)
{
byte[] buffer = new byte[100];
int bytesRead = alg.Read(buffer, 0, buffer.Length);
originalFile.Write(buffer, 0, returnedBytes);
if (bytesRead != buffer.Length)
break;
}
}
First, we create a file stream to read the ZIP file, and then we created our algorithm class that encapsulates the file stream for decompressing it.
Then we created a file stream for the original file, for writing the decompressed data.
After that, we read the data compressed 100bytes after each other -you may prefer more- and write this data to the original file stream.
By encapsulating disposable objects into using statements, you become assured that every object will be disposed in a certain point -end of the using statement- and no object will retain in memory.
From http://www.codeproject.com/Articles/66269/Programmatically-Compress-and-Decompress-Files.aspx
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。