public class GzipUtil {
/**
* @param str
* @return 压缩
*/
public static String compress(String str) {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = null;
GZIPOutputStream gzip = null;
String compress = "";
try {
out = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
compress = out.toString("ISO-8859-1");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return compress;
}
/**
* @param str
* @return 解压缩
*/
public static String uncompress(String str) {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
GZIPInputStream gzip = null;
String uncompress = "";
try {
out = new ByteArrayOutputStream();
in = new ByteArrayInputStream(str.getBytes("ISO-8859-1"));
gzip = new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = gzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
uncompress = out.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != gzip) {
try {
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return uncompress;
}
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb.append("代码笔记:www.note52.com");
}
String str = sb.toString();
System.out.println("原长度:" + str.length());
System.out.println("压缩后长度:" + GzipUtil.compress(str).length());
String compress = GzipUtil.compress(str);
System.out.println("压缩后内容:" + compress);
System.out.println("解压后内容:" + GzipUtil.uncompress(compress));
}
}
这段代码的压缩效率还是比较高的,对于大段文本,能压缩到原文本的10%左右。但是有个问题时,这个压缩会出现特殊字符,如果用web service调用或者其他封装成xml格式数据时候,会报异常。那么,我们可以,将压缩后的文本通过base64进行编码,解压缩之前先进行base64解码,这样就可以避免特殊字符问题了。如下面代码
public class GzipUtil {
/**
* @param str
* @return 压缩
*/
public static String compress(String str) {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = null;
GZIPOutputStream gzip = null;
String compress = "";
try {
out = new ByteArrayOutputStream();
gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
// 这里增加base64编码
byte[] compressed = out.toByteArray();
compress = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return compress;
}
/**
* @param str
* @return 解压缩
*/
public static String uncompress(String str) {
if (str == null || str.length() == 0) {
return str;
}
ByteArrayOutputStream out = null;
ByteArrayInputStream in = null;
GZIPInputStream gzip = null;
String uncompress = "";
try {
out = new ByteArrayOutputStream();
// 这里增加base64解码
byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(str);
in = new ByteArrayInputStream(compressed);
gzip = new GZIPInputStream(in);
byte[] buffer = new byte[1024];
int offset = -1;
while ((offset = gzip.read(buffer)) != -1) {
out.write(buffer, 0, offset);
}
uncompress = out.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != gzip) {
try {
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != out) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return uncompress;
}
public static void main(String[] args) throws IOException {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 10; i++) {
sb.append("代码笔记:www.note52.com");
}
String str = sb.toString();
System.out.println("原长度:" + str.length());
System.out.println("压缩后长度:" + GzipUtil.compress(str).length());
String compress = GzipUtil.compress(str);
System.out.println("压缩后内容:" + compress);
System.out.println("解压后内容:" + GzipUtil.uncompress(compress));
}
}
base64编码后,就不会出现特殊字符了,完美解决xml传输问题。