Use the compressed stream in 2.0 to reduce the amount of network transmission

Source: Internet
Author: User

Public static byte [] compress (Dataset DS)
{
Binaryformatter SER = new binaryformatter ();
Memorystream MS = new memorystream ();
// Binary is serialized as binary content. It can only be used in ADO. NET 2.0
DS. remotingformat = serializationformat. Binary;
// Serialize to memorystream
Ser. serialize (MS, DS );
// Compressed memorystream
Memorystream compressms = new memorystream ();
// Write the compressed memorystream
Gzipstream compressedstream = new gzipstream (compressms, compressionmode. Compress, true );
Compressedstream. Write (Ms. toarray (), 0, ms. toarray (). Length );
Return compressms. toarray ();

}

Public static dataset decompress (byte [] data)
{< br> memorystream uncompressms = new memorystream ();
memorystream compressms = new memorystream (data );
gzipstream decompressedstream = new gzipstream (uncompressms, compressionmode. decompress, true);
decompressedstream. write (compressms. toarray (), 0, compressms. toarray (). length);
binaryformatter SER = new binaryformatter ();
return (Dataset) Ser. deserialize (uncompressms);
}

 

After comparison, there are obvious advantages in using compression and not using compression, which is about 10 of the traffic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.