使用Zlib解压缩MemoryStream

时间:2020-06-29 07:01:22

标签: c# zlib dotnetzip

我正在编写处理大量文件的软件,这些文件包含zlib压缩数据,位于文件的不同部分,而不是整个文件本身。我知道如何掌握我需要的部分。但是,我无法使记录的zlib流类正常工作。我用谷歌搜索并尝试了几种不同的解决方案,除使用静态方法的解决方案外,其他解决方案均无法正常工作。以下代码可以正常工作,但不能像我希望的那样直接用于Zlib流类:

// reader is the BinaryReader for the original file and... 
// The "Data" section consists of a UInt32 and the compressed data
byte[] compressedStream = reader.ReadBytes((int)Size - 4); // name was kept for code compatibility
MemoryStream deflatedStream = new MemoryStream(ZlibStream.UncompressBuffer(compressedStream), true);

使用上面的代码实际上没有任何问题,因为它为我提供了所需的解压缩数据。但是,对于为什么我的原始代码直接实例化zlib流类的原因却感到困惑,因为这些原始代码直接使用zlib流类(因为它们使用相同的基本API):

MemoryStream compressedStream = new MemoryStream(reader.ReadBytes((int)Size - 4));
ZlibStream deflatedStream = new ZlibStream(compressedStream, CompressionMode.Decompress, true);

访问“ deflatedStream”的几乎任何属性都会导致错误。我认为这意味着它不起作用。可能值得注意的是,我还没有使用过DotNetZip库,而是使用了Zlib.Portable(第二受欢迎的库)。但是,API似乎相同。

0 个答案:

没有答案