简体   繁体   English

.NET 中的好的 zlib 实现?

[英]Good zlib implementation in .NET?

I'm building an network application that needs to be able to switch from normal network traffic to a zlib compressed stream, mid stream.我正在构建一个网络应用程序,它需要能够从正常网络流量切换到zlib压缩流,中间流。 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.我对这个问题的想法涉及布尔开关,当打开时将导致网络代码通过一个类传递所有数据,我可以将IEnumerable<byte>输入到该类中,然后拉出解压缩的流,将其传递给已经存在的协议解析代码。

Things I've looked at:我看过的东西:

  • ZLib.NET - It seems a little... Ecclectic, and not quite what I want. ZLib.NET - 似乎有点……不拘一格,但不是我想要的。 Would still make a decent start to build off though.尽管如此,仍然会有一个不错的开始。 ( Jon Skeet's comments here hardly inspire me either.) 乔恩斯基特在这里的评论也几乎没有激励我。)
  • SharpZipLib - This doesn't seem to support zlib at all? SharpZipLib - 这似乎根本不支持 zlib? Can anyone confirm or deny this?任何人都可以确认或否认这一点吗?

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 .我正在尝试实施MCCP 2 This involves a signal being sent in the network stream, and everything after this signal is a zlib compressed data stream.这涉及在网络流中发送的信号,该信号之后的所有内容都是 zlib 压缩数据流。 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).无论如何,要清楚的是,我正在接收端(客户端,而不是服务器),并且我已经从网络流中读取了一堆数据,并且切换将在此中间(总共至少可能),因此任何解决方案都需要能够在它接管 NetworkStream 之前将一些额外的数据输入其中(或者我手动输入其余数据)。

SharpZipLib does support ZLib. SharpZipLib 确实支持 ZLib。 Look in the FAQ .查看常见问题解答

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.编辑:好的......听起来你需要一个支持缓冲的流,但比BufferedStream提供更多的控制。 You'd need to "rewind" the stream if you saw the decompression toggle, and then create a GZipStream on top of it.如果您看到解压切换,则需要“倒带”流,然后在其上创建 GZipStream。 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.您的缓冲区至少需要与对 Read() 的最大调用一样大,以便您始终可以有足够的缓冲区来回退。

Included in DotNetZip there is a ZlibStream , for compressing or decompressing zlib streams of data. DotNetZip 中包含一个ZlibStream ,用于压缩或解压缩 zlib 数据流。 You didn't ask, but there is also a GZipStream and a DeflateStream.你没有问,但还有一个 GZipStream 和一个 DeflateStream。 As well as a ZlibCodec class, if that is your thing.以及 ZlibCodec 类,如果这是你的东西。 (just inflates or deflates buffers, as opposed to streams). (只是膨胀或收缩缓冲区,而不是流)。

DotNetZip is a fully-managed library with a liberal license. DotNetZip 是一个完全托管的库,具有自由许可。 You don't need to use any of the .zip capability to get at the Zlib stuff.您不需要使用任何 .zip 功能来获取 Zlib 内容。 And the zlib stuff is packaged as a separate (smaller) DLL just for this purpose.为了这个目的,zlib 的东西被打包成一个单独的(较小的)DLL。

I can recommend you Gerry Shaw's zlib wrapper for .NET:我可以向您推荐 Gerry Shaw 的 .NET 的 zlib 包装器:

http://www.organicbit.com/zip/ http://www.organicbit.com/zip/

As far as I know the ZLib (gzip) library doesn't support listing the files in the header.据我所知,ZLib (gzip) 库不支持在标题中列出文件。 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 :)这是我前一阵子使用sharp zip库的时候,所以我愿意删除它:)

Old question, but System.IO.Compression.DeflateStream is actually the right answer if you need proper zlib support:老问题,但如果您需要适当的 zlib 支持, System.IO.Compression.DeflateStream实际上是正确的答案:

Starting with the .NET Framework 4.5, the DeflateStream class uses the zlib library .从 .NET Framework 4.5 开始,DeflateStream 类使用 zlib 库 As a result, it provides a better compression algorithm and, in most cases, a smaller compressed file than it provides in earlier versions of the .NET Framework.因此,它提供了更好的压缩算法,并且在大多数情况下,比在早期版本的 .NET Framework 中提供的压缩文件更小。

Doesn't get better than that.没有比这更好的了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM