Asp. NET tip: Use gzip compression and decompression

Source: Internet
Author: User
Asp.net| Skills | compression

. NET supports two kinds of compression formats: gzip and deflate. I tried it, the compression rate and speed are no different. Where gzip can be opened by WinRAR.

Easy to use, the following program compresses a string into a file:

using (deflatestream gzip = new Deflatestream (FS, compressionmode.compress))
{
byte[] buf = Encoding.UTF8.GetBytes (This.txbSource.Text);
Gzip. Write (buf, 0, buf. Length);
Gzip. Flush ();
}

This is only required for decompression:

gzip = new GZipStream (new MemoryStream (BUF), compressionmode.decompress);
using (StreamReader reader = new StreamReader (gzip))
{
This.txbTarget.Text = reader. ReadToEnd ();
}

If you extract from the file, just replace the MemoryStream with a FileStream on the line.
Of course, need to add: using System.IO.Compression;



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.