一些ActionScript LZW壓縮解壓縮的資源:
- Compression LZW en Actionscript 2 - LAlex devblog v6
- Compression LZW - Shoebox blog - Actionscript 2.0
- LZW compression methods in AS2
LZW Algorithms:
其實 Flash 8 ActionScript LZW 幾百 kb 的資料,速度還算 OK
不過千萬不要貿貿然地,就把程式 copy 去用
建議多K一下 HTTP 規格以及 Flash Doc 再來用
免得資料傳輸量沒減少,反而暴增~~
以下是直接從AS版的LZW改成Java版的LZW:
package idv.ticore.compress;
import java.util.*;
public class LZW {
private static boolean xmlsafe = false;
private LZW() {
}
public static String compress(String str) {
Map dico = new HashMap();
int skipnum = xmlsafe ? 5 : 0;
for (char i = 0 ; i < 256 ; ++i) {
dico.put(Character.toString(i), new Character(i));
}
if (xmlsafe) {
dico.put("<", new Character((char)256));
dico.put(">", new Character((char)257));
dico.put("&", new Character((char)258));
dico.put(""", new Character((char)259));
dico.put(""", new Character((char)260));
}
String res = "";
String txt2encode = str;
String[] splitStr = txt2encode.split("");
int len = splitStr.length;
int nbChar = 256 + skipnum;
String buffer = "";
for (int i = 1; i <= len; i++) {
String current;
if(i <= len - 1) {
current = splitStr[i];
} else {
current = null;
}
if(dico.get(buffer + current) != null) {
buffer += current;
} else {
res += ((Character)dico.get(buffer)).toString();
dico.put(buffer + current, new Character((char)nbChar));
nbChar++;
buffer = current;
}
}
return res;
}
public static String decompress(String str) {
Map dico = new HashMap();
int skipnum = xmlsafe ? 5 : 0;
for (int i = 0 ; i < 256 ; ++i) {
dico.put(Integer.toString(i), Character.toString((char)i));
}
if (xmlsafe) {
dico.put("256", "<");
dico.put("257", ">");
dico.put("258", "&");
dico.put("259", """);
dico.put("260", """);
}
String txt2encode = str;
String[] splitStr = txt2encode.split("");
int len = splitStr.length;
int nbChar = 256 + skipnum;
String buffer = "";
String chaine = "";
String res = "";
for (int i = 1; i < len; i++) {
int code = txt2encode.charAt(i - 1);
String current = (String)dico.get(Integer.toString(code));
if (buffer == "") {
buffer = current;
res += current;
} else {
if (code <= 255 + skipnum) {
res += current;
chaine = buffer + current;
dico.put("" + nbChar, chaine);
nbChar++;
buffer = current;
} else {
chaine = (String)dico.get("" + code);
if (chaine == null) {
chaine = buffer + buffer.substring(0, 1);
}
res += chaine;
dico.put("" + nbChar, buffer + chaine.substring(0, 1));
nbChar++;
buffer = chaine;
}
}
}
return res;
}
}
Read more...



