GZip

Sample Code

public class Gzip {
    public static void Compress(Stream inputStream, Stream outputStream) {
        using (var gzip = new GZipStream(outputStream, CompressionMode.Compress)) {
            byte[] buffer = new byte[8192];
            int count;
            while ((count = inputStream.Read(buffer, 0, buffer.Length)) > 0) {
                gzip.Write(buffer, 0, count);
            }
        }
    }

    public static void Decompress(Stream inputStream, Stream outputStream) {
        using (var gzip = new GZipStream(inputStream, CompressionMode.Decompress)) {
            byte[] buffer = new byte[8192];
            int count;
            while ((count = gzip.Read(buffer, 0, buffer.Length)) > 0) {
                outputStream.Write(buffer, 0, count);
            }
        }
    }
}

Notes

This is generic to any stream, where you define the input and output streams and pass the references in. For example, to GZip a file into a byte array in memory, define a FileStream to open the file, and a MemoryStream to store the result.