数据压缩
只有当订阅后,返回市场数据时,远程服务会将数据压缩返回给客户端。远程服务返回数据有两个格式,Binary 格式 和 Text 格式,当返回了 Binary 格式 表示数据被远程服务压缩过,客户端此时需要解压。
压缩说明
zlib是提供资料压缩之用的库,由Jean-loup Gailly与Mark Adler所开发,初版0.9版在1995年5月1日发表。zlib使用抽象化的DEFLATE算法,最初是为libpng库所写的,后来普遍为许多软件所使用。此库为自由软件。官方链接 http://zlib.net/
解压示例
示例代码
import zlib
message = b'abcd1234'
compressed = zlib.compress(message)
decompressed = zlib.decompress(compressed).decode('UTF-8')
print(decompressed) # abcd1234
import java.util.zip.*;
try {
// Encode a String into bytes
String inputString = "blahblahblah";
byte[] input = inputString.getBytes("UTF-8");
// Compress the bytes
byte[] output = new byte[100];
Deflater compresser = new Deflater(true);
compresser.setInput(input);
compresser.finish();
int compressedDataLength = compresser.deflate(output);
compresser.end();
// Decompress the bytes
Inflater decompresser = new Inflater(true);
decompresser.setInput(output, 0, compressedDataLength);
byte[] result = new byte[100];
int resultLength = decompresser.inflate(result);
decompresser.end();
// Decode the bytes into a String
String outputString = new String(result, 0, resultLength, "UTF-8");
} catch(java.io.UnsupportedEncodingException ex) {
// handle
} catch (java.util.zip.DataFormatException ex) {
// handle
}