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();            
        }


Tuesday, January 04, 2005 2:18:52 AM (Eastern Standard Time, UTC-05:00) #    Comments [0]  | 

 

All content © 2008, Mark Arteaga
Related Sites
Archives
Sitemap
Disclaimer

Powered by: newtelligence dasBlog 1.9.7174.0

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

Send mail to the author(s) E-mail

Theme design by Jelle Druyts