js lzw中文压缩算法
1 function utf8(){ 2 3 } 4 5 utf8.prototype.encode=function(text) 6 { 7 var result = ""; 8 for (var n = 0; n < text.length; n++) 9 { 10 var c = text.charCodeAt(n); 11 if (c < 128) 12 { 13 result += String.fromCharCode(c); 14 } 15 else if (c > 127 && c < 2048) 16 { 17 result += String.fromCharCode((c >> 6) | 192); 18 result += String.fromCharCode((c & 63) | 128); 19 } 20 else 21 { 22 result += String.fromCharCode((c >> 12) | 224); 23 result += String.fromCharCode(((c >> 6) & 63) | 128); 24 result += String.fromCharCode((c & 63) | 128); 25 } 26 } 27 return result; 28 } 29 30 utf8.prototype.decode=function(text) 31 { 32 var result = ""; 33 var i = 0; 34 var c1 = 0; 35 var c2= 0; 36 var c3 = 0; 37 while (i < text.length) 38 { 39 c1 = text.charCodeAt(i); 40 if (c1 < 128) 41 { 42 result += String.fromCharCode(c1); 43 i++; 44 } 45 else if (c1 > 191 && c1 < 224) 46 { 47 c2 = text.charCodeAt(i + 1); 48 result += String.fromCharCode(((c1 & 31) << 6) | (c2 & 63)); 49 i += 2; 50 } 51 else 52 { 53 c2 = text.charCodeAt(i + 1); 54 c3 = text.charCodeAt(i + 2); 55 result += String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)); 56 i += 3; 57 } 58 } 59 return result; 60 } 61 62 /** 63 * Created with JetBrains WebStorm. 64 * User: Administrator 65 * Date: 12-8-3 66 * Time: 下午4:01 67 * To change this template use File | Settings | File Templates. 68 */ 69 /** 70 * lzw中文压缩类 71 * @type {*} 72 */ 73 function lzwcn(){ 74 75 } 76 77 lzwcn.prototype.compress=function(str){ 78 var rStr=''; 79 rStr=as3long.utf8.encode(str); 80 var i=0; 81 var size=0; 82 var xstr=''; 83 var chars = 256; 84 var dict = new Array(); 85 for (i = 0; i < chars; i++) 86 { 87 dict[String(i)] = i; 88 } 89 var splitted=new Array(); 90 splitted = rStr.split(""); 91 var buffer=new Array(); 92 size=splitted.length; 93 var current=''; 94 var result = new String(""); 95 for(i = 0;i<=size;i++) 96 { 97 current = new String(splitted[i]); 98 xstr = (buffer.length == 0) ? String(current.charCodeAt(0)) : (buffer.join("-") + "-" + String(current.charCodeAt(0))); 99 if (dict[xstr] !== undefined) 100 { 101 buffer.push(current.charCodeAt(0)); 102 } 103 else 104 { 105 result += String.fromCharCode(dict[buffer.join("-")]); 106 dict[xstr] = chars; 107 chars++; 108 buffer = new Array(); 109 buffer.push(current.charCodeAt(0)); 110 } 111 } 112 return result; 113 } 114 115 lzwcn.prototype.decompress=function(str){ 116 var i; 117 var chars = 256; 118 var dict = new Array(); 119 for (i = 0; i < chars; i++) 120 { 121 dict[i] = String.fromCharCode(i); 122 } 123 var original = new String(str); 124 var splitted= original.split(""); 125 var size = splitted.length; 126 var buffer= new String(""); 127 var chain= new String(""); 128 var result = new String(""); 129 for (i = 0; i < size; i++) 130 { 131 var code = original.charCodeAt(i); 132 var current = dict[code]; 133 if (buffer == "") 134 { 135 buffer = current; 136 result += current; 137 } 138 else 139 { 140 if (code <= 255) 141 { 142 result += current; 143 chain = buffer + current; 144 dict[chars] = chain; 145 chars++; 146 buffer = current; 147 } 148 else 149 { 150 chain = dict[code]; 151 if (chain == null) 152 { 153 chain = buffer + buffer.slice(0, 1); 154 } 155 result += chain; 156 dict[chars] = buffer + chain.slice(0, 1); 157 chars++; 158 buffer = chain; 159 } 160 } 161 } 162 result = as3long.utf8.decode(result); 163 return result; 164 } 165 166 /** 167 * Created with JetBrains WebStorm. 168 * User: Administrator 169 * Date: 12-8-3 170 * Time: 下午4:50 171 * To change this template use File | Settings | File Templates. 172 */ 173 function as3long(){ 174 175 } 176 177 as3long.prototype.lzwcn=new lzwcn(); 178 as3long.prototype.utf8=new utf8(); 179 window.as3long=new as3long(); 180 181
压缩版下载地址: https://files.cnblogs.com/longhuang/as3long.min.js
使用方法
1 var str1=as3long.lzwcn.compress("At Microsoft, we've got a long history with email — from Hotmail, our consumer service used by more than 300 million people monthly, to Outlook and Exchange, the software and service managing communications, calendar, and contacts for many millions more.Using the lessons we've learned from both consumers and enterprises, it's our goal to become the very best email service anywhere. That is a big statement – but one we are absolutely committed to deliver upon. That journey starts today.An experience with no compromisesOutlook.com is the first step in creating one complete experience for the next generation of communications. Email should be connected to your friends – whether they like to use Facebook, Twitter, LinkedIn, Google, or a combination. Email should let you get more done, faster – with immediate access to your inbox and tools that can automatically categorize, move, or delete messages you don't want. Email should be deeply integrated with other services – for Outlook.com, you'll find that Office Web Apps, SkyDrive, and, soon, Skype come built right in. And we hope you have already noticed our fast, beautiful user experience. We'd love your feedbackAs we've said before, there is much more to come. If you'd like to follow along in real time, be sure to subscribe to our blog or follow us on Twitter. Along the way, if you've got any questions, comments, or concerns, please submit that feedback via the options menu in our header.Learn more"); 2 var str2=as3long.lzwcn.decompress(str1);
效果:
At Microsoft, we've goāa long historyčith email â fĆm HĕīĭČour cĚsumeĿsņvĄĒuňd bĤmĢĒħan 300 mĭliĚ peoplĒœnħlyČġ Outęok ŗŏExchŗgeůhĒĈĊwarĒŹ ňrŊcũŗagiěŀommunĄatšnsČcaŨndƈČƋŁūactsijĢŝŗŒşƠƱœƉ.UsƕĜħĒŨsĈơčďđĘeƈneŏĴƘŐĕĨƬŃŅrƱƋeūņprğeƢ Ħ'ƱĽĿĔƥ ŰbeŁŅǫƃ đģŐǡāĪĬĮƍƏƊnywƃƻ TžāğŸŐiĜĠƞĪǛāİŐų ĚĒĎŸƉŸbĈlųeŭƗƙĦtǎǫo dȡiǴ upĚ.ȄȆ jǧǍĤȍrưȨƨy.AŘexŤǟǛƐĥħ nȩǯǞƘǠsŲŴoŶ.ǯǣƱǁijiǘāĠepǣŘąNjƟƖȗȣŧeȦĩɅņiɈĒfƳɜǍxāƀǍrƞƠȖfȣƚƜƤƟłȳEĺǻhĽlŏǭƗnǍƯȧŰyǧijɇƧƱȒčƃǁĿǁĤŠkŕȩōĒFƮebɖkČTwȥņČLƕʥdInČGɖgŨļĿėǯbƕɾȲ ʉǺƌʌuʎNJāʗuēɭŝŔȪȗČfaɡĿʝʲɋiƙǎiȎŸcƐDŽȨ ˒ĿƕʭxŸƧȨolɛȵƤŘaųƘɾƤlȢʅeĔǟzƁ˖đˁȪȡɭũǡsƓǡˬĽ˘n'āƇūʈʊˌʍʏĒȫɢȢƕȦgɽȦŏˠĨǓņƌʼnŋʜȓɵĿɔŵkɘƘČ˒'̀ɝ˳ŖāOffŋ Wʬ AppǢSkyDǟ̉˲dČĈĚČ͏yŤȣĒbuĭāǟghȇnȳɂ̩Ēʌ͝˒ĝaljƥƉadĤɍƟƐŏʘ˛ĠČǭ˻ƟfˎȯƍɯɆɲnƐȳ͈'ŏęlj˭ijeǎbƮkAƱĎĐƄĬ̴̟̇ʠĒȈƚŽ˖țŰǯeȳIʁ̼Ώiʥ˫ɵ̀owŸęƖƕ ʹĮƟŅͿƄľʦƌuȝąiʐŰʘbęĜƳδęηōȖŘʱʳrͩιǀDzƇŮǣήĽΜēĕ˲ĤquǷƸƣƘŅūǢƳƬƐrơČɬ˜ςbŞā́ΓΕΗdz˥DZĒŦʆdžϮ˓λʘƃ͵ņ.LNjϴΧe||||||789
At Microsoft, we've got a long history with email — from Hotmail, our consumer service used by more than 300 million people monthly, to Outlook and Exchange, the software and service managing communications, calendar, and contacts for many millions more.Using the lessons we've learned from both consumers and enterprises, it's our goal to become the very best email service anywhere. That is a big statement – but one we are absolutely committed to deliver upon. That journey starts today.An experience with no compromisesOutlook.com is the first step in creating one complete experience for the next generation of communications. Email should be connected to your friends – whether they like to use Facebook, Twitter, LinkedIn, Google, or a combination. Email should let you get more done, faster – with immediate access to your inbox and tools that can automatically categorize, move, or delete messages you don't want. Email should be deeply integrated with other services – for Outlook.com, you'll find that Office Web Apps, SkyDrive, and, soon, Skype come built right in. And we hope you have already noticed our fast, beautiful user experience. We'd love your feedbackAs we've said before, there is much more to come. If you'd like to follow along in real time, be sure to subscribe to our blog or follow us on Twitter. Along the way, if you've got any questions, comments, or concerns, please submit that feedback via the options menu in our header.Learn more||||||1467