Android 解压html压缩数据

时间:2023-03-08 23:32:53
Android 解压html压缩数据
public static String unzipHTML(String s){
int endPos = s.indexOf("\r\n\r\n");
if(endPos<10)
return s;
try{
String header = s.substring(0, endPos);
if(header.indexOf("Content-Encoding: gzip")!=-1){
String[] strs = s.split("\r\n\r\n", 2); Log.d("com.she.jyass.c", strs[0] + "\nstrs[1] len=" + strs[1].length()); ByteArrayInputStream bis = new ByteArrayInputStream(strs[1].getBytes("Latin1"));
GZIPInputStream g = new GZIPInputStream(bis); BufferedReader br = new BufferedReader(new InputStreamReader(g));
StringBuffer sb = new StringBuffer();
char[] buffer = new char[10240];
int len;
while ( (len=br.read(buffer)) >0) {
sb.append(buffer, 0, len);
}
s = strs[0] + "\r\n\r\n" + sb.toString();
}
}catch(Exception e){
commonUtils.printError(e, "unzipHTML");
}
return s;
}

使用方法:

String s = new String(rtn.data, "Latin1");

s = commonUtils.unzipHTML(s);

注意,即使你的网页是utf-8的,也要用Latin1,否则出来的是乱码