title: Crash safe on-the-fly compression with GZipStream
body: <p>I'm compressing a log file as data is written to it, something like:</p>
  
  <pre><code>using (var fs = new FileStream("Test.gz", FileMode.Create, FileAccess.Write, FileShare.None))
  {
    using (var compress = new GZipStream(fs, CompressionMode.Compress))
    {
      for (int i = 0; i &lt; 1000000; i++)
      {
        // Clearly this isn't what is happening in production, just 
        // a simply example
        byte[] message = RandomBytes();
        compress.Write(message, 0, message.Length);
  
        // Flush to disk (in production we will do this every x lines, 
        // or x milliseconds, whichever comes first)
        if (i % 20 == 0)
        {
          compress.Flush();
        }
      }
    }
  }
  </code></pre>
  
  <p>What I want to ensure is that if the process crashes or is killed, the archive is still valid and readable. I had hoped that anything since the last flush would be safe, but instead I am just ending up with a corrupt archive.</p>
  
  <p>Is there any way to ensure I end up with a readable archive after each flush?</p>
  
  <p>Note: it isn't essential that we use GZipStream, if something else will give us the desired result.</p>