files

Code Snippets

Title: GZip compression
Description: GZip compression class
Category: C# Compression
Visibility:  Public
Added by: Patrick Goldbach on 5/6/2012

 Currently rated 5 by 3 user(s)

Get link for this code snippet
Collapse code snippet
Expand code snippet
Tweet this snippet
Tweet about this snippet
Delete code snippet
Edit code snippet
using System.IO;
using System.IO.Compression;

public static class GZip
    {
        #region Static Functions
        /// <summary>
        /// Compresses byte data
        /// </summary>
        /// <param name="bytes">Byte array to be compressed</param>
        /// <returns>A byte array of compressed data</returns>
        public static byte[] Compress(byte[] bytes)
        {
            var stream = new MemoryStream();
            var zipStream = new GZipStream(stream, CompressionMode.Compress, true);
            zipStream.Write(bytes, 0, bytes.Length);
            zipStream.Close();
            return stream.ToArray();
        }

        /// <summary>
        /// Decompresses byte data
        /// </summary>
        /// <param name="bytes">Byte array to be decompressed</param>
        /// <returns>A byte array of decompressed data</returns>
        public static byte[] Decompress(byte[] bytes)
        {
            var stream = new MemoryStream();
            var zipStream = new GZipStream(new MemoryStream(bytes), CompressionMode.Decompress, true);
            var buffer = new byte[4096];
            while (true)
            {
                int size = zipStream.Read(buffer, 0, buffer.Length);
                if (size > 0)
                {
                    stream.Write(buffer, 0, size);
                }
                else
                {
                    break;
                }
            }
            zipStream.Close();
            return stream.ToArray();
        }

        #endregion
    }

Title: Deflate comrpession
Description: Utility class used for compressing data suing deflate
Category: C# Compression
Visibility:  Public
Added by: Patrick Goldbach on 5/6/2012

 Currently rated 5 by 2 user(s)

Get link for this code snippet
Collapse code snippet
Expand code snippet
Tweet this snippet
Tweet about this snippet
Delete code snippet
Edit code snippet
using System.IO;
using System.IO.Compression;   

    public static class Deflate
    {
        #region Static Functions
        /// <summary>
        /// Compresses data
        /// </summary>
        /// <param name="bytes">The byte array to be compressed</param>
        /// <returns>A byte array of compressed data</returns>
        public static byte[] Compress(byte[] bytes)
        {
            var stream = new MemoryStream();
            var zipStream = new DeflateStream(stream, CompressionMode.Compress, true);
            zipStream.Write(bytes, 0, bytes.Length);
            zipStream.Close();
            return stream.ToArray();
        }

        /// <summary>
        /// Decompresses data
        /// </summary>
        /// <param name="bytes">The byte array to be decompressed</param>
        /// <returns>A byte array of uncompressed data</returns>
        public static byte[] Decompress(byte[] bytes)
        {
            var stream = new MemoryStream();
            var zipStream = new DeflateStream(new MemoryStream(bytes), CompressionMode.Decompress, true);
            var buffer = new byte[4096];
            while (true)
            {
                var size = zipStream.Read(buffer, 0, buffer.Length);
                if (size > 0) stream.Write(buffer, 0, size);
                else break;
            }
            zipStream.Close();
            return stream.ToArray();
        }
        #endregion
    }