Python中最简单的方法来压缩解压缩与C#(反之亦然)解压缩 [英] Easiest way to compress in Python and decompress with decompress C# (and vice versa)

查看:416
本文介绍了Python中最简单的方法来压缩解压缩与C#(反之亦然)解压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于Mono-C#客户端和一个Python服务器,它通过TCP / IP套接字通信的程序。这些消息使用大多是二进制格式,但每封邮件的最大部分通常被嵌入UTF-8字符串(英文)。每个消息通常是短(小于100字节),但有些可以更长(高达64K)。大量的数据交换,我想通过压缩时,它传送的数据,以减少消息的大小和带宽的使用。

I have a program with a Mono-based C# client and a Python server, which communicate over a TCP/IP socket. The messages use a mostly binary format but the largest part of each message is usually embedded UTF-8 strings (in English). Each message is typically short (under 100 bytes) but some can be longer (up to 64K). A lot of data is exchanged and I'd like to reduce message sizes and bandwidth usage by compressing the data when it's transmitted.

我最初的研究并没有止跌回升什么这是跨越2标准库显然兼容。 Python有一个的zlib库但我不能使用C#的的 DeflateStream 或GZipStream(因为它们需要一个外部DLL,我没有可用),它似乎并没有与SharpZipLib的ZipOutputStream工作(发放错误-3 - 不正确的头响应)。 (那些不使用单可能有更多的运气 - 见下文邓肯的回答。)

My initial research hasn't turned up anything that is obviously compatible across the 2 standard libraries. Python has a zlib library but I can't use C#'s DeflateStream or GZipStream (as they require an external DLL that I don't have available) and it doesn't seem to work with SharpZipLib's ZipOutputStream (giving out "error -3 - incorrect header" responses). (Those not using Mono might have more luck - see Duncan's answer below.)

我会有兴趣听一些简单方法,以便压缩在这个通信链路,轴承介意可能容易在一种语言实现的任何解决方案都需要有在其他的等效。我承认,是专门对UTF-8字符串,而不是二进制消息的解决方案虽然更倾向于将压缩整个字节流。

I would be interested in hearing about easy ways to enable compression over this communications link, bearing in mind that any solution that may be easy to implement in one language needs to have an equivalent in the other. I'd accept a solution that is specialised towards the UTF-8 strings rather than the binary messages although the preference would be to compress the whole byte stream.

理想我 。倒要保持外部依赖到最低限度,但我意识到可能是不实际的。

Ideally I'd like to keep external dependencies to a minimum, but I realise that may not be practical.

更新:尝试过用SharpZipLib和Python的遇到重复的错误解码方面,我真的可以做到与代码的具体建议,是众所周知的工作,而不是压缩库的一种语言或对方只是建议。

推荐答案

从SharpZipLib bzip2的和Python的图书馆为我工作。下面是我测试,方法如下:

The BZip2 from SharpZipLib and Python's library worked for me. Here's what I tested and how:

首先,C#程序(引用SharpZipLib):

First, the C# program (referencing SharpZipLib):

using System;
using ICSharpCode.SharpZipLib.BZip2;
using System.IO;

namespace Test
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            var fStream = new FileStream("/home/konrad/output.bin", FileMode.Create);
            using(var writer = new StreamWriter(new BZip2OutputStream(fStream)))
            {
                for(var i = 0; i < 10; i++)
                {
                    writer.WriteLine("Line no {0}.", i);
                }
            }
        }
    }

}

那么Python:

from bz2 import BZ2File
import sys

f = BZ2File("/home/konrad/output.bin")
for line in f.readlines():
    sys.stdout.write(line)

接下来,C#程序运行。而在这之后:

Next, C# program is ran. And after that:

$ python ctest.py
Line no 0.
Line no 1.
Line no 2.
Line no 3.
Line no 4.
Line no 5.
Line no 6.
Line no 7.
Line no 8.
Line no 9.

我认为它的工作原理其他方式轮为好。

I assume it works the other way round as well.

这篇关于Python中最简单的方法来压缩解压缩与C#(反之亦然)解压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆