Compressing a file using GZipStream

 string file = @"C:\windows\temp\file.txt";

using (FileStream inFile = File.OpenRead(file))
{
    using (FileStream outFile = File.Create(
        Path.GetFullPath(file) + ".gz"))
    {
        using (GZipStream Compress = new GZipStream(outFile,
                CompressionMode.Compress))
        {
            byte[] buffer = new byte[4096];
            int numRead;
            while ((numRead = inFile.Read(buffer, 0, buffer.Length)) != 0)
            {
                Compress.Write(buffer, 0, numRead);
            }
        }
    }
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s