import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GZIPUtils {
public static final String GZIP_ENCODE_UTF_8 = "UTF-8";
public static byte[] compress(String str, String encoding) {
if (str == null || str.length() == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip;
try {
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(encoding));
gzip.close();
} catch ( Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}
public static byte[] compress(String str) throws IOException {
///str = URLEncoder.encode(str, GZIP_ENCODE_UTF_8);
return compress(str, GZIP_ENCODE_UTF_8);
}
public static byte[] uncompress(byte[] bytes) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
}
return out.toByteArray();
}
public static String uncompressToString(byte[] bytes, String encoding) {
if (bytes == null || bytes.length == 0) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
try {
GZIPInputStream ungzip = new GZIPInputStream(in);
byte[] buffer = new byte[256];
int n;
while ((n = ungzip.read(buffer)) >= 0) {
out.write(buffer, 0, n);
}
return out.toString(encoding);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String uncompressToString(byte[] bytes) {
return uncompressToString(bytes, GZIP_ENCODE_UTF_8);
}
public static String toString(byte[] bytes) {
return org.apache.commons.codec.binary.Base64.encodeBase64String(bytes);
}
public static byte[] decodeToString(String bytes) {
return org.apache.commons.codec.binary.Base64.decodeBase64(bytes);
}
public static void main(String[] args) throws IOException {
/*String s = "{\"navigate\":\"POSLOGIN\",\"userInfo\":{\"sysDate\":1575540038000,\"userFlag\":\"Y\",\"orderType\":\"\",\"lastURL\":\"\",\"shopName\":\"安麗體驗廣場\",\"saleDate\":1575475200000,\"locale\":\"zh_TW\",\"orderChannel\":\"POS\",\"lastDateForMonth\":1577807999000,\"regionCode\":\"130\",\"staffName\":\"邱信展2\",\"logonPosFlag\":\"Y\",\"startTime\":1575540038000,\"integerFirstDateForMonth\":20191201,\"posSequence\":0,\"saleDateInt\":20191205,\"shopCode\":\"01\",\"firstDateForMonth\":1575129600000,\"cardHiddenFlag\":\"N\",\"preference\":{\"dateFormatInput\":\"yyyyMMdd\",\"currencyFormatD3\":\"#,##0.000\",\"currencyFormatD2\":\"#,##0.00\",\"dateFormat\":\"yyyy-MM-dd\",\"currencyFormatD0\":\"#,##0\",\"timeFormat\":\"HH:mm:ss\",\"dateAndTimeFormat\":\"MM-dd HH:mm\"},\"idHiddenFlag\":\"N\",\"sysDateInt\":20191205,\"privilegeInited\":false,\"staffNumber\":\"QIU02\",\"orderDivFlowList\":[],\"posCode\":\"T003\",\"inValidPrivHotKeyStrSet\":[\"A01\"],\"intSaleDateTW\":1191205,\"currencyCode\":\"NTD\",\"integerLastDateForMonth\":20191231}}";
System.out.println("字符串长度:"+s.length());
System.out.println("字符长度byte:"+ s.getBytes("UTF-8").length);
String cstr = GZIPUtils.toString( compress(s));
cstr = new String( compress(s));
System.out.println("byse64压缩后String::"+ cstr );
System.out.println("byse64压缩后ByteLength::"+ cstr.getBytes("UTF-8").length);
System.out.println("压缩后byte::"+compress(s).length);
System.out.println("解压后byte:"+uncompress(compress(s)).length);
System.out.println("解压字符串后::"+uncompressToString(compress(s)));
System.out.println("解压字符串后::"+toString(compress(s)));
*/
}
}
=====================================================================
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>pako.js实现gzip的压缩和解压</title>
</head>
<body>
<script type="text/javascript" src="pako.js"></script>
<script >
var b64Data = 'H4sIAAAAAAAAAG1ST4/SQBT/Kma4smZaFll62yxBGimLtqsxZmNGZgqTtDN1ZsCwhIs375q9ajwZE5NNPOnBLyMHvoVv2imQRQ4kze/fm/d7KyTIgk+JYShA48t4ePk4HKEmmmumQpFKFKyQXupeSfDanXb7FOPWGca44vQzMgXlS5BIRZlKloV1gs+MaHP1bFh96JksRiS30ObHh+2v2+33j9tvt5vfXzeff1qcZGyfcdpp+xiXGZmcAASym9nr5EWdcjEjQrCsGtllWXlfqkgKMyttOme40+12SxvFplyKC0mtldfCNtKQNHUzbd/f/f3zZXP3ybdmcirFWOqDpwFXmYTnxzvgwrAprIGr+xP42Ot68NdEhdQxeztnYgJ6vH9sKMyO1q525CbEHoSmx5423PO7j9xyJkTRAaeUCTerba5QLGWqClshWulzYkJRzCEPLeEXRZQCdTJXlrisCL0WoI1mo4Efgvsx7B/AgO6dnelJFJ38zxbXOoAM7HAnGgyCPA+0dmbngiaHcGn3oCShNWz6+KnuMO/tsVB8wTNoJRTcMIqClGSa1YXP8zdMgf5peIX9+p56fNHP5Lsh1+D06rqszFWRQNXI9vycZJyOwXsgzRO2jI2KmWWjc2jruryE2BULhxp49Tj1NpzfKOmh3dkMj+62ekbLW6//AT9C+HuaAwAA';
var ticketData = '{"ed":"20170121 09:44:01","fs":[{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"003","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"005","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"004","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"007","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"008","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"026","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"033","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"034","oids":["0"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"035","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"037","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"038","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"041","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"042","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"047","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"046","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"048","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"051","oids":["1"]},{"usg":[1,1,1,1,1,1,1],"act":0,"fid":"053","oids":["4"]}],"qty":1,"sd":"20161021 09:44:01","cd":"72016102116762039687"}';
var han = "就是qwqw上来就发上来快点减肥蓝色的就疯了似的";
// Output to console
var ehan = zip(han);
var s = unzip(b64Data) ;
console.log("unzipped:");
console.log(s);
console.log("中午");
document.write(han+"<br/>");
document.write(s);
var data = zip(ticketData);
console.log("zipped:");
console.log(data);
function unzip(b64Data){
var strData = atob(b64Data);
// Convert binary string to character-number array
var charData = strData.split('').map(function(x){return x.charCodeAt(0);});
// Turn number array into byte-array
var binData = new Uint8Array(charData);
// // unzip
var data = pako.inflate(binData);
// Convert gunzipped byteArray back to ascii string:
//strData = String.fromCharCode.apply(null, new Uint16Array(data));
// Convert gunzipped byteArray back to utf8 string:
strData = byteToUtf8String(data);
return strData;
}
function zip(str){
var binaryString = pako.gzip(str, { to: 'string' });
return btoa(binaryString);
}
function byteToUtf8String(arr) {
if(typeof arr === 'string') {
return arr;
}
var str = '',
_arr = arr;
for(var i = 0; i < _arr.length; i++) {
var one = _arr[i].toString(2),
v = one.match(/^1+?(?=0)/);
if(v && one.length == 8) {
var bytesLength = v[0].length;
var store = _arr[i].toString(2).slice(7 - bytesLength);
for(var st = 1; st < bytesLength; st++) {
store += _arr[st + i].toString(2).slice(2);
}
str += String.fromCharCode(parseInt(store, 2));
i += bytesLength - 1;
} else {
str += String.fromCharCode(_arr[i]);
}
}
return str;
}
</script>
=========
</body>
</html>