无法使用 GZipStream 解压缩文件(不使用 CopyToAsync)
Failed to use GZipStream to decompress a file (without using CopyToAsync)
我正在尝试使用 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();
}
gzipStream.CopyToAsync
有效,但反之则不然。为什么?
谢谢。
ReadAsync 正在返回读取的字节数 - 您忽略了该数字。您只能 WriteAsync 您首先读取的确切字节数。
我正在尝试使用 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();
}
gzipStream.CopyToAsync
有效,但反之则不然。为什么?
谢谢。
ReadAsync 正在返回读取的字节数 - 您忽略了该数字。您只能 WriteAsync 您首先读取的确切字节数。