GZip Compression & Compact Framework

Here are some utils methods to compress/decompress byte data in gzip format.  Couldn’t really find many examples to inflate (decompress) the byte data after it’s been compressed…maybe i just didn’t Google hard enought ;-).  Pretty cool library…especaily when you reduce a bitmap 80-90%!!


Note: This is only a wrapper to the main SharpZipLib library available at http://www.icsharpcode.net/OpenSource/SharpZipLib/Default.aspx so you will need that library for the utils to work.


        /// <summary>
        /// Compress the raw data using gzip compression
        /// </summary>
        /// <param name=”rawData”></param>
        /// <returns></returns>
        public static byte[] DeflateData(byte[] data)
        {        
            MemoryStream stream = new MemoryStream();
            GZipOutputStream gzipOut = new GZipOutputStream(stream);
            gzipOut.Write(data,0,data.Length);
            gzipOut.Finish();
            return stream.ToArray();
        }

        /// <summary>
        /// Decompress the data using gzip compression
        /// </summary>
        /// <param name=”data”></param>
        /// <returns></returns>
        public static byte[] InflateData(byte[] data)
        {
            MemoryStream ms = new MemoryStream();
            ms.Write(data,0,data.Length);
            ms.Seek(0,SeekOrigin.Begin);
            Stream gzin = new GZipInputStream(ms);
            MemoryStream decompMemStream = new MemoryStream();
            int size = 2048;
            byte[] buffer = new byte[size];
            while(size>0)
            {
                size = gzin.Read(buffer, 0, size);
                if (size > 0)
                    decompMemStream.Write(buffer, 0, size);
            }

            return decompMemStream.ToArray();            
        }


Warning: count(): Parameter must be an array or an object that implements Countable in /nfs/c07/h02/mnt/110221/domains/markarteaga.com/html/wp-includes/class-wp-comment-query.php on line 405

Leave a Reply