Two new compression classes in c#2.0 (Downmoon original)

Source: Internet
Author: User
Tags arrays flush
Compression | original

Two new compression classes in the. NET Framework 2.0

System.IO.Compression namespaces
Note: This namespace is new in the. NET Framework version 2.0.
The System.IO.Compression namespace contains classes that provide basic streaming compression and decompression services.
(Downmoon original)
Class description
Deflatestream provides methods and properties to compress and decompress streams using the Deflate algorithm.
GZipStream provides methods and properties for compressing and decompressing streams.
Enumeration description
COMPRESSIONMODE Specifies whether the underlying stream is compressed or uncompressed.

The following is an example of GZipStream to illustrate


Note: This class is new in the. NET Framework version 2.0.

Provides methods and properties to compress and decompress streams.
Namespaces: System.IO.Compression
Assembly: System (in System.dll)
Grammar
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

Note
This class represents the GZIP data format, which uses an industry-standard algorithm for lossless compression and decompression files. This format includes a cyclic redundancy check value that detects data corruption. The GZIP data format uses the same algorithm as the Deflatestream class, but it can be extended to use other compression formats. This format can be easily implemented in a way that does not involve patent rights. The gzip format is available from the RFC 1952 "gzip file format specification 4.3 (gzip 4.3) gzip files format Specification 4.3 (gzip file formats specification 4.3 ) "is obtained in. This class cannot be used to compress files larger than 4 GB.

Description to Inheritors when inheriting from GZipStream, the following members must be overridden: CanSeek, CanWrite, and CanRead.


Below provides a complete compression and decompression class (Downmoon original):

 class clszip
    {
        public void Compressfile (String sourcefile, String destinationfile)
        {
 & nbsp;         //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 to
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, "Error occurred while compressing 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) br>        {
            //Make sure the source file is there
            if (F Ile. 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 while 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 ();
}

}
}



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.