views:

180

answers:

4

Hi,

I want to Compress an Object in dot net to reduce its size and then UnCompress it on in my client application.

Thanks, Mrinal Jaiswal

A: 

Serialize it simply by adding the following above your class: (maybe take a look at: http://blog.kowalczyk.info/article/Serialization-in-C.html to fully understand how it works.)

[Serializable]
class Whatever
Kolky
Er - serialize != compress.
Pure.Krome
+1  A: 

You can always GZip it.

David Hedlund
A: 

I suppose you need to improve the serialization procedure, by compressing the contained data. Once I needed that in .NET, I used SoapExtensions, but you can also use httpmodule's functionality like msdn proposed:

//overriding the GetWebRequest method in the Web service proxy
protected override WebRequest GetWebRequest(Uri uri)
{
  WebRequest request = base.GetWebRequest(uri);
  request.Headers.Add("Accept-Encoding", "gzip, deflate");
  return request;
}
//overriding the GetWebResponse method in the Web service proxy
protected override WebResponse GetWebResponse(WebRequest request)
{
  //decompress the response from the Web service
  return response;
}
Aggelos Mpimpoudis
+1  A: 

I have update the code there was a problem with older version.

Here is a function which serialize and compress and viceversa.

public static byte[] SerializeAndCompress(object obj) {

    using (MemoryStream ms = new MemoryStream()) {
        using (GZipStream zs = new GZipStream(ms, CompressionMode.Compress, true)) {
            BinaryFormatter bf = new BinaryFormatter();
                bf.Serialize(zs, obj);
        }
        return ms.ToArray();
    }
}
public static object DecompressAndDeserialze(byte[] data) {

    using (MemoryStream ms = new MemoryStream(data)) {
        using (GZipStream zs = new GZipStream(ms, CompressionMode.Decompress, true)) {
            BinaryFormatter bf = new BinaryFormatter();
            return bf.Deserialize(zs);
        }
    }
}

Following is how to use it.

    [Serializable]
    class MyClass
    {
        public string Name { get; set; }
    }

    static void Main(string[] args) {
        MyClass myClassInst = new MyClass();
        myClassInst.Name = "Some Data";

        byte[] data= SerializeAndCompress(myClassInst);
        MyClass desInst = (MyClass)DecompressAndDeserialze(data);

    }

But there is a catch to compression. Remember the above example object is serialize to 153 bytes but the compress version is 266 bytes the reason is that if have small objects with less data then the gzip header information and compression header will at least take 120bytes. So if your object are big enough than compress them if they are just less 300 bytes or so its no need to compress them. You can check compression ratio and see if you object even require compression.

Another suggestion try to compress bulk of data will always give better compression over individual compress objects.

affan
Hi affan,Your code is not working. Its giving exception at line"return bf.Deserialize(zs);" Exception is "End of Stream encountered before parsing was completed"Can you help in thisThanks,Mrinal Jaiswal
mickey mouse
Yes you right there was problem with GZipStream Flush()function. It seems that it need to be disposed or closed first so that it can finish writing to memory stream. Update code is much better than older version anyway.
affan