找回密码
 注册
搜索
热搜: 回贴

ASP.NET技巧:用GZip压缩和解压

2009-12-13 12:38| 发布者: admin| 查看: 91| 评论: 0|原作者: 云忆

○.Net支持两种压缩格式:GZip和De......


.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;

最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 19:30 , Processed in 0.171141 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部