Dataset gzip/deflate serializer

Source: Internet
Author: User
Namespace test {using system. diagnostics; using system. web; using system. web. services; using system. data; using system; using microshaoft; [WebService (namespace = "http://www.microshaoft.com/")] public class service1class: WebService {[webmethod] Public String helloworld () {return "Hello World";} [webmethod] public byte [] getdata () {dataset DS = new dataset (); // populate dataset to do return datasetserializer. datasettogzipbytes (DS);} static void main () {// todo: add Code To start the application Program // Byte [] DATA = datasetserializer. datasettogzipbytes (new dataset (); console. writeline (data. length); dataset DS = datasetserializer. gzipbytestodataset (data); console. writeline ("Hello World"); console. writeline (environment. version. tostring () ;}} namespace microshaoft {using system; using system. io; using system. data; using system. runtime. serialization; using system. runtime. serialization. formatter S. binary; public class datasetserializer {// server static public byte [] datasettogzipbytes (Dataset dataset) {memorystream MS = new memorystream (); iformatter F = new binaryformatter (); dataset. remotingformat = serializationformat. binary; F. serialize (MS, dataset); byte [] DATA = Ms. toarray (); Ms. close (); Ms. dispose (); MS = NULL; return compresshelper. gzipcompress (data);} // client static public dat Aset gzipbytestodataset (byte [] data) {DATA = compresshelper. gzipdecompress (data); memorystream MS = new memorystream (data); iformatter F = new binaryformatter (); dataset = f. deserialize (MS) as dataset; Ms. close (); Ms. dispose (); MS = NULL; return dataset ;}} namespace microshaoft {using system; using system. text; using system. io; using system. io. compression; // <summary> // Summary of crypto. /// </Summary> public class compresshelper {Private Static byte [] readstreamtobytes (Stream stream) {byte [] buffer = new byte [64*1024]; memorystream MS = new memorystream (); int r = 0; int L = 0; long position =-1; if (stream. canseek) {position = stream. position; stream. position = 0;} while (true) {r = stream. read (buffer, 0, buffer. length); If (r> 0) {L + = r; Ms. write (buffer, 0, R) ;}else {break ;}} byte [] bytes = new byte [l]; Ms. position = 0; Ms. read (bytes, 0, (INT) L); Ms. close (); MS = NULL; If (position> = 0) {stream. position = position;} return bytes;} public static byte [] gzipcompress (byte [] data) {memorystream MS = new memorystream (); gzipstream stream = new gzipstream (MS, compressionmode. compress, true); stream. write (data, 0, Data. length); stream. close (); stream. dispose (); stream = NULL; byte [] buffer = readstreamtobytes (MS); Ms. close (); Ms. dispose (); MS = NULL; return buffer;} public static byte [] gzipdecompress (byte [] data) {memorystream MS = new memorystream (data ); gzipstream stream = new gzipstream (MS, compressionmode. decompress); byte [] buffer = readstreamtobytes (Stream); Ms. close (); Ms. dispose (); MS = NULL; stream. close (); stream. dispose (); stream = NULL; return buffer;} public static byte [] deflatecompress (byte [] data) {memorystream MS = new memorystream (); deflatestream stream = new deflatestream (MS, compressionmode. compress, true); stream. write (data, 0, Data. length); stream. close (); stream. dispose (); stream = NULL; byte [] buffer = readstreamtobytes (MS); Ms. close (); Ms. dispose (); MS = NULL; return buffer;} public static byte [] deflatedecompress (byte [] data) {memorystream MS = new memorystream (data ); deflatestream stream = new deflatestream (MS, compressionmode. decompress); byte [] buffer = readstreamtobytes (Stream); Ms. close (); Ms. dispose (); MS = NULL; stream. close (); stream. dispose (); stream = NULL; return buffer ;}}}

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.