改进压缩ASP.NET中的ViewState方法

using System;
using System.IO;
using System.Web;
using System.Web.UI;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

/// <summary>
/// 压缩ViewState By Mack.Z (MSPlus),页面继承该类即可
/// </summary>
public class CompressionViewState : System.Web.UI.Page
{
    /// <summary>
    /// 设定序列化后的字符串长度为多少后启用压缩
    /// </summary>
    private const Int32 LimitLength = 1096;
    /// <summary>
    /// 存放压缩ViewState的隐藏域名称
    /// </summary>
    private const string ViewStateName = "__COMPRESSSTATE";

    /// <summary>
    /// 保存页的所有视图状态信息和控件状态信息,页面刷新时调用
    /// </summary>
    /// <param name="pViewState">要在其中存储视图状态信息的 Object</param>
    protected override void SavePageStateToPersistenceMedium(object pViewState)
    {
        using (StringWriter writer = new StringWriter())
        {
            LosFormatter format = new LosFormatter();
            format.Serialize(writer, pViewState);
            string viewStateStr = writer.ToString();
            bool useZip = false;
            if (viewStateStr.Length > LimitLength)
            {
                useZip = true;
                byte[] pBytes = System.Convert.FromBase64String(viewStateStr); //压缩viewstate                    
                pBytes = Compress(pBytes);
                viewStateStr = System.Convert.ToBase64String(pBytes);
            }
            if ((Request != null && Request.Headers["X-MicrosoftAjax"] != null))//ajax框架中的请求
                ScriptManager.RegisterHiddenField(this, ViewStateName, viewStateStr);
            else
                RegisterHiddenField(ViewStateName, viewStateStr);
            RegisterHiddenField("__USEZIP", useZip.ToString().ToLower());
        }
    }
    /// <summary>
    /// 将所有保存的视图状态信息加载到 Page 对象,在post提交时调用
    /// </summary>
    /// <returns>保存的视图状态</returns>
    protected override object LoadPageStateFromPersistenceMedium()
    {
        string viewStateStr = Request.Form.Get(ViewStateName);
        string useZip = Request.Form.Get("__USEZIP");
        if (useZip == "true")
        {
            byte[] pBytes = System.Convert.FromBase64String(viewStateStr);
            pBytes = DeCompress(pBytes);//解压缩viewstate
            viewStateStr = System.Convert.ToBase64String(pBytes);
        }
        return new LosFormatter().Deserialize(viewStateStr);
    }
    /// <summary>
    /// 压缩算法
    /// </summary>
    /// <param name="pBytes"></param>
    /// <returns></returns>
    public static byte[] Compress(byte[] pBytes)
    {
        MemoryStream mMemory = new MemoryStream();
        Deflater mDeflater = new Deflater(Deflater.BEST_COMPRESSION);
        using (DeflaterOutputStream mStream = new DeflaterOutputStream(mMemory, mDeflater, 131072))
        {
            mStream.Write(pBytes, 0, pBytes.Length);
        }
        return mMemory.ToArray();
    }
    /// <summary>
    /// 解压缩算法
    /// </summary>
    /// <param name="pBytes"></param>
    /// <returns></returns>
    public static byte[] DeCompress(byte[] pBytes)
    {
        MemoryStream mMemory = new MemoryStream();
        using (InflaterInputStream mStream = new InflaterInputStream(new MemoryStream(pBytes)))
        {
            Int32 mSize;
            byte[] mWriteData = new byte[4096];
            while (true)
            {
                mSize = mStream.Read(mWriteData, 0, mWriteData.Length);
                if (mSize > 0)
                    mMemory.Write(mWriteData, 0, mSize);
                else
                    break;
            }
        }
        return mMemory.ToArray();
    }
}

 对网路上共享的方法进行了改进和总结。

dll文件下载:https://files.cnblogs.com/slyzly/ICSharpCode.SharpZipLib.rar

posted @ 2013-01-08 15:10  事理  阅读(501)  评论(0编辑  收藏  举报