如何使用 zlib.NET 膨胀文件?
我正在使用 zlib.NET 库来尝试膨胀由 zlib 压缩的文件(也许是在 Linux 机器上)。 这就是我正在做的事情:
zlib.ZInputStream zinput =
new zlib.ZInputStream(File.Open(path, FileMode.Open, FileAccess.Read));
while (stopByte != (data = zinput.ReadByte()))
{
// check data here
}
zinput.Close();
数据字节与压缩数据字节匹配,所以我一定做错了什么。
I'm using the zlib.NET library to try and inflate files that are compressed by zlib (on a Linux box, perhaps). Here's what I'm doing:
zlib.ZInputStream zinput =
new zlib.ZInputStream(File.Open(path, FileMode.Open, FileAccess.Read));
while (stopByte != (data = zinput.ReadByte()))
{
// check data here
}
zinput.Close();
The data bytes match the compressed data bytes, so I must be doing something wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
跳过 zlib 标头(前两个字节,
78 9C
),然后使用DeflateStream
内置于 .net 对我有用。Skipping the zlib header (first two bytes,
78 9C
) and then using theDeflateStream
built into .net worked for me.除了即使面对异常也无法使用“using”语句关闭流之外,这对我来说看起来还不错。 数据肯定是压缩的吗? 你能在linux机器上用zlib解压它吗?
查看源代码后,它非常可怕 - 对
int Read(buffer, offset, length)
的调用最终将调用其内部int Read()
方法length 倍。 鉴于这种不稳定的开始,我不确定我是否会特别信任该代码,但我预计它至少可以稍微工作! 您是否尝试过使用 SharpZipLib ?
Other than failing to use a "using" statement to close the stream even in the face of an exception, that looks okay to me. Is the data definitely compressed? Are you able to decompress it with zlib on the linux box?
Having looked at the source code, it's pretty ghastly - a call to
int Read(buffer, offset, length)
will end up calling its internalint Read()
methodlength
times for example. Given that sort of shaky start, I'm not sure I'd trust the code particularly heavily, but I'd have expected it to work at least slightly! Have you tried using SharpZipLib?看来我犯了一个错误,假设所有虚拟方法都被重写,但事实并非如此。 我使用的是 zlib.ZInputStream.ReadByte(),它只是继承的 Stream.ReadByte(),它不做任何膨胀。
我使用 zlib.ZInputStream.Read() 代替,它的工作原理如下。
It appears I made the mistake of assuming all virtual methods were overridden, which wasn't the case. I was using zlib.ZInputStream.ReadByte(), which is just the inherited Stream.ReadByte(), which doesn't do any inflate.
I used zlib.ZInputStream.Read() instead, and it worked like it should.
下面的代码可以帮助你们。 实例化对象并使用函数。
The below code could help you guys. Instantiate the object and make use of the functions.
我最近不幸地将之前使用 php 的 zlib 文档提供给各种浏览器和平台,包括 IE7。 一旦我发现文档是 zlib'd 而不是 gzip'd(正如当时所想的那样),我使用 SharpZipLib 在 .NET Framework v4 中按以下方式(利用 Stream.CopyTo):
我想我会把它放在这里,以防有人需要使用 SharpZipLib 的类的帮助。
I recently had the misfortune of serving out docs previously zlib'd using php to a variety of browsers and platforms including IE7. Once I figured out that the docs were zlib'd and not gzip'd (as was thought at the time) I used SharpZipLib in the following way in .NET Framework v4 (taking advantage of Stream.CopyTo):
Thought I would put it here in case anyone needs help with the classes to use from SharpZipLib.
仔细看看示例代码,它正在将数据从常规 Filestream 复制到 ZOutputStream。 减压必须通过该层进行。
Look at the sample code more closely, it is copying data from a regular Filestream to the ZOutputStream. The decompression must be happening through that layer.