Zipping/Compressing ViewState in ASP.NET March 30, '05 Comments [7] Posted in ASP.NET | DasBlog | ViewState | HttpModule | Bugs Sponsored By Here's an interesting, odd, but obvious idea. If you're not able to use HttpCompression like the Blowery HttpCompression module that we use with dasBlog due to the pile of bugs in older versions of IE around compression, you can "zip" up the ViewState on fat (usually DataGrid related bloat). This is some VB code that Vlad Olifier did, that he said I could post on my blog. It's also a new submission at GotDotnet. To use it, you just derive your ASP.NET page class from it. Using Zipped ViewState: Public Class CompressPage Inherits PageViewStateZip Just deriving from System.Web.UI.Page as usual: Public Class RegularPage Inherits System.Web.UI.Page The "trick" is pretty simple. There are little-known virtuals in Page that you can override - specifically LoadPageStateFromPersistanceMedium (where that persistance medium is a hidden input box) and SavePageStateToPersistenceMedium. When it's time to load view state, we pull it out of the form and un-base64 the string into a byte array, un-zip the bytes, then deserialize the ViewState. When it's time to save, reverse the process - Serialize, zip, store. There's some overhead, certainly. The amount of compression is usually about 50%, but your mileage may vary (YMMV). The real decision flow is this: Can you use HttpCompression (via XCompress or an HttpModule)? If so, use it. Can't? (Bugs, SSL, Compatibility, bosses, don't want to take the perf hit, etc) Got Fat ViewState on a few pages? Use Zipped ViewState on a few pages as needed. Imports System.IOImports Zip = ICSharpCode.SharpZipLib.Zip.Compression Public Class PageViewStateZip : Inherits System.Web.UI.Page Protected Overrides Function LoadPageStateFromPersistenceMedium() As Object Dim vState As String = Me.Request.Form("__VSTATE") Dim bytes As Byte() = System.Convert.FromBase64String(vState) bytes = vioZip.Decompress(bytes) Dim format As New LosFormatter Return format.Deserialize(System.Convert.ToBase64String(bytes)) End Function Protected Overrides Sub SavePageStateToPersistenceMedium(ByVal viewState As Object) Dim format As New LosFormatter Dim writer As New StringWriter format.Serialize(writer, viewState) Dim viewStateStr As String = writer.ToString() Dim bytes As Byte() = System.Convert.FromBase64String(viewStateStr) bytes = vioZip.Compress(bytes) Dim vStateStr As String = System.Convert.ToBase64String(bytes) RegisterHiddenField("__VSTATE", vStateStr) End SubEnd Class Note that this sample uses SharpZipLib from ICSharpCode, but I assume you could use others, and I'd probably use System.IO.Compression if I was using .NET 2.0. The buffer sizes are hard-coded, but the only one that really matters it the one in Compress(). Again, salt to taste. Imports System.IOImports Zip = ICSharpCode.SharpZipLib.Zip.Compression'//--Download ICSharpCode.SharpZipLib from'//--http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx Public Class vioZip Shared Function Compress(ByVal bytes() As Byte) As Byte() Dim memory As New MemoryStream Dim stream = New Zip.Streams.DeflaterOutputStream(memory, _ New Zip.Deflater(Zip.Deflater.BEST_COMPRESSION), 131072) stream.Write(bytes, 0, bytes.Length) stream.Close() Return memory.ToArray() End Function Shared Function Decompress(ByVal bytes() As Byte) As Byte() Dim stream = New Zip.Streams.InflaterInputStream(New MemoryStream(bytes)) Dim memory As New MemoryStream Dim writeData(4096) As Byte Dim size As Integer While True size = stream.Read(writeData, 0, writeData.Length) If size > 0 Then memory.Write(writeData, 0, size) Else Exit While End While stream.Close() Return memory.ToArray() End FunctionEnd Class « On the Train to the Indigo SDR in Seattl... | Blog Home | TechEd Video #3 - Drink the TechEd KoolA... » About Scott Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author. About Newsletter Sponsored By Hosting By