zoukankan      html  css  js  c++  java
  • 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;
        }
    }
  • 相关阅读:
    Java 第十一届 蓝桥杯 省模拟赛 洁净数
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 第十层的二叉树
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    Java 第十一届 蓝桥杯 省模拟赛 70044与113148的最大公约数
    20. Valid Parentheses
    290. Word Pattern
    205. Isomorphic Strings
    71. Simplify Path
  • 原文地址:https://www.cnblogs.com/skydau/p/2007101.html
Copyright © 2011-2022 走看看