I was just looking at sample code for string compression. I believe that using the GZipStream class is enough. But I do not understand why we should convert it to a base 64 line, as shown in the example.
using System.IO.Compression; using System.Text; using System.IO; public static string Compress(string text) { byte[] buffer = Encoding.UTF8.GetBytes(text); MemoryStream ms = new MemoryStream(); using (GZipStream zip = new GZipStream(ms, CompressionMode.Compress, true)) { zip.Write(buffer, 0, buffer.Length); } ms.Position = 0; MemoryStream outStream = new MemoryStream(); byte[] compressed = new byte[ms.Length]; ms.Read(compressed, 0, compressed.Length); byte[] gzBuffer = new byte[compressed.Length + 4]; System.Buffer.BlockCopy(compressed, 0, gzBuffer, 4, compressed.Length); System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzBuffer, 0, 4); return Convert.ToBase64String (gzBuffer); }
Also, I donβt understand that gzBuffer is initialized with compressed.Length + 4 size. Actually, I donβt understand why we have the last few statements. Can someone tell a little light ...
PS: I'm not a computer science student.
deostroll Jul 12 '10 at 9:10 2010-07-12 09:10
source share