欧美日韩一区二区三区四区不卡,日韩欧美视频一区二区三区四区,久久精品欧美一区二区三区不卡,国产精品久久久乱弄

咨詢電話:186 7916 6165 咨詢電話:186 7916 6165 (微信同號(hào))    在線QQ:181796286
NEWS BLOG ·
學(xué)無止境
關(guān)注開優(yōu)網(wǎng)絡(luò) 關(guān)注前沿
ASP.NET公共類庫(kù)之XML處理基類XMLHelper.cs
ASP.NET公共類庫(kù)之HTML格式輔助類HtmlUtils.cs

ASP.NET公共類庫(kù)之?dāng)?shù)據(jù)壓縮類GZipHandler.cs

發(fā)表日期:2015-09-08    文章編輯:南昌開優(yōu)網(wǎng)絡(luò)    瀏覽次數(shù):3973    標(biāo)簽:ASP.NET應(yīng)用

/**********************************************
 * 類作用:   數(shù)據(jù)壓縮類
 * 作者:開優(yōu)網(wǎng)絡(luò)
 * http://www.568387.com/
 ***********************************************/

using System;
using System.Web;
using System.IO;
using System.Text;
using System.IO.Compression;
using System.Text.RegularExpressions;

namespace Svnhost.Web
{
    public class GZipHandler : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            HttpContext Context = HttpContext.Current;
            HttpRequest Request = Context.Request;
            HttpResponse Response = Context.Response;

            string AcceptEncoding = Request.Headers["Accept-Encoding"];
            // *** Start by checking whether GZip is supported by client

            bool UseGZip = false;

            if (!string.IsNullOrEmpty(AcceptEncoding) &&

                AcceptEncoding.ToLower().IndexOf("gzip") > -1)

                UseGZip = true;

            // *** Create a cachekey and check whether it exists

            string CacheKey = Request.QueryString.ToString() + UseGZip.ToString();

            byte[] Output = Context.Cache[CacheKey] as byte[];

            if (Output != null)
            {
                // *** Yup - read cache and send to client
                SendOutput(Output, UseGZip);
                return;
            }

            // *** Load the script file 

            string Script = "";


            StreamReader sr = new StreamReader(context.Server.MapPath(Request["src"]));

            Script = sr.ReadToEnd();


            // *** Now we're ready to create out output

            // *** Don't GZip unless at least 8k

            if (UseGZip && Script.Length > 6000)

                Output = GZipMemory(Script);

            else
            {

                Output = Encoding.ASCII.GetBytes(Script);

                UseGZip = false;

            }
            // *** Add into the cache with one day

            Context.Cache.Add(CacheKey, Output, null, DateTime.UtcNow.AddDays(1), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
            // *** Write out to Response object with appropriate Client Cache settings

            this.SendOutput(Output, UseGZip);
        }
        /// <summary>
        /// Sends the output to the client using appropriate cache settings.
        /// Content should be already encoded and ready to be sent as binary.
        /// </summary>
        /// <param name="Output"></param>
        /// <param name="UseGZip"></param>
        private void SendOutput(byte[] Output, bool UseGZip)
        {

            HttpResponse Response = HttpContext.Current.Response;
            Response.ContentType = "application/x-javascript";
            if (UseGZip)
                Response.AppendHeader("Content-Encoding", "gzip");
            //if (!HttpContext.Current.IsDebuggingEnabled)
            // {

            Response.ExpiresAbsolute = DateTime.UtcNow.AddYears(1);
            Response.Cache.SetLastModified(DateTime.UtcNow);
            Response.Cache.SetCacheability(HttpCacheability.Public);
            // }

            Response.BinaryWrite(Output);
            Response.End();

        }


        /// <summary>
        /// Takes a binary input buffer and GZip encodes the input
        /// </summary>
        /// <param name="Buffer"></param>
        /// <returns></returns>

        public static byte[] GZipMemory(byte[] Buffer)
        {

            MemoryStream ms = new MemoryStream();
            GZipStream GZip = new GZipStream(ms, CompressionMode.Compress);
            GZip.Write(Buffer, 0, Buffer.Length);
            GZip.Close();
            byte[] Result = ms.ToArray();
            ms.Close();
            return Result;

        }
        public static byte[] GZipMemory(string Input)
        {
            return GZipMemory(Encoding.ASCII.GetBytes(Input));

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

    }
}
主站蜘蛛池模板: 黔江区| 黔南| 临夏县| 巴青县| 韩城市| 丘北县| 淮北市| 威远县| 镇江市| 正宁县| 宜都市| 沁源县| 兰考县| 祁连县| 青河县| 怀柔区| 河东区| 体育| 澎湖县| 恩平市| 景东| 房产| 桐乡市| 花莲县| 常宁市| 金坛市| 石狮市| 宾阳县| 宣威市| 辉南县| 轮台县| 仙居县| 星座| 黄龙县| 雷州市| 南召县| 会东县| 炉霍县| 玛沁县| 云南省| 高阳县|