System. Io. Compression namespace
Note: This namespace is added in. NET Framework 2.0.
The system. Io. Compression namespace contains classes that provide basic stream compression and decompression services.
(Original downmoon)
Class description
Deflatestream provides methods and attributes used to compress and decompress streams using the deflate algorithm.
Gzipstream provides methods and attributes for compressing and extracting streams.
Enumeration description
Compressionmode specifies whether to compress or decompress the basic stream.
The following uses gzipstream as an example to describe
Note: This class is added in. NET Framework 2.0.
Provides methods and attributes for compressing and extracting streams.
Namespace: system. Io. Compression
Assembly: system (in system. dll)
Syntax
Visual Basic (Declaration)
Public class gzipstream
Inherits stream
Visual Basic (usage)
Dim instance as gzipstream
C #
Public class gzipstream: Stream
C ++
Public ref class gzipstream: Public stream
J #
Public class gzipstream extends stream
JScript
Public class gzipstream extends stream
Remarks
This class represents the gzip data format, which uses industry-standard algorithms for lossless compression and decompression of files. This format includes a cyclic redundancy check value that detects data corruption. Gzip uses the same algorithm as deflatestream, but it can be extended to use other compression formats. This format can be easily implemented in a way that does not involve the right to use a patent. The GZIP format can be obtained from RFC 1952 "GZIP file format specification 4.3 (GZIP file format specification 4.3) GZIP file format specification 4.3 (GZIP file format specification 4.3. This class cannot be used to compress files larger than 4 GB.
Description to the successor: When inheriting from gzipstream, the following Members must be rewritten: canseek, canwrite, and Canread.
The following provides a complete compression and decompression class (original downmoon ):
Class clszip
{
Public void compressfile (string sourcefile, string destinationfile)
{
// Make sure the source file is there
If (file. exists (sourcefile) = false)
Throw new filenotfoundexception ();
// Create the streams and byte arrays needed
Byte [] buffer = NULL;
Filestream sourcestream = NULL;
Filestream destinationstream = NULL;
Gzipstream compressedstream = NULL;
Try
{
// Read the bytes from the source file into a byte array
Sourcestream = new filestream (sourcefile, filemode. Open, fileaccess. Read, fileshare. Read );
// Read the source stream values into the buffer
Buffer = new byte [sourcestream. Length];
Int checkcounter = sourcestream. Read (buffer, 0, buffer. Length );
If (checkcounter! = Buffer. length)
{
Throw new applicationexception ();
}
// Open the filestream to write
Destinationstream = new filestream (destinationfile, filemode. openorcreate, fileaccess. Write );
// Create a compression stream pointing to the destiantion stream
Compressedstream = new gzipstream (destinationstream, compressionmode. Compress, true );
// Now write the compressed data to the destination file
Compressedstream. Write (buffer, 0, buffer. Length );
}
Catch (applicationexception ex)
{
MessageBox. Show (ex. message, "An error occurred when compressing the file:", messageboxbuttons. OK, messageboxicon. Error );
}
Finally
{
// Make sure we allways close all streams
If (sourcestream! = NULL)
Sourcestream. Close ();
If (compressedstream! = NULL)
Compressedstream. Close ();
If (destinationstream! = NULL)
Destinationstream. Close ();
}
}
Public void decompressfile (string sourcefile, string destinationfile)
{
// Make sure the source file is there
If (file. exists (sourcefile) = false)
Throw new filenotfoundexception ();
// Create the streams and byte arrays needed
Filestream sourcestream = NULL;
Filestream destinationstream = NULL;
Gzipstream decompressedstream = NULL;
Byte [] quartetbuffer = NULL;
Try
{
// Read in the compressed source stream
Sourcestream = new filestream (sourcefile, filemode. Open );
// Create a compression stream pointing to the destiantion stream
Decompressedstream = new gzipstream (sourcestream, compressionmode. decompress, true );
// Read the footer to determine the length of the destiantion File
Quartetbuffer = new byte [4];
Int position = (INT) sourcestream. Length-4;
Sourcestream. Position = position;
Sourcestream. Read (quartetbuffer, 0, 4 );
Sourcestream. Position = 0;
Int checklength = bitconverter. toint32 (quartetbuffer, 0 );
Byte [] buffer = new byte [checklength + 100];
Int offset = 0;
Int Total = 0;
// Read the compressed data into the buffer
While (true)
{
Int bytesread = decompressedstream. Read (buffer, offset, 100 );
If (bytesread = 0)
Break;
Offset + = bytesread;
Total + = bytesread;
}
// Now write everything to the destination file
Destinationstream = new filestream (destinationfile, filemode. Create );
Destinationstream. Write (buffer, 0, total );
// And flush everyhting to clean out the buffer
Destinationstream. Flush ();
}
Catch (applicationexception ex)
{
MessageBox. Show (ex. message, "An error occurred when extracting the file:", messageboxbuttons. OK, messageboxicon. Error );
}
Finally
{
// Make sure we allways close all streams
If (sourcestream! = NULL)
Sourcestream. Close ();
If (decompressedstream! = NULL)
Decompressedstream. Close ();
If (destinationstream! = NULL)
Destinationstream. Close ();
}
}
}
Trackback: http://tb.blog.csdn.net/TrackBack.aspx? Postid = 606592