Go Back

using DeflateStream or GZipStream

basically this is the examples of msdn thrown together, seperated by a preprocessor define (kickin it c++ old school

 
#define DEFLATE
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.IO.Compression;
 
namespace DeflateLesson2
{
 
    class Program
    {
 
        static void Main(string[] args)
        {
 
            byte[] data = new byte[10240000];
            FillDataWithZeroes(data);
            using (MemoryStream ms = new MemoryStream())
            {
#if DEFLATE
                using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Compress,true))
#else
                using (GZipStream ds = new GZipStream(ms, CompressionMode.Compress, true))
#endif
                {
                    Stuff(data, ds);
                    Console.WriteLine("Uncompressed: {0} bytes", data.Length);
                    Console.WriteLine("Compresseed: {0} bytes ", ms.Length);
 
                }
 
 
                ms.Position = 0;
#if DEFLATE
                using (DeflateStream ds = new DeflateStream(ms, CompressionMode.Decompress))
#else
                using (GZipStream ds = new GZipStream(ms, CompressionMode.Decompress))
#endif
                {
                    byte[] buffer = new byte[data.Length + 100];
                    int offset = 0;
                    int actuallyRead =0;
 
                    while (true)
                    {
 
                        int bytesRead = ds.Read(buffer, offset, 100);
                        if (bytesRead == 0) break;
                        actuallyRead += bytesRead;
                        offset += 100;
 
                    }
                    Console.WriteLine("Bytes actually read: {0} bytes", actuallyRead);
                }
            }
            Console.ReadKey();
        }
#if DEFLATE
        private static void Stuff(byte[] data, DeflateStream ds)
#else
        private static void Stuff(byte[] data, GZipStream ds)
#endif
        {
            ds.Write(data, 0, data.Length);
        }
 
        private static void FillDataWithZeroes(byte[] data)
        {
            for (int x = 0; x < data.Length; x++)
            {
                data[x] = new byte();
                data[x] = (byte)(x % 5);
            }
        }
    }
}

 

 

Facebook DZone It! Digg It! StumbleUpon Technorati Del.icio.us NewsVine Reddit Blinklist Furl it!

Post a comment!
  1. Formatting options