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

本文介绍了一种通过直接生成并使用压缩后的静态页面(ghtml)来提高网站加载速度的方法,该方法利用IIS6的gzip功能,并针对不同浏览器需求提供适应性响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

      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.TransmitFile(filePath);
                    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();
            }
        }

http://www.cnblogs.com/cgy985/archive/2008/07/22/1248943.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值