问题
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?
回答1:
- LZO compression: For more information: http://www.oberhumer.com/opensource/lzo/
- GZip compression: For more information: http://msdn.microsoft.com/tr-tr/library/system.io.compression.gzipstream%28v=vs.110%29.aspx
- Quick LZ compression: For more information: http://www.quicklz.com/index.php
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();
回答2:
As your string length averages 900, Why do you need to compress it as the lowest MTU of common networks are about 1474 (ethernet and ipV4) ? Knowing that half of your bytes are UTF-8 1 byte-encoded.
I.e : If you send 100-250 bytes
across the network, it will be the same cost as if you send 1474 bytes
.
回答3:
You may have a look at the System.IO.Compression.GZipStream class, or its "brother" DeflateStream in the same namespace.
回答4:
Given that you control both ends of the solution. Maybe you should look at using a Binary protocol built on-top of something like Protobuf instead of using Text and trying to compress it.
If you can send less data in a block, but send more of those blocks as 1 operation across your network you can then cut down on your transmission costs.
ABC - 1st send (delay 3 sec) DEF - 2nd send
send ABCDEF in 1 send, but delay 6 sec.
来源:https://stackoverflow.com/questions/24927529/string-compression-decompression-of-data-to-be-sent-over-the-network