Compressing in Memory

 
using System;
using System.IO;
using System.IO.Compression;

using System.Linq;
using System.Text;

class Program
{
    static void Main()
    {
        byte[] data = new byte[1000]; 
        var ms = new MemoryStream();
        using (Stream ds = new DeflateStream(ms, CompressionMode.Compress))
            ds.Write(data, 0, data.Length);

        byte[] compressed = ms.ToArray();
        Console.WriteLine(compressed.Length); // 113

        // Decompress back to the data array:
        ms = new MemoryStream(compressed);
        using (Stream ds = new DeflateStream(ms, CompressionMode.Decompress))
            for (int i = 0; i < 1000; i += ds.Read(data, i, 1000 - i)) ;
    }
}
  

The output:


117
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.