All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
ASP.NET技巧:用GZip压缩和解压_[Asp.Net教程]

Writer: delv Article type: Programming skills(编程技巧) Time: 2014/1/10 6:28:11 Browse times: 370 Comment times: 0

ASP.NET技巧:用GZip压缩和解压_[Asp.Net教程]


Head photo

Go homepage
Upload pictures
Write articles

ASP.NET技巧:用GZip压缩和解压_[Asp.Net教程]

.Net支持两种压缩格式:GZip和Deflate。我试了一下,压缩率和速度没区别。其中,GZip可以被WinRAR打开。

使用起来很简单,下面的程序将字符串压缩入文件:

using (DeflateStream gzip = new DeflateStream(fs, CompressionMode.Compress))
{
byte[] buf = Encoding.UTF8.GetBytes(this.txbSource.Text);
gzip.Write(buf, 0, buf.Length);
gzip.Flush();
}

解压只需要这样:

gzip = new GZipStream(new MemoryStream(buf), CompressionMode.Decompress);
using (StreamReader reader = new StreamReader(gzip))
{
this.txbTarget.Text = reader.ReadToEnd();
}

如果从文件解压,只需要把MemoryStream换成一个FileStream就行了。
当然,需要加:using System.IO.Compression;

来源:网络





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.