将压缩数据写入NetworkStream
我正在编写一个简单的客户端-服务器应用程序,在查看 MSDN 文档时,我遇到了一些有趣的流类型...
http://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx http://msdn.microsoft.com/en-us /library/system.io.compression.gzipstream.aspx
显然,存在压缩流这样的东西!当然,考虑到我们正在处理网络问题,这非常有吸引力。然而,最不幸的是,TcpClient.GetStream() 只返回一个 NetworkStream——而不是任何形式的压缩流。
我想知道是否可以连接压缩流以重定向到 NetworkStream,这意味着我可以编写压缩流,并且该流会将其压缩输出写入我的 NetworkStream。我还需要知道如何执行相反的操作,获取压缩流以从 NetworkStream 中读取。
另一方面,您建议我做什么——GZip 或 Deflate 哪个提供最快的压缩?那么两者的压缩率有什么区别呢?
I am writing a simple client-server application, and looking the MSDN docs, I came across some interesting stream types...
http://msdn.microsoft.com/en-us/library/system.io.compression.deflatestream.aspx
http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx
Apparently, there is such a thing as a compressed stream! Naturally, this is very attractive, considering we are dealing with networking. However, most unfortunately, TcpClient.GetStream() only returns a NetworkStream -- Not any form of compressed stream.
I was wondering if it is possible to wire a compressed stream to redirect to the NetworkStream, meaning I could write the the compressed stream and that stream would write its compressed output to my NetworkStream. I'd also need to know how to do the reverse, get a compressed stream to read from a NetworkStream.
On the side, which do you recommend I do -- Which offers the fastest compression, GZip or Deflate? And what is the difference in compression between the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这些是包装流。
您可以围绕任何现有流创建一个
GzipStream
来读取压缩数据或将压缩数据写入内部流。These are wrapper streams.
You can create a
GzipStream
around any existing stream to read or write compressed data to the inner stream.查看networkComms.net,一个开源网络通信库,其中包括使用的选项发送数据时有许多不同类型的压缩。您所要做的就是更改 NetworkComms.DefaultCompressor 属性。您可以选择:
为了支持这些以及任何其他数量的不同压缩方法 networkComms.net 在基础上使用基本的网络流,并确保所有内容在到达之前都已被压缩 观点。
Checkout networkComms.net, an open source network communication library which includes the option of using many different types of compression when sending data. All you have to do is change the NetworkComms.DefaultCompressor property. You can choose from:
In order to to support these plus any further number of different compression methods networkComms.net uses a basic networkStream at the base and just makes sure everything has been compressed before it reaches that point.