GZip 压缩解压 --- Pako 的压缩解压

一、GZip压缩解压

package java调用JS;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Pako_GzipUtils {
	
    /**
     * @param str:正常的字符串
     * @return 压缩字符串 类型为: ‹‹ ³)°K,NIc i£_`Çe#‚  c¦%ÂXHòjyIÅÖ`
     * @throws IOException
     */
    public static String compress(String str) throws IOException {   
      if (str == null || str.length() == 0) {   
        return str;   
      }   
      ByteArrayOutputStream out = new ByteArrayOutputStream();   
      GZIPOutputStream gzip = new GZIPOutputStream(out);   
      gzip.write(str.getBytes());   
      gzip.close();   
      return out.toString("ISO-8859-1");   
    }   

   
    /**
     * @param str:类型为: ‹‹ ³)°K,NIc i£_`Çe#‚  c¦%ÂXHòjyIÅÖ`
     * @return 解压字符串  生成正常字符串。
     * @throws IOException
     */
    public static String uncompress(String str) throws IOException {   
      if (str == null || str.length() == 0) {   
        return str;   
      }   
      ByteArrayOutputStream out = new ByteArrayOutputStream();   
      ByteArrayInputStream in = new ByteArrayInputStream(str   
          .getBytes("ISO-8859-1")); 
      GZIPInputStream gunzip = new GZIPInputStream(in);   
      byte[] buffer = new byte[256];   
      int n;   
      while ((n = gunzip.read(buffer)) >= 0) {   
        out.write(buffer, 0, n);   
      }   
      // toString()使用平台默认编码,也可以显式的指定如toString("GBK")   
      return out.toString();   
    }   

    /**
     * @param jsUriStr :字符串类型为:%1F%C2%8B%08%00%00%00%00%00%00%03%C2%B3)%C2%B0K%2CNI%03c%20i%C2%A3_%60%C3%87e%03%11%23%C2%82%0Dc%C2%A6%25%C3%82XH%C3%B2jyI%C3%85%05%C3%96%60%1E%00%17%C2%8E%3Dvf%00%00%00
     * @return 生成正常字符串
     * @throws IOException 
     */
    public static String  unCompressURI(String jsUriStr) throws IOException {
		String decodeJSUri=URLDecoder.decode(jsUriStr,"UTF-8");
		String gzipCompress=uncompress(decodeJSUri);
    	return gzipCompress;
	}
    /**
     * @param strData :字符串类型为: 正常字符串
     * @return 生成字符串类型为:%1F%C2%8B%08%00%00%00%00%00%00%03%C2%B3)%C2%B0K%2CNI%03c%20i%C2%A3_%60%C3%87e%03%11%23%C2%82%0Dc%C2%A6%25%C3%82XH%C3%B2jyI%C3%85%05%C3%96%60%1E%00%17%C2%8E%3Dvf%00%00%00
     * @throws IOException 
     */
    public static String  compress2URI(String strData) throws IOException {
    	String encodeGzip=compress(strData);
    	String jsUriStr=URLEncoder.encode(encodeGzip,"UTF-8");
    	return jsUriStr;
    }
	
    
	
	
}

二、 Pako 1.0.3的 JS 中的解压:

<head>
<meta charset="UTF-8">
<meta name="viewport"
	content="width=device-width,initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<title>mail</title>
<style>
* {
	box-sizing: border-box;
}
.punch-line {
	word-warp: break-word;
	word-break: break-all;
}

.rececc-padding {
	padding-right: 2.0rem;
}

.as-nolines {
	max-width: 100%;
	overflow-x: hidden;
	text-overflow: ellipsis;
	-ms-text-overflow: ellipsis;
	-o-text-overflow: ellipsis;
	-webkit-text-overflow: ellipsis;
	white-space: nowrap;
}

.as-80nolines {
	max-width: 100px;
	overflow-x: hidden;
	text-overflow: clip; /* 剪切 不以...作为结尾 */
	-ms-text-overflow: clip;
	-o-text-overflow: clip;
	-webkit-text-overflow: clip;
	white-space: nowrap;
}

.div-inline {
	float: left; /* div并排 */
}
</style>
<script type="text/javascript" charset="utf-8" src="pako.min.js"></script>
</head>
<body style="word-wrap: break-word; font-family: Arial;">
<article>

/*此处写你想要的内容*/

</article>

<script>
  function punzipMsgSTR(){
	try{
 var punzipstr="%1F%C2%8B%08%00%00%00%00%00%00%03%C2%B3)%C2%B0K%2CNI%03c%20i%C2%A3_%60%C3%87e%03%11%23%C2%82%0Dc%C2%A6%25%C3%82XH%C3%B2jyI%C3%85%05%C3%96%60%1E%00%17%C2%8E%3Dvf%00%00%00";
        punzipstr =decodeURIComponent(punzipstr);
        console.log("punzipMsgSTR------------ok-"+punzipstr);
        var restored =pako.ungzip( punzipstr, { to: 'string' } ); // 解
        console.log("punzipMsgSTR------------ok-"+restored);
	}catch(err){
		console.log("punzipMsgSTR------------error-"+err);
	}
	return restored;
  }

 
  function unzipMessageEACH(string){ 
	  var iframe = document.createElement('iframe');
	  iframe.style.display = 'none';
	  var iframedocument;
	  var iframeWindow;
	  document.body.appendChild(iframe);
	  iframedocument =  iframe.contentDocument;// contentWindow.document;
	  iframeWindow = iframe.contentWindow;
	  iframedocument.open();
	  iframedocument.write(string);
	  iframedocument.close();
	  
	  var classnames="jscompressed";
	  var tagnames="p";
	  var tags=iframedocument.getElementsByTagName(tagnames);// 获取HTML的
														// *表示所有类型标签
	  for(var i in tags){// 对标签进行遍历
		  if(tags[i].nodeType==1){// 判断节点类型
			  if(tags[i].getAttribute("class") == classnames) { // 判断和需要CLASS名字相同的,并组成一个数组
					var htmlcontent =tags[i].innerHTML; 
					
					if ((htmlcontent == null || htmlcontent == undefined || htmlcontent == ''|| htmlcontent.substr(0, 1)!=="%")) {
						continue;
					}
					try {
						console.log("unzipMessageEACH------htmlcontent = "+htmlcontent);
						htmlcontent =decodeURIComponent(htmlcontent);
						  var restored =pako.ungzip( htmlcontent, { to: 'string' } ); // 解
						  tags[i].innerHTML=restored;
					} catch (e) {
						console.log("unzipMessageEACH------------e-"+e);
					}
			  } 
		  } 
	  } 
	  console.log("newdom.value iframeWindow = "+iframeWindow.document.body.innerHTML);
	  var result= iframeWindow.document.body.innerHTML+"";
	  document.body.removeChild(iframe);
	return result;
	 /* return iframedocument.innerHTML; */
	/* return iframedocument.innerText; */
	} 
	   
</script>
</body>
</html>

三实例字符串。

 var punzipstr="%1F%C2%8B%08%00%00%00%00%00%00%03%C2%B3)%C2%B0K%2CNI%03c%20i%C2%A3_%60%C3%87e%03%11%23%C2%82%0Dc%C2%A6%25%C3%82XH%C3%B2jyI%C3%85%05%C3%96%60%1E%00%17%C2%8E%3Dvf%00%00%00";


这个字符串是个 UriEncode字符串,需要解析出压缩的代码,才能解压出来。(因为压缩后的字符串,一般不能传递,需要BASE64 生成字符串,或者生成EncodeUri等方式,再进行传递)





评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值