title: Writing to ZipArchive using the HttpContext OutputStream
body: <p>I've been trying to get the "new" ZipArchive included in .NET 4.5 (<code>System.IO.Compression.ZipArchive</code>) to work in a ASP.NET site. But it seems like it doesn't like writing to the stream of <code>HttpContext.Response.OutputStream</code>.</p>
  
  <p>My following code example will throw </p>
  
  <blockquote>
    <p>System.NotSupportedException: Specified method is not supported</p>
  </blockquote>
  
  <p>as soon as a write is attempted on the stream.</p>
  
  <p>The <code>CanWrite</code> property on the stream returns true.</p>
  
  <p>If I exchange the OutputStream with a filestream, pointing to a local directory, it works. What gives?</p>
  
  <pre><code>ZipArchive archive = new ZipArchive(HttpContext.Response.OutputStream, ZipArchiveMode.Create, false);
  
  ZipArchiveEntry entry = archive.CreateEntry("filename");
  
  using (StreamWriter writer = new StreamWriter(entry.Open()))
  {
      writer.WriteLine("Information about this package.");
      writer.WriteLine("========================");
  }
  </code></pre>
  
  <p>Stacktrace:</p>
  
  <pre class="lang-none prettyprint-override"><code>[NotSupportedException: Specified method is not supported.]
  System.Web.HttpResponseStream.get_Position() +29
  System.IO.Compression.ZipArchiveEntry.WriteLocalFileHeader(Boolean isEmptyFile) +389
  System.IO.Compression.DirectToArchiveWriterStream.Write(Byte[] buffer, Int32 offset, Int32 count) +94
  System.IO.Compression.WrappedStream.Write(Byte[] buffer, Int32 offset, Int32 count) +41
  </code></pre>