title: C# Compressing a lot of data blocks fast/efficiently
body: <p>I have around 270k data block pairs, each pair consists of one 32KiB and one 16KiB block.  </p>
  
  <p>When I save them to one file I of course get a very large file.
  But the data is easily compressed.<br>
  After compressing the 5.48GiB file with WinRAR, with strong compression, the resulting file size is 37.4MiB.  </p>
  
  <p>But I need random access to each individual block, so I can only compress the blocks individually.<br>
  For that I used the Deflate class provided by .NET, which reduced the file size to 382MiB (which I could live with).<br>
  But the speed is not good enough.</p>
  
  <p>A lot of the speed loss is probably due to always creating a new MemoryStream and Deflate instance for each block.
  But it seems they aren't designed to be reused.</p>
  
  <p>And I guess (much?) better compression can be achieved when a "global" dictionary is used instead having one for each block.</p>
  
  <p>Is there an implementation of a compression algorithm (preferably in C#) which is suited for that task?</p>
  
  <p>The following link contains the percentage with which each byte number occurs, divided into three block types (32KiB blocks only).
  The first and third block type has an occurrence of 37,5% and the second 25%.
  <a href="http://arokh.dnsalias.org/SO/Freq.txt" rel="noreferrer">Block type percentages</a></p>
  
  <p>Long file short story:
  Type1 consists mostly of ones.
  Type2 consists mostly of zeros and ones
  Type3 consists mostly of zeros
  Values greater than 128 do not occur (yet).</p>
  
  <p>The 16KiB block consists almost always of zeros</p>