一些ActionScript LZW壓縮解壓縮的資源:
LZW Algorithms:
其實Flash 8 ActionScript LZW幾百kb的資料,速度還算OK
不過千萬不要貿貿然地,就把程式copy去用
建議多K一下HTTP規格以及Flash Doc再來用
免得資料傳輸量沒減少,反而暴增~~
以下是直接從AS版的LZW改成Java版的LZW:
| Java LZW |
1 package idv.ticore.compress; 2 import java.util.*; 3 4 public class LZW { 5 private static boolean xmlsafe = false; 6 private LZW() { 7 } 8 public static String compress(String str) { 9 10 Map dico = new HashMap(); 11 int skipnum = xmlsafe ? 5 : 0; 12 for (char i = 0 ; i < 256 ; ++i) { 13 dico.put(Character.toString(i), new Character(i)); 14 } 15 if (xmlsafe) { 16 dico.put("<", new Character((char)256)); 17 dico.put(">", new Character((char)257)); 18 dico.put("&", new Character((char)258)); 19 dico.put(""", new Character((char)259)); 20 dico.put(""", new Character((char)260)); 21 } 22 String res = ""; 23 String txt2encode = str; 24 String[] splitStr = txt2encode.split(""); 25 int len = splitStr.length; 26 int nbChar = 256 + skipnum; 27 String buffer = ""; 28 29 for (int i = 1; i <= len; i++) { 30 String current; 31 if(i <= len - 1) { 32 current = splitStr[i]; 33 } else { 34 current = null; 35 } 36 if(dico.get(buffer + current) != null) { 37 buffer += current; 38 } else { 39 res += ((Character)dico.get(buffer)).toString(); 40 41 dico.put(buffer + current, new Character((char)nbChar)); 42 nbChar++; 43 buffer = current; 44 } 45 } 46 return res; 47 } 48 49 public static String decompress(String str) { 50 Map dico = new HashMap(); 51 int skipnum = xmlsafe ? 5 : 0; 52 for (int i = 0 ; i < 256 ; ++i) { 53 dico.put(Integer.toString(i), Character.toString((char)i)); 54 } 55 if (xmlsafe) { 56 dico.put("256", "<"); 57 dico.put("257", ">"); 58 dico.put("258", "&"); 59 dico.put("259", """); 60 dico.put("260", """); 61 } 62 String txt2encode = str; 63 String[] splitStr = txt2encode.split(""); 64 int len = splitStr.length; 65 int nbChar = 256 + skipnum; 66 String buffer = ""; 67 String chaine = ""; 68 String res = ""; 69 70 for (int i = 1; i < len; i++) { 71 int code = txt2encode.charAt(i - 1); 72 String current = (String)dico.get(Integer.toString(code)); 73 if (buffer == "") { 74 buffer = current; 75 res += current; 76 } else { 77 if (code <= 255 + skipnum) { 78 res += current; 79 chaine = buffer + current; 80 dico.put("" + nbChar, chaine); 81 nbChar++; 82 buffer = current; 83 } else { 84 chaine = (String)dico.get("" + code); 85 if (chaine == null) { 86 chaine = buffer + buffer.substring(0, 1); 87 } 88 res += chaine; 89 dico.put("" + nbChar, buffer + chaine.substring(0, 1)); 90 nbChar++; 91 buffer = chaine; 92 } 93 } 94 } 95 return res; 96 } 97 } 98 |