String - CompressGZip
A string extension method that compress the given string to GZip byte array.
public static string FileName = "test.txt"; public static void Main() { string phrase = "The quick brown fox jumps over the lazy dog."; byte[] bytesBeforeCompression = phrase.ToByteArray(); //C# Extension Method: String - CompressGZip byte[] bytesAfterCompression = phrase.CompressGZip(); var orginalBytes = Decompress(bytesAfterCompression); var file = FileName.ToFileInfo(); file.WriteAllBytes(orginalBytes); Console.WriteLine(file.ReadAllText()); } public static byte[] Decompress(byte[] data) { using (var compressedStream = new MemoryStream(data)) { using (var zipStream = new GZipStream(compressedStream, CompressionMode.Decompress)) { using (var resultStream = new MemoryStream()) { zipStream.CopyTo(resultStream); return resultStream.ToArray(); } } } }
View Source
using System.IO; using System.IO.Compression; using System.Text; public static partial class Extensions { /// <summary> /// A string extension method that compress the given string to GZip byte array. /// </summary> /// <param name="this">The stringToCompress to act on.</param> /// <returns>The string compressed into a GZip byte array.</returns> public static byte[] CompressGZip(this string @this) { byte[] stringAsBytes = Encoding.Default.GetBytes(@this); using (var memoryStream = new MemoryStream()) { using (var zipStream = new GZipStream(memoryStream, CompressionMode.Compress)) { zipStream.Write(stringAsBytes, 0, stringAsBytes.Length); zipStream.Close(); return (memoryStream.ToArray()); } } } /// <summary> /// A string extension method that compress the given string to GZip byte array. /// </summary> /// <param name="this">The stringToCompress to act on.</param> /// <param name="encoding">The encoding.</param> /// <returns>The string compressed into a GZip byte array.</returns> public static byte[] CompressGZip(this string @this, Encoding encoding) { byte[] stringAsBytes = encoding.GetBytes(@this); using (var memoryStream = new MemoryStream()) { using (var zipStream = new GZipStream(memoryStream, CompressionMode.Compress)) { zipStream.Write(stringAsBytes, 0, stringAsBytes.Length); zipStream.Close(); return (memoryStream.ToArray()); } } } }