SimpleCompressor.cs

/*
 * Created by SharpDevelop.
 * User: Petteri Kautonen
 * Date: 26.11.2006
 * Time: 20:58
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
using ICSharpCode.SharpZipLib.Zip.Compression;
using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

namespace VPKSoft
{
    /// <summary>
    /// Description of SimpleCompressor.
    /// </summary>
    public class SimpleCompressor
    {
        public SimpleCompressor()
        {
        }
        
        public static bool SkipHeaders = false;
        
        public static byte [] Compress(byte [] Bytes)
        {
            MemoryStream Ms = new MemoryStream();
            Deflater Def = new Deflater(9, SkipHeaders);
            DeflaterOutputStream Dos = new DeflaterOutputStream(Ms, Def);
            Dos.Write(Bytes, 0, Bytes.Length);
            Dos.Flush();
            Dos.Finish();
            Dos.Close();
            return Ms.GetBuffer();            
        }

        public static byte [] Decompress(byte [] Bytes)
        {
            MemoryStream Ms = new MemoryStream();
            MemoryStream Ms2 = new MemoryStream();
            Ms.Write(Bytes, 0, Bytes.Length);
            Ms.Position = 0;
            Inflater Inf = new Inflater(SkipHeaders);
            InflaterInputStream Iis = new InflaterInputStream(Ms, Inf);
            byte [] Buffer = new byte [Bytes.Length];
            int Length;
            do {
                Length = Iis.Read(Buffer, 0, Buffer.Length);
                Ms2.Write(Buffer, 0, Length);
            } while (Length > 0);
            
            Iis.Close();
            Ms.Close();
            return Ms2.GetBuffer();
        }
    }
}