Decompress a byte[] with Gzip - CSharp System.IO.Compression

CSharp examples for System.IO.Compression:GZip

Description

Decompress a byte[] with Gzip

Demo Code


using System.IO.Compression;
using System.IO;//from   w w w  . ja  va  2 s  .com
using System;

public class Main{
        /// <summary>
        /// Decompress a byte[].
        /// </summary>
        /// <param name="gzBuffer"></param>
        /// <returns>byte array decompressed.</returns>
        public static byte[] Decompress(byte[] gzBuffer)
        {
            var ms = new MemoryStream();

            var msgLength = BitConverter.ToInt32(gzBuffer, 0);

            ms.Write(gzBuffer, 4, gzBuffer.Length - 4);

            var buffer = new byte[msgLength];

            ms.Position = 0;

            var zip = new GZipStream(ms, CompressionMode.Decompress);

            zip.Read(buffer, 0, buffer.Length);

            return buffer;
        }
}

Related Tutorials