C# Can';t使用GZipStream解压数据而不出现异常,can';无法确定原因

C# Can';t使用GZipStream解压数据而不出现异常,can';无法确定原因,c#,.net,gzip,blazor,gzipinputstream,C#,.net,Gzip,Blazor,Gzipinputstream,在我正在进行的Blazor WebAssembly项目中,我正在压缩后端WebAPI服务器上的数据,并将其传递给前端Blazor服务器(运行Blazor WebAssembly,而不是“Blazor server”,这是一个令人困惑的术语),然后将其交付给用户,然后用户对其进行解压缩。我遇到了以下异常: System.IO.IOException:读取的内部数据损坏 System.IO.Compression.DeflateStreamNative.CheckResult(System.Int3

在我正在进行的Blazor WebAssembly项目中,我正在压缩后端WebAPI服务器上的数据,并将其传递给前端Blazor服务器(运行Blazor WebAssembly,而不是“Blazor server”,这是一个令人困惑的术语),然后将其交付给用户,然后用户对其进行解压缩。我遇到了以下异常:

System.IO.IOException:读取的内部数据损坏 System.IO.Compression.DeflateStreamNative.CheckResult(System.Int32 结果,System.String,其中)位于:0的System.IO.Compression.DeflaTestStreamNative.ReadZStream (System.IntPtr缓冲区,System.Int32长度)英寸 :0 at System.IO.Compression.DeflateStream.ReadInternal(System.Byte[]数组, System.Int32偏移量,System.Int32计数) :0在System.IO.Compression.DeflateStream.Read处 (System.Byte[]数组,System.Int32偏移量,System.Int32计数) in:0 at System.IO.Compression.GZipStream.Read(System.Byte[]数组, System.Int32偏移量,System.Int32计数) :0 at OptionalyticsFrontend.Shared.Services.Compression.DecompressString (系统字节[]输入)[0x0001b]输入 C:\Users\codef\source\repos\OptionalyticsFrontend\OptionalyticsFrontend\Shared\Services\Compression.cs:24 在OptionalyticsFrontend.Client.Pages.Application.GetFilteredOptions ()[0x00183]英寸 C:\Users\codef\source\repos\OptionalyticsFrontend\OptionalyticsFrontend\Client\Pages\Application.razor:310

我已经一遍又一遍地查看了指定的行,并且查看了Google和Stack Overflow一段时间,但没有弄清楚。以下是在后端和前端使用的压缩/解压缩方法:

public static string DecompressString(this byte[] input)
{
    var result = new byte[input.Length];
    using (var source = new MemoryStream(input))
    {
        using (var decompressionStream = new GZipStream(source,
            CompressionMode.Decompress))
        {
            decompressionStream.Read(result, 0, input.Length); // <---------- THIS IS THE ERROR LINE
        }
    }
    return Encoding.UTF8.GetString(result);
}

public static byte[] CompressString(this string inputString)
{
    byte[] input = Encoding.UTF8.GetBytes(inputString);
    using (var result = new MemoryStream())
    {
        using (var compressionStream = new GZipStream(result,
            CompressionMode.Compress))
        {
            compressionStream.Write(input, 0, input.Length);
            compressionStream.Flush();

        }
        return result.ToArray();
    }
}
我已经查看并尝试实现了以下堆栈溢出答案,但没有一个有效,我每次都会遇到相同的异常

这是怎么回事?我以.NET标准和.NET核心运行前端Blazor服务器(实际的客户端和共享代码是.NET标准,服务器端是.NET核心)

编辑:正如Andy的评论所问,我添加了一个新的流,并尝试复制到新的流,但这会导致相同的错误

public static string DecompressString(byte[] input)
{
    var result = new byte[input.Length];
    using (var newStream = new MemoryStream())
    {
        using (var source = new MemoryStream(input))
        {
            using (var decompressionStream = new GZipStream(source,
                CompressionMode.Decompress))
            {
                decompressionStream.CopyTo(newStream);
            }
        }
        newStream.Position = 0;
        return Encoding.UTF8.GetString(newStream.ToArray());
    }
}

显然,问题在于,在Application.razor文件中,我使用
await response.Content.ReadAsByteArrayAsync()
而不是
await response.Content.ReadAsAsync()
,回叫我的前端服务器,您需要后者,因为前者以一种您不一定期望的方式转换数据,当转换为UTF8字符串时,看起来非常像Base64字符串。奇怪的行为比比皆是。

不要使用
解压流.Read
,而是创建一个新的内存流并执行
解压流.CopyTo(newMemoryStream)
,然后执行newMemoryStream.Position=0;Encoding.UTF8.GetString(theNewMemoryStream.ToArray())@Andy试过了,没用。同样的例外。新代码将在一秒钟内添加到答案中。我尝试了你的代码,效果很好。不知道你的项目是怎么回事。如果您这样做:
var ret=“hello”.CompressString().DecompressString()我没有错误。当你使用上面的
CopyTo
代码时,它工作得非常完美。@Andy事实证明,这是因为在我的一个文件中,在所有服务器之间的数据交换中,我使用了
wait response.Content.ReadAsByteArrayAsync()
而不是
wait response.Content.ReadAsAsync()
,这破坏了一切。
public static string DecompressString(byte[] input)
{
    var result = new byte[input.Length];
    using (var newStream = new MemoryStream())
    {
        using (var source = new MemoryStream(input))
        {
            using (var decompressionStream = new GZipStream(source,
                CompressionMode.Decompress))
            {
                decompressionStream.CopyTo(newStream);
            }
        }
        newStream.Position = 0;
        return Encoding.UTF8.GetString(newStream.ToArray());
    }
}