0

DeflateStream.Read 不起作用,我正在尝试从压缩的内存流中读取,但 read 的字节数组参数仍然为空。

        var memoryStream = new MemoryStream();

        var writeStream = new DeflateStream(memoryStream, CompressionLevel.Optimal, true);

        var readStream = new DeflateStream(memoryStream, CompressionMode.Decompress, true);

        var serializedPayloadBytes = Serialize(new Payload { Message = "Payload" });

        var serializedHeaderBytes = Serialize(new PayloadHeader { Length = serializedPayloadBytes.Length });

        var headerSize = serializedHeaderBytes.Length;

        var package = new byte[serializedHeaderBytes.Length + serializedPayloadBytes.Length];

        Buffer.BlockCopy(serializedHeaderBytes, 0, package, 0, serializedHeaderBytes.Length);

        Buffer.BlockCopy(serializedPayloadBytes, 0, package, serializedHeaderBytes.Length, serializedHeaderBytes.Length);

        writeStream.Write(package, 0, package.Length);

        writeStream.Flush();

        writeStream.Close();

        var arr = new byte[serializedHeaderBytes.Length];

        readStream.Read(arr, 0, headerSize);

arr始终为空(所有字节均为零), memoryStream.ToArray() 有数据。

快速观看 arr 参数

4

1 回答 1

0

s 位置在写入后位于流的MemoryStream末尾。如果您想在写入后再次从同一流中读取,则必须将其设置回 0。

memoryStream.Position = 0;
于 2015-06-18T12:00:00.627 回答