Argentini.Halide.H3Compress.CompressString C# (CSharp) Method

CompressString() public static method

GZip compress a string. Only useful for strings over 300 characters in length.
public static CompressString ( string text ) : String
text string String to compress.
return String
        public static String CompressString(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;

            byte[] compressed = new byte[ms.Length];
            ms.Read(compressed, 0, compressed.Length);

            byte[] gzipBuffer = new byte[compressed.Length + 4];
            System.Buffer.BlockCopy(compressed, 0, gzipBuffer, 4, compressed.Length);
            System.Buffer.BlockCopy(BitConverter.GetBytes(buffer.Length), 0, gzipBuffer, 0, 4);
            return Convert.ToBase64String(gzipBuffer);
        }