• Asp.Net 性能 ViewState 压缩的2种方法



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.IO;
    using System.IO.Compression;
    using System.Web.UI;

    /// <summary>
    /// Summary description for PageBase
    /// </summary>
    public class PageBase : System.Web.UI.Page
    {
        
    // 压缩
        public static byte[] Compress(byte[] data)
        {
            MemoryStream output 
    = new MemoryStream();
            GZipStream gzip 
    = new GZipStream(output,
                              CompressionMode.Compress, 
    true);
            gzip.Write(data, 
    0, data.Length);
            gzip.Close();
            
    return output.ToArray();
        }

        
    // 解压缩
        public static byte[] Decompress(byte[] data)
        {
            MemoryStream input 
    = new MemoryStream();
            input.Write(data, 
    0, data.Length);
            input.Position 
    = 0;
            GZipStream gzip 
    = new GZipStream(input,
                              CompressionMode.Decompress, 
    true);
            MemoryStream output 
    = new MemoryStream();
            
    byte[] buff = new byte[64];
            
    int read = -1;
            read 
    = gzip.Read(buff, 0, buff.Length);
            
    while (read > 0)
            {
                output.Write(buff, 
    0, read);
                read 
    = gzip.Read(buff, 0, buff.Length);
            }
            gzip.Close();
            
    return output.ToArray();
        }

        
    protected override void SavePageStateToPersistenceMedium(object pageViewState)
        {
            MemoryStream ms 
    = new MemoryStream();
            LosFormatter m_formatter 
    = new LosFormatter();
            m_formatter.Serialize(ms, pageViewState);
            ms.Position 
    = 0;
            StreamReader sr 
    = new StreamReader(ms);
            
    string viewStateString = sr.ReadToEnd();
            
    byte[] ViewStateBytes = Convert.FromBase64String(viewStateString);
            ViewStateBytes 
    = Compress(ViewStateBytes);
            Session[
    "ViewState"= Convert.ToBase64String(ViewStateBytes);
            ms.Close();
            
    return;
        }

        
    // 序列化ViewState
        protected override object LoadPageStateFromPersistenceMedium()
        {
            
    object viewStateBag;
            
    string m_viewState = (string)Session["ViewState"];
            
    byte[] ViewStateBytes = Convert.FromBase64String(m_viewState);
            ViewStateBytes 
    = Decompress(ViewStateBytes);
            LosFormatter m_formatter 
    = new LosFormatter();
            
    try
            {
                viewStateBag 
    = m_formatter.Deserialize(Convert.ToBase64String(ViewStateBytes));
            }
            
    catch (Exception ex)
            {
                viewStateBag 
    = string.Empty;
            }
            
    return viewStateBag;
        }
    }
  • 相关阅读:
    SQL SERVER 2000 配置文件 SETUP.INI
    (转)Sybase ASE基础知识:利用Sybase Central简单操作Sybase ASE数据库
    新软发布:Autorun病毒免疫工具
    vc 编程最需要注意的地方
    (转)不得不了解VB中的CallByName
    作业总结
    (转)傻瓜式简单制作Windows7旗舰版免激活光盘镜像教程 (安装后自动激活)
    发布C#模块:平面凸包的计算
    凸包计算模块ConvexHull的使用方法
    模块发布——树类模块
  • 原文地址:https://www.cnblogs.com/skydau/p/2007101.html
Copyright © 2020-2023  润新知