Friday, 30 March 2012

compress viewstate asp.net

Method 1:

protected override void SavePageStateToPersistenceMedium(object pageViewState)
{
    if (bool.Parse(ConfigurationManager.AppSettings["ViewStateCompressionEnable"] ?? "false"))
    {
        Pair pair1;
        System.Web.UI.PageStatePersister pageStatePersister1 = this.PageStatePersister;
        Object ViewState;
        if (pageViewState is Pair)
        {
            pair1 = ((Pair)pageViewState);
            pageStatePersister1.ControlState = pair1.First;
            ViewState = pair1.Second;
        }
        else
        {
            ViewState = pageViewState;
        }
        LosFormatter mFormat = new LosFormatter();
        StringWriter mWriter = new StringWriter();
        mFormat.Serialize(mWriter, ViewState);
        String mViewStateStr = mWriter.ToString();
        byte[] pBytes = System.Convert.FromBase64String(mViewStateStr);
        pBytes = ViewStateCompressor.CompressViewState(pBytes);
        String vStateStr = System.Convert.ToBase64String(pBytes);
        pageStatePersister1.ViewState = vStateStr;
        pageStatePersister1.Save();
    }
    else
    {
        base.SavePageStateToPersistenceMedium(pageViewState);
    }
}

//// Deserialize view state
protected override object LoadPageStateFromPersistenceMedium()
{
    if (bool.Parse(ConfigurationManager.AppSettings["ViewStateCompressionEnable"] ?? "false"))
    {
        PageStatePersister pageStatePersister1 = this.PageStatePersister;
        pageStatePersister1.Load();
        String vState = pageStatePersister1.ViewState.ToString();
        byte[] pBytes = System.Convert.FromBase64String(vState);
        pBytes = ViewStateCompressor.DecompressViewState(pBytes);
        LosFormatter mFormat = new LosFormatter();
        Object ViewState = mFormat.Deserialize(System.Convert.ToBase64String(pBytes));
        return new Pair(pageStatePersister1.ControlState, ViewState);
    }
    else
    {
        return base.LoadPageStateFromPersistenceMedium();
    }
}
Method 2:

    public class ViewStateCompressor
    {
        public ViewStateCompressor() { }

        public static byte[] CompressViewState(byte[] uncompData)
        {
            using (MemoryStream mem = new MemoryStream())
            {
                CompressionMode mode = CompressionMode.Compress;
                // Use the newly created memory stream for the compressed data.
                using (GZipStream gzip = new GZipStream(mem, mode, true))
                {
                    //Writes compressed byte to the underlying
                    //stream from the specified byte array.
                    gzip.Write(uncompData, 0, uncompData.Length);
                    gzip.Close();
                }
                return mem.ToArray();
            }
        }

        public static byte[] DecompressViewState(byte[] compData)
        {
            GZipStream gzip;
            using (MemoryStream inputMem = new MemoryStream())
            {
                inputMem.Write(compData, 0, compData.Length);
                // Reset the memory stream position to begin decompression.
                inputMem.Position = 0;
                CompressionMode mode = CompressionMode.Decompress;
                gzip = new GZipStream(inputMem, mode, true);
                using (MemoryStream outputMem = new MemoryStream())
                {
                    // Read 1024 bytes at a time
                    byte[] buf = new byte[1024];
                    int byteRead = -1;
                    byteRead = gzip.Read(buf, 0, buf.Length);
                    while (byteRead > 0)
                    {
                        //write to memory stream
                        outputMem.Write(buf, 0, byteRead);
                        byteRead = gzip.Read(buf, 0, buf.Length);
                    }
                    gzip.Close();
                    return outputMem.ToArray();
                }
            }
        }
    }

No comments:

Post a Comment