A extension class for string to add GZip features.

Copy the following code to your project first.

    using System;
    using System.IO;
    using System.IO.Compression;
    using System.Text;

    public static class GZipExtensions
    {
        private static byte[] Zip(string str)
        {
            var bytes = Encoding.UTF8.GetBytes(str);

            using (var msi = new MemoryStream(bytes))
            using (var mso = new MemoryStream())
            {
                using (var gs = new GZipStream(mso, CompressionMode.Compress))
                {
                    msi.CopyTo(gs);
                }

                return mso.ToArray();
            }
        }

        private static string Unzip(byte[] bytes)
        {
            using (var msi = new MemoryStream(bytes))
            using (var mso = new MemoryStream())
            {
                using (var gs = new GZipStream(msi, CompressionMode.Decompress))
                {
                    gs.CopyTo(mso);
                }

                return Encoding.UTF8.GetString(mso.ToArray());
            }
        }

        public static string UnZipBase64(this string zippedBase64)
        {
            var zippedBytes = Convert.FromBase64String(zippedBase64);
            return Unzip(zippedBytes);
        }

        public static string ZipToBase64(this string sourceString)
        {
            var zippedBytes = Zip(sourceString);
            return Convert.ToBase64String(zippedBytes);
        }
    }

 

When you need to use it:

        [TestMethod]
        public void Temp()
        {
            var myString = "StringStringStringStringStringStringStringString";
            var zipped = myString.ZipToBase64();

            var unzipped = zipped.UnZipBase64();

            Assert.AreEqual(unzipped, myString);
            Assert.IsTrue(zipped.Length < myString.Length);
        }