字符串压缩/解压缩要通过网络发送的数据 [英] String Compression / Decompression of data to be sent over the network

查看:151
本文介绍了字符串压缩/解压缩要通过网络发送的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中寻找一个字符串压缩方法,我可以在数据字符串之前,我写它到网络套接字上运行?



为什么?









b

这是一个应用程序需要它运行在服务器上不断地发送消息到我们的另一个服务器。但是托管应用程序的服务器是使用数据计费,这是相当昂贵的。移动主机不是一个选项。



所以我需要一个算法/库,它可以efferntly(cpu明智)压缩我们发送的sting消息。我愿意牺牲一些cpu使用在网络上较少的数据。



我不知道什么类型的压缩比,一个会期望,我认为它取决于类型你发送的字符串消息和它的长度。



我看的是相当短的字符串,平均从100个字符到256个字符。有一个奇怪的,约900个字符长。



范例字串:



žŸKO9404ŸMR4ŸTT8DB3CŸTM08:50:26.253ŸDIVFSV09



,这里是一个更长的例子:



žŸAC15019ŸCI1602ŸSC7ŸZN001ŸPN01ŸFT7F55ŸCLSGKNetworkŸTP1ŸMR0ŸTT9733EŸIDGPAM01; GPAM02; GPAM03 ; GPAM05; GPAM04; GPAM06; GPAM07; GPAM08; GPAM09; GPAM10; GPAM02; GPAM03; GPAM04; GPAM05; GPAM06; GPAM07; GPAM08; GPAM09; GPAM10;GPAM01ŸTM09:01:08.858; 09:01:09.066; 09:01:09.043 ; 09:01:09.044; 09:01:09.066; 09:01:09.066; 09:01:09.065; 09:01:09.068; 09:01:09.067; 09:01:09.067; 09:01:50.395; 09 :01:50.386; 09:01:50.386; 09:01:50.386; 09:01:50.396; 09:01:50.384; 09:01:50.385; 09:01:50.386; 09:01:50.386; 09:01 :50.384GT584C; 584C; 584C; 584C; 584C; 584C; 584C; 584C; 584C; 584C; 589F; 589F; 589F; 589F; 589F; 589F; 589F; 589F; 589F; 589F p>

所以我在寻找一个解决方案,我可以在发送服务器上压缩,然后在接收服务器上解压缩。


解决方案



Gzip压缩示例:

 code> System.IO.MemoryStream ms = new System.IO.MemoryStream(); 
System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms,System.IO.Compression.CompressionMode.Compress);

//压缩
sw.Write ...
sw.Close();

Gzip示例解压缩:

  System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray); 
System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
System.IO.Compression.CompressionMode.Decompress);

//解压缩
int rByte = sr.Read ...
sr.Close();


I am looking for a string compression method in C# which I can run on the data String before I write it to the network socket? I am fairly new to any sort of compression hence the reason I am looking for some advice / guidance here.

WHY?

This is needed in an application where it runs on a Server which constantly sends messages out to another server of ours, However the server hosting the application is billed on data used and this is fairly expensive. Moving hosting is not an option.

So I need an Algorithm / Library which can efferently (cpu wise) compress the sting messages we are sending. I am willing to sacrifice some cpu usage for less data on the network.

Im not sure what type of compression ratios one would expect, I assume that it depends on the type of string message you are sending and the length of it.

I am looking at fairly short strings, ranging from 100 characters to 256 characters on average. There is the odd one that is ~900 characters long.

Example Strings:

žŸKO9404ŸMR4ŸTT8DB3CŸTM08:50:26.253ŸDIVFSV09

and here is a longer example:

žŸAC15019ŸCI1602ŸSC7ŸZN001ŸPN01ŸFT7F55ŸCLSGKNetworkŸTP1ŸMR0ŸTT9733EŸIDGPAM01;GPAM02;GPAM03;GPAM05;GPAM04;GPAM06;GPAM07;GPAM08;GPAM09;GPAM10;GPAM02;GPAM03;GPAM04;GPAM05;GPAM06;GPAM07;GPAM08;GPAM09;GPAM10;GPAM01ŸTM09:01:08.858;09:01:09.066;09:01:09.043;09:01:09.044;09:01:09.066;09:01:09.066;09:01:09.065;09:01:09.068;09:01:09.067;09:01:09.067;09:01:50.395;09:01:50.386;09:01:50.386;09:01:50.386;09:01:50.396;09:01:50.384;09:01:50.385;09:01:50.386;09:01:50.386;09:01:50.384ŸTG584C;584C;584C;584C;584C;584C;584C;584C;584C;584C;589F;589F;589F;589F;589F;589F;589F;589F;589F;589F

So I am looking for a solution which I can compress on the sending server, and then decompress on the receiving server.

What would be my best solution?

解决方案

Gzip Sample For Compress:

    System.IO.MemoryStream ms = new System.IO.MemoryStream();
    System.IO.Compression.GZipStream sw = new System.IO.Compression.GZipStream(ms, System.IO.Compression.CompressionMode.Compress);

    //Compress
    sw.Write ...
    sw.Close();

Gzip Sample For Decompress:

    System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray);
    System.IO.Compression.GZipStream sr = new System.IO.Compression.GZipStream(ms,
        System.IO.Compression.CompressionMode.Decompress);

    //Decompress
    int rByte = sr.Read ...
    sr.Close();

这篇关于字符串压缩/解压缩要通过网络发送的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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