天天看点

谈mvc开发中gzip压缩的应用

压缩view的内容,可加过滤器

    public class GzipFilter : ActionFilterAttribute

    {

        public override void OnResultExecuting(ResultExecutingContext filterContext)

        {

            string acceptEncoding = filterContext.HttpContext.Request.Headers["Accept-Encoding"];

            if (String.IsNullOrEmpty(acceptEncoding)) return;

            var response = filterContext.HttpContext.Response;

            acceptEncoding = acceptEncoding.ToUpperInvariant();

            if (acceptEncoding.Contains("GZIP"))

            {

                response.AppendHeader("Content-Encoding", "gzip");

                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);

            }

            else if (acceptEncoding.Contains("DEFLATE"))

                response.AppendHeader("Content-Encoding", "deflate");

                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);

        }

    }

然后在要压缩的页面控制器上加标签。

        [GzipFilter]

        public ActionResult Index()

现在基本上所有的浏览器支持gzip, deflate.

这里是编程对css和js文件进行压缩放在本地,然后发送给客户端。

----这种方法在iis7.5的集成模式下有效,在vs中有效,但在iis6里我还没配置好,无效

----关键是请求,只对action有效,像js,css文件的请求,在BeginRequest里检测不到。这种方法运行在iis7里很完美,文件大概会被压缩到原来的1/3到1/4.

此方法主要是给请求的文件加上http头//Response.AppendHeader("Content-Encoding", "gzip"); 这里很难处理。

如果有谁找到iis6里面可以运行的方法麻烦告诉我,或许能一起讨论找到更好的解决方案,非常感谢!

[email protected]

浏览器检测到这个头,就会对文件进行解压缩,就正常运行了。

        protected void Application_BeginRequest(object sender, EventArgs e)

            GzipFiles();

        private void GzipFiles()

            string acceptEncoding = Request.Headers["Accept-Encoding"];

            string filepath = Request.FilePath;

            string mapfilepath = Server.MapPath("~" + filepath);

            if (acceptEncoding.Contains("gzip"))

                #region Gzip处理

                if (filepath.EndsWith(".css"))//css文件处理

                {

                    Response.AppendHeader("Content-Type", "text/css");

                    Request.ContentType = "text/css";

                    if (filepath.EndsWith("gzip.css"))

                    {

                        FileInfo fi = new FileInfo(mapfilepath);

                        Response.AppendHeader("Content-Encoding", "gzip");

                        int len = mapfilepath.Length - "gzip.css".Length;

                        if (fi.Exists == false) GZip(mapfilepath.Substring(0, len), mapfilepath);

                    }

                }

                else if (filepath.EndsWith(".js"))//js文件处理

                    Response.AppendHeader("Content-Type", "application/x-javascript");

                    Request.ContentType = "application/x-javascript";

                    if (filepath.EndsWith("gzip.js"))

                        int len = mapfilepath.Length - "gzip.js".Length;

                #endregion

            else if (acceptEncoding.Contains("deflate"))

                #region deflate处理

                    if (filepath.EndsWith("deflate.css"))

                        int len = mapfilepath.Length - "deflate.css".Length;

                    if (filepath.EndsWith("deflate.js"))

                        int len = mapfilepath.Length - "deflate.js".Length;

        public void GZip(string fileName, string gipFileName)

            FileStream fr = File.Create(gipFileName);

            FileStream fc = File.OpenRead(fileName);

            GZipStream gzs = new GZipStream(fr, CompressionMode.Compress); //压缩文件类

            byte[] arr = new byte[fc.Length];

            fc.Read(arr, 0, (int)fc.Length);

            gzs.Write(arr, 0, (int)fc.Length);

            gzs.Close();

            fc.Close();

            fr.Close();

        //解压缩文件方法

        public void DeZGip(string fileName, string gipFileName)

            //准备输入输出文件

            FileStream fc = File.Create(fileName);

            FileStream fr = File.OpenRead(gipFileName);

            GZipStream gzs = new GZipStream(fr, CompressionMode.Decompress);

            byte[] arr = new byte[fr.Length];

            fr.Read(arr, 0, (int)fr.Length);

            fc.Write(arr, 0, (int)fr.Length);

继续阅读