Data stream compression code

Source: Internet
Author: User
Using system; using system. collections. generic; using system. text; using system. io; using system. io. compression; namespace commonutility. compressionutility {public class compressionutility {// <summary> // read bytes from the data stream /// </Summary> /// <Param name = "stream"> </Param >/// <returns> </returns> Public static byte [] readstreamtobytes (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;} // <summary> // Gzip algorithm Compress data /// </Summary> /// <Param name = "data"> </param> /// <returns> </returns> 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 ;} /// <summary> /// decompress the data using the gzip algorithm /// </Summary> /// <Param name = "data"> </param> /// <returns> </returns> 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 ;} /// <summary> /// deflate algorithm compresses data /// </Summary> /// <Param name = "data"> </param> // <returns> </returns> 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 ;} /// <summary> /// decompress the data using the deflate algorithm /// </Summary> /// <Param name = "data"> </param> /// <returns> </returns> 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.