Zip format compression, decompression (C #)

Source: Internet
Author: User

Compression method

        #regionCompression/// <summary>        ///Compression/// </summary>        /// <param name= "bytes" >non-compressed byte data</param>        /// <returns></returns>         Public Static byte[] Compress (byte[] bytes) {            if(bytes = =NULL)                return NULL; using(MemoryStream OStream =NewMemoryStream ())//Compression{GZipStream ZipStream=NewGZipStream (OStream, compressionmode.compress); Zipstream.write (Bytes,0, Bytes.                Length);                Zipstream.flush ();                Zipstream.close (); returnOstream.toarray (); }        }        /// <summary>        ///Compression/// </summary>        /// <param name= "dataoriginal" >data that has not been compressed</param>        /// <returns></returns>         Public Static byte[] Compress (Objectdataoriginal) {            if(Dataoriginal = =NULL)                return NULL; byte[] bytes =NULL; using(MemoryStream mstream =NewMemoryStream ())//serializing into binary arrays{BinaryFormatter Bformatter=NewBinaryFormatter ();                Bformatter.serialize (Mstream, dataoriginal); Bytes=Mstream.toarray (); }            returnCompress (bytes); }        /// <summary>        ///Compress string/// </summary>        /// <param name= "Unzipdata" >data that has not been compressed</param>        /// <returns></returns>         Public Static stringCompress (stringunzipdata) {            if(string. IsNullOrEmpty (unzipdata))return NULL; varbytes = Compress (unzipdata as Object); if(bytes = =NULL)                return NULL; returnConvert.tobase64string (bytes);//Be sure to Base64, or it will go wrong.        }        #endregion
View Code

Decompression method

        #regionUnzip/// <summary>        ///Unzip/// </summary>        /// <param name= "Stream" >Flow</param>        /// <returns></returns>         Public Static ObjectDecompress (Stream stream) {stream. Seek (0, Seekorigin.begin); GZipStream Unzipstream=NewGZipStream (Stream, compressionmode.decompress,true); ObjectDsresult =NULL; BinaryFormatter Bformatter=NewBinaryFormatter (); Dsresult= Bformatter.deserialize (Unzipstream) as Object;            Unzipstream.close (); returnDsresult; }        /// <summary>        ///Unzip/// </summary>        /// <param name= "bytes" >compressed array of bytes</param>        /// <returns></returns>         Public Static ObjectDecompress (byte[] bytes) {            if(bytes = =NULL)                 return NULL; using(MemoryStream mstream =NewMemoryStream (bytes)) {                returnDecompress (Mstream); }                    }        /// <summary>        ///extracting a string/// </summary>        /// <param name= "Zipdata" >the compressed string</param>        /// <returns></returns>         Public Static stringDecompress (stringzipdata) {            if(string. IsNullOrEmpty (zipdata))return NULL; varbytes = convert.frombase64string (zipdata);//Be sure to Base64, or it will go wrong.            returnDecompress (bytes) as string; }        #endregion
View Code

String compression, decompression byte array and string of each other to use BASE64 encoding!

Zip format compression, decompression (C #)

Related Article

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.