Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have update the code there was a problem with older version.</p> <p>Here is a function which serialize and compress and viceversa. </p> <pre><code>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); } } } </code></pre> <p>Following is how to use it.</p> <pre><code> [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); } </code></pre> <p>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.</p> <p>Another suggestion try to compress bulk of data will always give better compression over individual compress objects.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload