简体   繁体   English

无法使用GZipStream解压缩文件(不使用CopyToAsync)

[英]Failed to use GZipStream to decompress a file (without using CopyToAsync)

I'm trying to to decompress a MemoryStream using ReadAsync / WriteAsync but it's not working. 我正在尝试使用ReadAsync / WriteAsync解压缩MemoryStream但是它不起作用。

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. gzipStream.CopyToAsync可以正常工作,但不能使用其他方法。 Why? 为什么?

Thanks. 谢谢。

ReadAsync is returning number of bytes read - you're ignoring that number. ReadAsync返回读取的字节数-您忽略了该数字。 You can only WriteAsync the exact count of bytes you've read first. 您只能WriteAsync首先读取的确切字节数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM