问题描述
根据 .Net 4.5 中的 MSDN System.IO.Compression 基于 zlib.
我现在正在尝试将当前基于互操作的读取从非 .NET 服务器的 zlib 压缩流更改为基于 BCL 的实现.
我的实现如下所示:
According to MSDN in .Net 4.5 System.IO.Compression is based on zlib.
I am trying now to change my current interop based reading from a zlib deflated stream from a non .NET server into a BCL based implementation.
My implementation looks like this:
var enc = new UTF8Encoding();
var readBytes = BufferSizeRaw;
var outputBuffer = new byte[BufferSizeRaw];
var networkBuffer = _networkQueue.Take();
var ms = new MemoryStream(networkBuffer.InputBuffer, 0, networkBuffer.UsedLength);
using (Stream stream = new DeflateStream(ms, CompressionMode.Decompress))
while (readBytes==BufferSizeRaw)
{
readBytes = stream.Read(outputBuffer, 0, outputBuffer.Length);
stringBuffer+= enc.GetString(outputBuffer, 0, readBytes);
}
我在第一次调用 DeflateStream 上的解压缩/读取时收到以下异常:
I receive the following exception on the first call of the decompression/read on the DeflateStream :
块长度与其补码不匹配
基于互操作的调用使用 var result=inflate(ref zStyream, ZLibFlush.NoFlush;
有没有人尝试过相同的方法或看到代码错误的原因,或者我是否有错误的理解?我也尝试过截断前两个字节而没有任何运气.
前几个字节是 20、202、177、13.
The interop based call uses var result=inflate(ref zStyream, ZLibFlush.NoFlush;
Has anyone tried the same or sees a reason for an error in the code or is there a wrong understanding on my end?I have also tried it with truncating the first two bytes without any luck.
The first few bytes are 20, 202, 177,13.
推荐答案
您尝试压缩的数据的前几个字节是什么?
What are the first several bytes of the data you are trying to compress?
您可能有要解码的 zlib、gzip 或原始 deflate 数据.
You might have zlib, gzip, or raw deflate data that you're trying to decode.
顺便说一句,我强烈建议您使用 DotNetZip 的 接口到 zlib 而不是 NET 4.5(或 NET 任何版本).NET 4.5 在该界面中存在错误,Microsoft 已声明这些错误 不会修复(!).
By the way, I highly recommend that you use DotNetZip's interface to zlib instead of NET 4.5's (or NET any version). NET 4.5 has bugs in that interface that Microsoft has declared that they won't fix (!).
这篇关于.Net zlib 使用 .Net 4.5 膨胀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!