0

I'm trying to to decompress a MemoryStream using ReadAsync/WriteAsync but it's not working.

int bufferSize = 8192;
using (var memoryStream = new MemoryStream())
using (var fileStream = new FileStream(destinationFilename, FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
    // ... populate the MemoryStream ...
    memoryStream.Position = 0;

    using (var gzipStream = new GZipStream(memoryStream, CompressionMode.Decompress, true))
    {
        ////await gzipStream.CopyToAsync(fileStream);

        byte[] buffer = new byte[bufferSize];

        while (await gzipStream.ReadAsync(buffer, 0, bufferSize) > 0)
        {
            await fileStream.WriteAsync(buffer, 0, bufferSize);
        }
    }

    await fileStream.FlushAsync();
}

The gzipStream.CopyToAsync works but not the other way. Why?

Thanks.

pellea
  • 359
  • 3
  • 21

1 Answers1

0

ReadAsync is returning number of bytes read - you're ignoring that number. You can only WriteAsync the exact count of bytes you've read first.

Ondrej Svejdar
  • 21,349
  • 5
  • 54
  • 89