-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlzw.html
62 lines (62 loc) · 7.7 KB
/
lzw.html
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" name="viewport" content="width=device-width, initial-scale=1">
<title>LZW 压缩解压</title>
<style>
body { font-size:12px;overflow:auto; border-style:none; background-color:#efefef; }
#source, #result { overflow:auto; width:100%; height:calc(50vh - 80px); }
</style>
<script language="javascript">var LZString144=function(){function o(o,r){if(!t[o]){t[o]={};for(var n=0;n<o.length;n++)t[o][o.charAt(n)]=n}return t[o][r]}var r=String.fromCharCode,n="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",e="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+-$",t={},i={compressToBase64:function(o){if(null==o)return"";var r=i._compress(o,6,function(o){return n.charAt(o)});switch(r.length%4){default:case 0:return r;case 1:return r+"===";case 2:return r+"==";case 3:return r+"="}},decompressFromBase64:function(r){return null==r?"":""==r?null:i._decompress(r.length,32,function(e){return o(n,r.charAt(e))})},compressToUTF16:function(o){return null==o?"":i._compress(o,15,function(o){return r(o+32)})+" "},decompressFromUTF16:function(o){return null==o?"":""==o?null:i._decompress(o.length,16384,function(r){return o.charCodeAt(r)-32})},compressToUint8Array:function(o){for(var r=i.compress(o),n=new Uint8Array(2*r.length),e=0,t=r.length;t>e;e++){var s=r.charCodeAt(e);n[2*e]=s>>>8,n[2*e+1]=s%256}return n},decompressFromUint8Array:function(o){if(null===o||void 0===o)return i.decompress(o);for(var n=new Array(o.length/2),e=0,t=n.length;t>e;e++)n[e]=256*o[2*e]+o[2*e+1];var s=[];return n.forEach(function(o){s.push(r(o))}),i.decompress(s.join(""))},compressToEncodedURIComponent:function(o){return null==o?"":i._compress(o,6,function(o){return e.charAt(o)})},decompressFromEncodedURIComponent:function(r){return null==r?"":""==r?null:(r=r.replace(/ /g,"+"),i._decompress(r.length,32,function(n){return o(e,r.charAt(n))}))},compress:function(o){return i._compress(o,16,function(o){return r(o)})},_compress:function(o,r,n){if(null==o)return"";var e,t,i,s={},p={},u="",c="",a="",l=2,f=3,h=2,d=[],m=0,v=0;for(i=0;i<o.length;i+=1)if(u=o.charAt(i),Object.prototype.hasOwnProperty.call(s,u)||(s[u]=f++,p[u]=!0),c=a+u,Object.prototype.hasOwnProperty.call(s,c))a=c;else{if(Object.prototype.hasOwnProperty.call(p,a)){if(a.charCodeAt(0)<256){for(e=0;h>e;e++)m<<=1,v==r-1?(v=0,d.push(n(m)),m=0):v++;for(t=a.charCodeAt(0),e=0;8>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1}else{for(t=1,e=0;h>e;e++)m=m<<1|t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t=0;for(t=a.charCodeAt(0),e=0;16>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1}l--,0==l&&(l=Math.pow(2,h),h++),delete p[a]}else for(t=s[a],e=0;h>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1;l--,0==l&&(l=Math.pow(2,h),h++),s[c]=f++,a=String(u)}if(""!==a){if(Object.prototype.hasOwnProperty.call(p,a)){if(a.charCodeAt(0)<256){for(e=0;h>e;e++)m<<=1,v==r-1?(v=0,d.push(n(m)),m=0):v++;for(t=a.charCodeAt(0),e=0;8>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1}else{for(t=1,e=0;h>e;e++)m=m<<1|t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t=0;for(t=a.charCodeAt(0),e=0;16>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1}l--,0==l&&(l=Math.pow(2,h),h++),delete p[a]}else for(t=s[a],e=0;h>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1;l--,0==l&&(l=Math.pow(2,h),h++)}for(t=2,e=0;h>e;e++)m=m<<1|1&t,v==r-1?(v=0,d.push(n(m)),m=0):v++,t>>=1;for(;;){if(m<<=1,v==r-1){d.push(n(m));break}v++}return d.join("")},decompress:function(o){return null==o?"":""==o?null:i._decompress(o.length,32768,function(r){return o.charCodeAt(r)})},_decompress:function(o,n,e){var t,i,s,p,u,c,a,l,f=[],h=4,d=4,m=3,v="",w=[],A={val:e(0),position:n,index:1};for(i=0;3>i;i+=1)f[i]=i;for(p=0,c=Math.pow(2,2),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;switch(t=p){case 0:for(p=0,c=Math.pow(2,8),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;l=r(p);break;case 1:for(p=0,c=Math.pow(2,16),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;l=r(p);break;case 2:return""}for(f[3]=l,s=l,w.push(l);;){if(A.index>o)return"";for(p=0,c=Math.pow(2,m),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;switch(l=p){case 0:for(p=0,c=Math.pow(2,8),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;f[d++]=r(p),l=d-1,h--;break;case 1:for(p=0,c=Math.pow(2,16),a=1;a!=c;)u=A.val&A.position,A.position>>=1,0==A.position&&(A.position=n,A.val=e(A.index++)),p|=(u>0?1:0)*a,a<<=1;f[d++]=r(p),l=d-1,h--;break;case 2:return w.join("")}if(0==h&&(h=Math.pow(2,m),m++),f[l])v=f[l];else{if(l!==d)return null;v=s+s.charAt(0)}w.push(v),f[d++]=s+v.charAt(0),h--,s=v,0==h&&(h=Math.pow(2,m),m++)}}};return i}();</script>
<script>
function $(s){ return document.getElementById(s); }
window.onload = function(){
$("source").focus();
$("source").onkeydown = function(e){
e = window.event || e;
if(e.keyCode == 13 && e.ctrlKey)doit();
}
$("result").onkeydown = function(e){
e = window.event || e;
if(e.keyCode == 13 && e.ctrlKey)doit2();
}
$("source").value = document.head.innerHTML;
}
var auto_lz='(function(a){var e,b=String.fromCharCode,c=Math.pow,d="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",f=function(a){if(!e){e={};for(var b=0;b<d.length;b++)e[d.charAt(b)]=b}return e[a]},g=function(a,d,e){var f,g,h,i,j,k,l=[],m=4,n=4,o=3,p=[],q=e(0),r=d,s=1;for(f=0;3>f;f+=1)l[f]=f;for(h=0,i=4,g=1;g!=i;)j=q&r,r>>=1,0==r&&(r=d,q=e(s++)),h|=(j>0?1:0)*g,g<<=1;switch(h){case 0:for(h=0,i=256,g=1;g!=i;)j=q&r,r>>=1,0==r&&(r=d,q=e(s++)),h|=(j>0?1:0)*g,g<<=1;k=b(h);break;case 1:for(h=0,i=65536,g=1;g!=i;)j=q&r,r>>=1,0==r&&(r=d,q=e(s++)),h|=(j>0?1:0)*g,g<<=1;k=b(h);break;case 2:return""}for(f=l[3]=k,p.push(k);;){if(s>a)return"";for(h=0,i=c(2,o),g=1;g!=i;)j=q&r,r>>=1,0==r&&(r=d,q=e(s++)),h|=(j>0?1:0)*g,g<<=1;switch(k=h){case 0:for(h=0,i=256,g=1;g!=i;)j=q&r,r>>=1,0==r&&(r=d,q=e(s++)),h|=(j>0?1:0)*g,g<<=1;l[n++]=b(h),k=n-1,m--;break;case 1:for(h=0,i=65536,g=1;g!=i;)j=q&r,r>>=1,0==r&&(r=d,q=e(s++)),h|=(j>0?1:0)*g,g<<=1;l[n++]=b(h),k=n-1,m--;break;case 2:return p.join("")}if(0==m&&(m=c(2,o),o++),l[k])k=l[k];else{if(k!==n)return null;k=f+f.charAt(0)}p.push(k),l[n++]=f+k.charAt(0),m--,f=k,0==m&&(m=c(2,o),o++)}};return a?g(a.length,32,function(b){return f(a.charAt(b))}):null})("';
function str2hex(s){
var i,hexstr="",tmpstr="",tmparr=[];
var te=new TextEncoder();
tmparr=te.encode(s);
for(i=0;i<tmparr.length;i++){
tmpstr=tmparr[i].toString(16);
hexstr+=(1==tmpstr.length?"0"+tmpstr:tmpstr);
}
return hexstr;
}
function doit(){
var start_time = Date.now();
$("result").value = $("chkSE").checked?(auto_lz+LZString144.compressToBase64($("source").value)+"\");"):(LZString144.compressToBase64($("source").value));
var use_time = Date.now() - start_time;
var y=str2hex($("source").value).length/2;
var z=str2hex($("result").value).length/2;
$("info").innerHTML = " 源:"+ y + ",值:" + z + ",压缩比" + (z/y).toFixed(3) + ",用时:"+ use_time + "ms";
}
function doit2(){
$("source").value = $("chkSE").checked?(eval($("result").value)):(LZString144.decompressFromBase64($("result").value));
}
</SCRIPT>
</head>
<body>
<h1 style='text-align:center'>LZW 压缩解压</h1>
<textarea id="source"></textarea>
<br/>
<br/>
<textarea id="result"></textarea>
<br/>
<br/>
<input type="button" value="Compress" onclick="doit();" />
<input type="button" value="DeCompress" onclick="doit2();" />
<input type="checkbox" id="chkSE" /><label for="chkSE">压缩为自解压格式</label>
<span id="info"></span>
</body>
</html>