asp.net 能用gzipStream对页面html,js,css文件进行压缩吗?

begintransaction 2011-11-10 02:18:14
比如百度的页面
...全文
208 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
begintransaction 2011-11-10
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 net_lover 的回复:]
StreamReader sr = new StreamReader(stream,Encoding.GetEncoding("GB2312")); 或者其他编码

先看这里是否已经乱了
[/Quote]

WebRequest request = WebRequest.Create(Request.Url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();//获得基流

//转换成字节
StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8, true);
string str = sr.ReadToEnd();

Byte[] bytes = new Byte[str.Length];

stream.Read(bytes, 0, bytes.Length);

//启用内存流
MemoryStream ms = new MemoryStream();

//启用压缩GzipStream
GZipStream gzs = new GZipStream(ms, CompressionMode.Compress, true);

gzs.Write(bytes, 0, bytes.Length);

gzs.Close();

Byte[] newByte = ms.ToArray();


string temp = System.Text.Encoding.UTF8.GetString(newByte);//这里检测到转换成字符串就乱码了!

// int len = System.Text.Encoding.UTF8.GetBytes("").Length;


FileStream fs = new FileStream(Server.MapPath("text.html"), FileMode.Create, FileAccess.Write);

fs.Write(newByte, 0, newByte.Length);

fs.Close();
孟子E章 2011-11-10
  • 打赏
  • 举报
回复
StreamReader sr = new StreamReader(stream,Encoding.GetEncoding("GB2312")); 或者其他编码

先看这里是否已经乱了
begintransaction 2011-11-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 net_lover 的回复:]
可以啊
http://blog.csdn.net/kailan818/article/details/6556273
http://www.dotblogs.com.tw/jeff-yeh/archive/2008/04/16/2932.aspx
http://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream.……
[/Quote]

WebRequest request = WebRequest.Create(Request.Url);
WebResponse response = request.GetResponse();
Stream stream = response.GetResponseStream();//获得基流

//转换成字节
StreamReader sr = new StreamReader(stream);
string str = sr.ReadToEnd();

Byte[] bytes = new Byte[str.Length];

stream.Read(bytes, 0, bytes.Length);

//启用内存流
MemoryStream ms = new MemoryStream();

//启用压缩GzipStream
GZipStream gzs = new GZipStream(ms, CompressionMode.Compress, true);

gzs.Write(bytes, 0, bytes.Length);

gzs.Close();

Byte[] newByte = ms.ToArray();

FileStream fs = new FileStream(Server.MapPath("text.html"), FileMode.Create, FileAccess.Write);

fs.Write(newByte, 0, newByte.Length);

fs.Close();

我这样写,生成的一个静态页面打开是乱码的...
孟子E章 2011-11-10
  • 打赏
  • 举报
回复
可以啊
http://blog.csdn.net/kailan818/article/details/6556273
http://www.dotblogs.com.tw/jeff-yeh/archive/2008/04/16/2932.aspx
http://msdn.microsoft.com/zh-cn/library/system.io.compression.gzipstream.aspx

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧