代码片段,压缩和CurrentCulture 设定

public   class  BasePage : Page  {

        
public BasePage() {
        }


        
protected override void OnLoad(EventArgs e) {
            
base.OnLoad(e);
            
// Request might not be initialized -> use HttpContext
            string ua = HttpContext.Current.Request.UserAgent != null ? HttpContext.Current.Request.UserAgent.ToLower() : "";
            
if(Settings.EnableHttpCompression && !ua.Contains("konqueror"&& !ua.Contains("safari")) {
                
if(Request.Headers["Accept-encoding"!= null && Request.Headers["Accept-encoding"].Contains("gzip")) {
                    Response.Filter 
= new GZipStream(Response.Filter, CompressionMode.Compress, true);
                    Response.AppendHeader(
"Content-encoding""gzip");
                    Response.AppendHeader(
"Vary""Content-encoding");
                    
//Response.Write("HTTP Compression Enabled (GZip)");
                }

                
else if(Request.Headers["Accept-encoding"!= null && Request.Headers["Accept-encoding"].Contains("deflate")) {
                    Response.Filter 
= new DeflateStream(Response.Filter, CompressionMode.Compress, true);
                    Response.AppendHeader(
"Content-encoding""deflate");
                    Response.AppendHeader(
"Vary""Content-encoding");
                    
//Response.Write("HTTP Compression Enabled (Deflate)");
                }

            }

        }


        
protected override void InitializeCulture() {
            
if(Request.Cookies[Settings.CultureCookieName] != null{
                HttpCookie cookie 
= Request.Cookies[Settings.CultureCookieName];
                
string culture = cookie["C"];
                Thread.CurrentThread.CurrentCulture 
= new CultureInfo(culture);
                Thread.CurrentThread.CurrentUICulture 
= new CultureInfo(culture);
            }

            
else {
                
try {
                    
if(Settings.DefaultLanguage.Equals("-")) {
                        Thread.CurrentThread.CurrentCulture 
= new CultureInfo("en-US");
                        Thread.CurrentThread.CurrentUICulture 
= new CultureInfo("en-US");
                    }

                    
else {
                        Thread.CurrentThread.CurrentCulture 
= new CultureInfo(Settings.DefaultLanguage);
                        Thread.CurrentThread.CurrentUICulture 
= new CultureInfo(Settings.DefaultLanguage);
                    }

                }

                
catch {
                    Thread.CurrentThread.CurrentCulture 
= new CultureInfo("en-US");
                    Thread.CurrentThread.CurrentUICulture 
= new CultureInfo("en-US");
                }

            }

            
//Response.Write("Culture: " + Thread.CurrentThread.CurrentCulture.Name + "<br />");
            
//Response.Write("UICulture: " + Thread.CurrentThread.CurrentUICulture.Name + "</br >");
        }


    }
 
public   static   class  Compressor  {

        
/// <summary>
        
/// Compresses data.
        
/// </summary>
        
/// <param name="data">The data to compress.</param>
        
/// <returns>The compressed data.</returns>

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


        
/// <summary>
        
/// Decompresses data.
        
/// </summary>
        
/// <param name="data">The data to decompress.</param>
        
/// <returns>The decompressed data.</returns>

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


    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值