Skip Navigation Links
技术文档
·网站建设
·软件使用
·图形设计
·程序开发
·网络应用
·电脑技巧
 
公司介绍
·公司简介
·索仕SRCOS网络应用平台
·索仕网站管理系统
·影视广告制作
·联系我们
 
 

生成静态页,为什么不生成压缩静态页?

7/23/2008 11:44:31 AM

     iis6开启gzip后,是先将需要压缩的静态文件压缩保存在一个目录,请求来时先判断是否支持gzip,不支持直接发送静态文件,支持则再判断文件是否修改,没有就直接发送压缩的文件,有则重新生成压缩文件。

    根据我对公司的多个网站观察访问者浏览器支持gzip的高达99%以上,我就想又何必多保存一份静态文件,直接保存压缩后的文件不就ok,既节约了空间又节约了处理的过程laughing,万一碰见那1%不到的访客,解个压给他便是。好!就这么处理,为压缩的html专门取个后缀名.ghtml。

 
 
 
生成ghtml:

 

 
 
 

    首先iis注册.ghtml文件交给.net处理。

 

然后将需要生成ghtml的aspx文件通过这个函数处理,也就是生成静态文件,再多一步压缩
 


        ///<summary>
生成静态页,为什么不生成压缩静态页?        
/// 在html目录下生成压缩html
生成静态页,为什么不生成压缩静态页?        
/// </summary>
生成静态页,为什么不生成压缩静态页?        
/// <param name="aspxPath">动态页面请求路径</param>
生成静态页,为什么不生成压缩静态页?        
/// <param name="urlPath">静态页面请求路径</param>

生成静态页,为什么不生成压缩静态页?        public static void AspxToHtml(string aspxPath, string urlPath)
        
{
生成静态页,为什么不生成压缩静态页?            
string filePath = HttpContext.Current.Server.MapPath(urlPath);
生成静态页,为什么不生成压缩静态页?            
if (!Directory.Exists(filePath))
            
{
生成静态页,为什么不生成压缩静态页?                Directory.CreateDirectory(filePath.Substring(
0,filePath.LastIndexOf("\\")));
生成静态页,为什么不生成压缩静态页?            }

生成静态页,为什么不生成压缩静态页?            
using (FileStream fs = new FileStream(filePath, FileMode.Create))
            
{
生成静态页,为什么不生成压缩静态页?                
using (GZipStream gz = new GZipStream(fs, CompressionMode.Compress))
                
{
生成静态页,为什么不生成压缩静态页?                    
using (StreamWriter sw = new StreamWriter(gz, Encoding.UTF8))
                    
{
生成静态页,为什么不生成压缩静态页?                        HttpContext.Current.Server.Execute(aspxPath, sw,
false);
生成静态页,为什么不生成压缩静态页?                        sw.Flush();
生成静态页,为什么不生成压缩静态页?                    }

生成静态页,为什么不生成压缩静态页?                }

生成静态页,为什么不生成压缩静态页?            }

生成静态页,为什么不生成压缩静态页?        }

 
 


 

 

处理ghtml请求,浏览器支持gzip就直接写入文件,否则先解压内容再输出:

 

 

 

     自己写个HttpModule,在BeginRequest事件中处理.ghtml请求 ,静态页嘛就模拟一下html的304处理
 


 

生成静态页,为什么不生成压缩静态页?public class FreeModule : IHttpModule
    
{
生成静态页,为什么不生成压缩静态页?        
// Init方法仅用于给期望的事件注册方法
生成静态页,为什么不生成压缩静态页?
        public void Init(HttpApplication context)
        
{   
生成静态页,为什么不生成压缩静态页?            context.BeginRequest 
+= new EventHandler(context_BeginRequest);
生成静态页,为什么不生成压缩静态页?        }

生成静态页,为什么不生成压缩静态页?
生成静态页,为什么不生成压缩静态页?
生成静态页,为什么不生成压缩静态页?    
private void SetClientCaching(HttpResponse response, DateTime lastModified)
    
{
生成静态页,为什么不生成压缩静态页?        response.Cache.SetETag(lastModified.Ticks.ToString());
生成静态页,为什么不生成压缩静态页?        response.Cache.SetLastModified(lastModified);

生成静态页,为什么不生成压缩静态页?
        response.Cache.SetCacheability(HttpCacheability.Public);
生成静态页,为什么不生成压缩静态页?
    }

生成静态页,为什么不生成压缩静态页?
生成静态页,为什么不生成压缩静态页?
生成静态页,为什么不生成压缩静态页?        
// 处理BeginRequest 事件的实际代码
生成静态页,为什么不生成压缩静态页?
        void context_BeginRequest(object sender, EventArgs e)
        
{
生成静态页,为什么不生成压缩静态页?            HttpApplication application 
= (HttpApplication)sender;
生成静态页,为什么不生成压缩静态页?            HttpContext context 
= application.Context;
生成静态页,为什么不生成压缩静态页?            HttpRequest request
=context.Request;
生成静态页,为什么不生成压缩静态页?            HttpResponse response 
= context.Response;

生成静态页,为什么不生成压缩静态页?

 

生成静态页,为什么不生成压缩静态页?            string path = context.Request.Path.ToLower();
生成静态页,为什么不生成压缩静态页?            
生成静态页,为什么不生成压缩静态页?            
string acceptEncoding =
request.Headers["Accept-Encoding"];

生成静态页,为什么不生成压缩静态页?            bool accept = !string.IsNullOrEmpty(acceptEncoding)? acceptEncoding.ToLower().Contains("gzip") : false;
 

生成静态页,为什么不生成压缩静态页?

 

生成静态页,为什么不生成压缩静态页?            if (path.Contains(".ghtml"))
            
{
生成静态页,为什么不生成压缩静态页?                
string filePath = request.PhysicalApplicationPath + "/html" +
path;
生成静态页,为什么不生成压缩静态页?                
if (!File.Exists(filePath))
                
throw new FileNotFoundException("找不到文件ghtml"); }
生成静态页,为什么不生成压缩静态页?
生成静态页,为什么不生成压缩静态页?                DateTime writeTime 
= File.GetLastWriteTimeUtc(filePath);
生成静态页,为什么不生成压缩静态页?                DateTime since;
生成静态页,为什么不生成压缩静态页?                
if (DateTime.TryParse(request.Headers["IfModifiedSince"],out since) && writeTime == since.ToUniversalTime() )
                
{
生成静态页,为什么不生成压缩静态页?                    response.StatusCode 
= 304;
生成静态页,为什么不生成压缩静态页?                    response.StatusDescription 
= "Not Modified";
生成静态页,为什么不生成压缩静态页?                }

生成静态页,为什么不生成压缩静态页?                
else

                {

                       if (accept )
                   
{

 

生成静态页,为什么不生成压缩静态页?                       response.AppendHeader("Content-Encoding""gzip");

 

生成静态页,为什么不生成压缩静态页?                       response.TransmitFile(filePath);

生成静态页,为什么不生成压缩静态页?                    }

生成静态页,为什么不生成压缩静态页?                   else

                    {

生成静态页,为什么不生成压缩静态页?                       response.Write(DezipText(filePath));// 解压ghtml文件

 

生成静态页,为什么不生成压缩静态页?                    }

 

 

生成静态页,为什么不生成压缩静态页?                    SetClientCaching(response, writeTime);
生成静态页,为什么不生成压缩静态页?                    response.End();
生成静态页,为什么不生成压缩静态页?                }

生成静态页,为什么不生成压缩静态页?            }
    
生成静态页,为什么不生成压缩静态页?        }

生成静态页,为什么不生成压缩静态页?        
public void Dispose()
        
{
生成静态页,为什么不生成压缩静态页?        }

生成静态页,为什么不生成压缩静态页?    }

生成静态页,为什么不生成压缩静态页?

 

 

解压ghtml:

 

      最后还有个解压ghtml函数

        /// <summary>
生成静态页,为什么不生成压缩静态页?        
/// 解压text文件后返回str
生成静态页,为什么不生成压缩静态页?        
/// </summary>
生成静态页,为什么不生成压缩静态页?        
/// <param name="textPath">物理路径</param>
生成静态页,为什么不生成压缩静态页?        
/// <returns>文件字符串</returns>

生成静态页,为什么不生成压缩静态页?        public static string DezipText(string textPath)
        
{
生成静态页,为什么不生成压缩静态页?            
using (FileStream fs = File.OpenRead(textPath))
            

生成静态页,为什么不生成压缩静态页?                GZipStream gz 
= new GZipStream(fs, CompressionMode.Decompress);
生成静态页,为什么不生成压缩静态页?                StreamReader sr 
= new StreamReader(gz);
生成静态页,为什么不生成压缩静态页?                
return sr.ReadToEnd();
生成静态页,为什么不生成压缩静态页?            }

生成静态页,为什么不生成压缩静态页?        }

 

作者:無尽海 来源:博客园
 
 
 
昆明索仕科技开发有限公司 版权所有 Copyright© 2002-2010 Kunming Source Technology Exploitive Co.,LTD. All Rights Reserved.
电话:0871-5627877 业务QQ:163871 联系我们
本站基于:索仕网站信息管理系统建设 版本 2.0.4325