Sep 09

GZip Compression and Decompression in C#

In .NET GZipStream class in System.IO.Compression provides methods and properties to compress and decompress streams.

Below example shows how to compress and decompress large binary data

 

CompressorHelper.cs
public class CompressorHelper
    {
        public static byte[] DecompressGZip(byte[] bytesToDecompress)
        {
            using (var stream = new GZipStream(new MemoryStream(bytesToDecompress), CompressionMode.Decompress))
            {
                const int size = 4096;
                var buffer = new byte[size];
                using (var memoryStream = new MemoryStream())
                {
                    int count;
                    do
                    {
                        count = stream.Read(buffer, 0, size);
                        if (count > 0)
                        {
                            memoryStream.Write(buffer, 0, count);
                        }
                    } while (count > 0);
                    return memoryStream.ToArray();
                }
            }
        }

        public static byte[] CompressGZip(string input, Encoding encoding = null)
        {
            encoding = encoding ?? Encoding.Unicode;
            var bytes = encoding.GetBytes(input);
            using (var stream = new MemoryStream())
            {
                using (var zipStream = new GZipStream(stream, CompressionMode.Compress))
                {
                    zipStream.Write(bytes, 0, bytes.Length);
                    return stream.ToArray();
                }
            }
        }
    }

 

Suppose if you want to compress and decompress file then use FileCompressorHelper

 

FileCompressorHelper.cs
public class FileCompressorHelper
    {
        public static void Compress(FileInfo fileToCompress)
        {
            using (var fileStream = fileToCompress.OpenRead())
            {
                if ((File.GetAttributes(fileToCompress.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden & fileToCompress.Extension != ".gz")
                {
                    using (var compressedFileStream = File.Create(fileToCompress.FullName + ".gz"))
                    {
                        using (var compressionStream = new GZipStream(compressedFileStream, CompressionMode.Compress))
                        {
                            fileStream.CopyTo(compressionStream);
                        }
                    }

                    var fileInfo = new FileInfo("D:\\" + fileToCompress.Name + ".gz");
                }
            }
        }

        public static string Decompress(FileInfo fileToDecompress)
        {
            using (var fileStream = fileToDecompress.OpenRead())
            {
                string currentFileName = fileToDecompress.FullName;
                string newFileName = currentFileName.Remove(currentFileName.Length - fileToDecompress.Extension.Length);

                using (var decompressedFileStream = File.Create(newFileName))
                {
                    using (var decompressionStream = new GZipStream(fileStream, CompressionMode.Decompress))
                    {
                        decompressionStream.CopyTo(decompressedFileStream);
                       return fileToDecompress.Name;
                    }
                }
            }
        }
    }

Leave a Reply