title: Unexpected behavior with Stream.CopyToAsync ( DeflateStream )
body: <p>I am hoping that I wont have to copy the full example or create a minimal solution and the issue is some basic knowledge problem of mine.</p>
  <p>I have a application that syncs data with Azure Storage, it all works fine and the upload part is already in a Async Method. It all works.</p>
  <p>I was trying to optimize alittle more and wanted to change a stream.CopyTo to a CopyToAsync.</p>
  <pre><code>using(var filestream = streamProvider())
  {
      filestream.CopyTo(stream);
      stream.Position = 0;
  }
  </code></pre>
  <p>I changed it to await filestream.CopyToAsync(stream) and only parts of my files are upload.</p>
  <p>At the point of writing I found out that the Exception throw is a &quot;Found invalid data while decoding&quot;, IO.InvalidDataException.</p>
  <p>This lead me back to the stream being a GZip/ or DeflateStream - so guess my question is about if its not expected to work with CopyToAsync on those kind of streams?</p>
  <h3>Context</h3>
  <pre><code>public async Task UploadFile(string storePath, Func&lt;Stream&gt; streamProvider, bool copyLocalBeforeUpload = false)
  {
      using (new Timer(this.MessageQueue, &quot;UPLOAD&quot;, storePath))
      {
          using (var stream =  copyLocalBeforeUpload ? new MemoryStream() : streamProvider() )
          {
              try
              {
                  if (copyLocalBeforeUpload)
                  {
                      using (var filestream = streamProvider())
                      {
                          await filestream.CopyToAsync(stream);
                          stream.Position = 0;
                      }
                  }
  
                  var blob = GetBlobReference(storePath);
              
                  await blob.UploadFromStreamAsync(stream,new BlobRequestOptions { RetryPolicy = new LinearRetry(TimeSpan.FromMilliseconds(100), 3) });        
  
              }
              catch (Exception e)
              {
                  if (this.MessageQueue != null)
                  {
                      this.MessageQueue.Enqueue(string.Format(&quot;Failed uploading '{0}'&quot;, storePath));
                  }
              }
          }
      }
  }
  </code></pre>