C# .NET 中的好的 zlib 实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/513234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Good zlib implementation in .NET?
提问by Matthew Scharley
I'm building an network application that needs to be able to switch from normal network traffic to a zlibcompressed stream, mid stream. My thoughts on the matter involve boolean switch that when on will cause the network code to pass all the data through a class that I can feed IEnumerable<byte>
into, and then pull out the decompressed stream, passing that on to the already existing protocol parsing code.
我正在构建一个网络应用程序,它需要能够从正常网络流量切换到zlib压缩流,中间流。我对这个问题的想法涉及布尔开关,当打开时将导致网络代码通过我可以IEnumerable<byte>
输入的类传递所有数据,然后拉出解压缩的流,将其传递给现有的协议解析代码。
Things I've looked at:
我看过的东西:
- ZLib.NET- It seems a little... Ecclectic, and not quite what I want. Would still make a decent start to build off though. (Jon Skeet's comments herehardly inspire me either.)
- SharpZipLib- This doesn't seem to support zlib at all? Can anyone confirm or deny this?
- ZLib.NET- 似乎有点……不拘一格,但不是我想要的。尽管如此,仍然会有一个不错的开始。(乔恩斯基特在这里的评论也几乎没有激励我。)
- SharpZipLib- 这似乎根本不支持 zlib?任何人都可以确认或否认这一点吗?
I would very much prefer and all managed solution, but let's have at it... are there any other implementations of this library out there in .NET, that might be better suited to what I want to do, or should I take ZLib.NET and build off that as a start?
我非常喜欢和所有托管解决方案,但让我们来看看....NET 中是否有该库的任何其他实现,这可能更适合我想做的事情,或者我应该采用 ZLib。 NET 并以此作为开始?
PS:
PS:
Jon's asked for more detail, so here it is.
乔恩要求提供更多细节,所以在这里。
I'm trying to implement MCCP 2. This involves a signal being sent in the network stream, and everything after this signal is a zlib compressed data stream. There's links to exactly what they mean by that in the above link. Anyway, to be clear, I'm on the recieving end of this (client, not server), and I have a bunch of data read out of the network stream already, and the toggle will be in the middle of this (in all likelyhood atleast), so any solution needs to be able to have some extra data fed into it, before it takes over the NetworkStream (or I manually feed in the rest of the data).
我正在尝试实施MCCP 2。这涉及在网络流中发送的信号,该信号之后的所有内容都是 zlib 压缩数据流。在上面的链接中有指向它们的确切含义的链接。无论如何,要清楚的是,我正在接收端(客户端,而不是服务器),并且我已经从网络流中读取了一堆数据,并且切换将在此中间(总共至少可能),因此任何解决方案都需要能够在它接管 NetworkStream 之前将一些额外的数据输入其中(或者我手动输入其余数据)。
回答by Dirk Vollmar
I can recommend you Gerry Shaw's zlib wrapper for .NET:
我可以向您推荐 Gerry Shaw 的 .NET 的 zlib 包装器:
回答by Jon Skeet
SharpZipLib does support ZLib. Look in the FAQ.
SharpZipLib 确实支持 ZLib。查看常见问题解答。
Additionally, have you checked whether the System.IO.Compression namespace supports what you need?
此外,您是否检查过 System.IO.Compression 命名空间是否支持您所需要的?
I wouldn't use an IEnumerable<byte>
though - streams are designed to be chained together.
我不会使用IEnumerable<byte>
虽然 - 流被设计为链接在一起。
EDIT: Okay... it sounds like you need a stream which supports buffering, but with more control than BufferedStream
provides. You'd need to "rewind" the stream if you saw the decompression toggle, and then create a GZipStream on top of it. Your buffer would need to be at least as big as your biggest call to Read() so that you could always have enough buffer to rewind.
编辑:好的......听起来你需要一个支持缓冲的流,但比BufferedStream
提供的控制更多。如果您看到解压切换,则需要“倒带”流,然后在其上创建 GZipStream。您的缓冲区至少需要与对 Read() 的最大调用一样大,以便您始终可以有足够的缓冲区来回退。
回答by Chris S
As far as I know the ZLib (gzip) library doesn't support listing the files in the header. Assuming that matters to you, but it seems a big shortcoming. This was when I used the sharp zip library a while ago, so I'm willing to delete this :)
据我所知,ZLib (gzip) 库不支持在标题中列出文件。假设这对您很重要,但这似乎是一个很大的缺点。这是我前一阵子使用sharp zip库的时候,所以我愿意删除它:)
回答by Cheeso
Included in DotNetZipthere is a ZlibStream, for compressing or decompressing zlib streams of data. You didn't ask, but there is also a GZipStream and a DeflateStream. As well as a ZlibCodec class, if that is your thing. (just inflates or deflates buffers, as opposed to streams).
DotNetZip 中包含一个ZlibStream,用于压缩或解压缩 zlib 数据流。你没有问,但还有一个 GZipStream 和一个 DeflateStream。以及 ZlibCodec 类,如果这是你的东西。(只是膨胀或收缩缓冲区,而不是流)。
DotNetZip is a fully-managed library with a liberal license. You don't need to use any of the .zip capability to get at the Zlib stuff. And the zlib stuff is packaged as a separate (smaller) DLL just for this purpose.
DotNetZip 是一个完全托管的库,具有自由许可。您不需要使用任何 .zip 功能来获取 Zlib 内容。为了这个目的,zlib 的东西被打包成一个单独的(较小的)DLL。