字符串的gzip压缩与解压的整理

背景介绍
因为一直在处理restful API,给移动端提供的数据需要考虑流量问题,优先考虑就是压缩现有的字符串,然后再考虑业务逻辑方面的减少流量。鉴于找这些资料也花了不少时间,所以整理了这篇文章,留作纪念。
参考网址
PHP与C#的压缩与解压
http://www.oschina.net/question/2265205_181108
http://my.oschina.net/linland/blog/217769?fromerr=Kf9b6ElQ
http://www.yiibai.com/sql/function.gzencode.html

https://forums.xamarin.com/discussion/33607/using-system-io-compression-in-pcl

=====================
python的压缩与解压

http://outofmemory.cn/code-snippet/2030/Python-usage-gzip-pack-shujubao
http://www.cnblogs.com/mmix2009/p/3221371.html

http://blog.csdn.net/lxdcyh/article/details/4021476

javascript的压缩与解压
http://stackoverflow.com/questions/14620769/decompress-gzip-and-zlib-string-in-javascript
http://nodeca.github.io/pako/

代码具体实现
PHP (>=5.4)

[php] view plain copy

1
2
3
4
5
6
$srcString="1";  
$srcCompress=base64_encode(gzencode($srcString,9));
echo strlen($srcCompress).'---压缩后字符串长度<br/>';
$newString=base64_decode($srcCompress);
$newString=gzdecode($newString);
echo $newString.'----解压后原字符串<br/>';

c#
(建议还是考虑各个平台的,PCL版本下的见前面的网址,但是没调试通过)

[csharp] view plain copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
using System.IO.Compression;  
using System.Text;
public string Compress (string text)
{
byte[] buffer = Encoding.UTF8.GetBytes (text);
using (MemoryStream ms = new MemoryStream ()) {
GZipStream Compress = new GZipStream (ms, CompressionMode.Compress);

Compress.Write (buffer, 0, buffer.Length);

Compress.Close ();

return Convert.ToBase64String (ms.ToArray ());

}
}

public string Decompress (string text)
{
byte[] buffer = Convert.FromBase64String (text);
using (MemoryStream tempMs = new MemoryStream ()) {
using (MemoryStream ms = new MemoryStream (buffer)) {
GZipStream Decompress = new GZipStream (ms, CompressionMode.Decompress);

Decompress.CopyTo (tempMs);

Decompress.Close ();

return Encoding.UTF8.GetString (tempMs.ToArray ());
}
}
}

python
[python] view plain copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#coding=utf-8  
import base64
import StringIO,gzip
def zipData( content):
zbuf = StringIO.StringIO()
zfile = gzip.GzipFile(mode='wb', compresslevel=9, fileobj=zbuf)
zfile.write(content)
zfile.close()
return base64.b64encode(zbuf.getvalue())
def unzipData(content):
compresseddata=base64.b64decode(content)
compressedstream = StringIO.StringIO(compresseddata)
gzipper = gzip.GzipFile(fileobj=compressedstream)
data = gzipper.read()
return data
s=zipData("1")
print s
s1=unzipData(s)
print s1

javascript
[html] view plain copy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<html>  
<head>
<meta charset="UTF-8">
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://raw.githubusercontent.com/nodeca/pako/master/dist/pako.js"></script>
<script type="text/javascript">
function gzip(string) {
var charData = string.split('').map(function(x){return x.charCodeAt(0);});
var binData = new Uint8Array(charData);
var data= pako.gzip(binData);
var strData= String.fromCharCode.apply(null, new Uint16Array(data));
return btoa(strData);
}
function ungzip(string)
{
var strData = atob(string);
var charData = strData.split('').map(function(x){return x.charCodeAt(0);});
var binData = new Uint8Array(charData);
var data= pako.ungzip(binData);
var strData= String.fromCharCode.apply(null, new Uint16Array(data));
return strData;
}
test="1";
var s=gzip(test);
alert(s);
alert(ungzip(s));
</script>
</head>
</html>