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;
    }
}

 

转载于:https://www.cnblogs.com/skydau/archive/2011/04/06/2007101.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值