classProgram {Static voidMain (string[] args) { //the string to compress stringdata ="13800138000, Verification code: 1234 "Huaxin" \r\n13800138000, Captcha: 4567 "Huaxin""; byte[] buffer =System.Text.UTF8Encoding.UTF8.GetBytes (data); //the compressed byte array byte[] Compressedbuffer =NULL; //Compress buffer, compressed cacheMemoryStream ms =NewMemoryStream (); using(GZipStream ZS =NewGZipStream (MS, Compressionmode.compress,true) ) {Zs. Write (Buffer,0, buffer. Length); //The following two commented out code is problematic, the corresponding Compressedbuffer length only 10--the 10 bytes should only compress the header of buffer//ZS. Flush (); //Compressedbuffer = Ms. ToArray (); } //only GZipStream should respond to Memorystream.toarray () after dispose of the buffer is the result we needCompressedbuffer =Ms. ToArray (); //basse64 string After compressing a byte array stringText64 =convert.tobase64string (Compressedbuffer); Console.WriteLine (Text64); Console.readkey (); } }
. NET string gzip compression and Base64string conversions: