https://github.com/nodeca/pako
https://gitee.com/renew_old_romance/pako/tree/master
https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/pako/index.d.ts
参考 Javascript 简单实现Gzip 压缩字符串 基于pako.js
因为字符串需要与后端通讯,所以使用了bota/atob进行base64编码。
关于字符串与二进制处理,可以参考 jsmpeg系列一 基础知识 字符处理 ArrayBuffer TypedArray ,其中提到了ArrayBuffer与字符串的互相转换。
ArrayBuffer转为字符串,或者字符串转为ArrayBuffer,有一个前提,即字符串的编码方法是确定的。假定字符串采用UTF-16编码(JavaScript的内部编码方式),可以自己编写转换函数。
但是,ab2str这种写法,在实际使用中,如果buf过大,会有 Maximum call stack size exceeded 堆栈溢出。
可以参考 javascript - js数组转字符串 - 在字符串和ArrayBuffers之间转换 ,改为for的写法:
public class Trans {public void printNum(int num){
System.out.print(Thread.currentThread())//获取当前运行这个方法的类
for(int i=0i<25i++){
System.out.print(i+" ")
}
System.out.println()
}
}
<HTML><HEAD>
<TITLE>Decision Helper</TITLE>
<META http-equiv=Content-Type content="text/htmlcharset=gb2312">
<SCRIPT LANGUAGE="JavaScript">
<!--
function Compress(strNormalString)
{
alert("压缩前长度:" + strNormalString.length)
var strCompressedString = ""
var ht = new HashTable
for(i = 0i <128i++) {
var e = new HashTableElement
e.key = i
e.code = i
ht.Insert(e)
}
var used = 128
var intLeftOver = 0
var intOutputCode = 0
var pcode = 0
var ccode = 0
var k = 0
for(var i=0i<strNormalString.lengthi++) {
ccode = strNormalString.charCodeAt(i)
k = (pcode <<8) | ccode
if((intSearch = ht.Search(k)) != null) {
pcode = intSearch
} else {
intLeftOver += 12
intOutputCode <<= 12
intOutputCode |= pcode
pcode = ccode
if(intLeftOver >= 16) {
strCompressedString += String.fromCharCode( intOutputCode >>( intLeftOver - 16 ) )
intOutputCode &= (Math.pow(2,(intLeftOver - 16)) - 1)
intLeftOver -= 16
}
if(used <4096) {
used ++
var e = new HashTableElement
e.key = k
e.code = used - 1
ht.Insert(e)
}
}
}
if(pcode != 0) {
intLeftOver += 12
intOutputCode <<= 12
intOutputCode |= pcode
}
if(intLeftOver >= 16) {
strCompressedString += String.fromCharCode( intOutputCode >>( intLeftOver - 16 ) )
intOutputCode &= (Math.pow(2,(intLeftOver - 16)) - 1)
intLeftOver -= 16
}
if( intLeftOver >0) {
intOutputCode <<= (16 - intLeftOver)
strCompressedString += String.fromCharCode( intOutputCode )
}
alert("压缩后长度:" + strCompressedString.length)
return strCompressedString
}
function Decompress(strCompressedString)
{
var strNormalString = ""
var ht = new Array
for(i = 0i <128i++)
{
ht[i] = String.fromCharCode(i)
}
var used = 128
var intLeftOver = 0
var intOutputCode = 0
var ccode = 0
var pcode = 0
var key = 0
for(var i=0i<strCompressedString.lengthi++) {
intLeftOver += 16
intOutputCode <<= 16
intOutputCode |= strCompressedString.charCodeAt(i)