packagecom.wy.data.emaildata.util;importorg.apache.commons.io.FileUtils;importjava.io.ByteArrayInputStream;importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.IOException;importjava.util.zip.GZIPInputStream;importjava.util.zip.GZIPOutputStream;importjava.util.zip.ZipEntry;importjava.util.zip.ZipInputStream;importjava.util.zip.ZipOutputStream;public classZipUtils {/*** 使用gzip进行压缩*/
public staticString gzip(String primStr) {if (primStr == null || primStr.length() == 0) {returnprimStr;
}
ByteArrayOutputStream out= newByteArrayOutputStream();
GZIPOutputStream gzip= null;try{
gzip= newGZIPOutputStream(out);
gzip.write(primStr.getBytes());
}catch(IOException e) {
e.printStackTrace();
}finally{if (gzip != null) {try{
gzip.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}return newsun.misc.BASE64Encoder().encode(out.toByteArray());
}/***
Description:使用gzip进行解压缩
*
*@paramcompressedStr
*@return
*/
public staticString gunzip(String compressedStr) {if (compressedStr == null) {return null;
}
ByteArrayOutputStream out= newByteArrayOutputStream();
ByteArrayInputStream in= null;
GZIPInputStream ginzip= null;byte[] compressed = null;
String decompressed= null;try{
compressed= newsun.misc.BASE64Decoder().decodeBuffer(compressedStr);
in= newByteArrayInputStream(compressed);
ginzip= newGZIPInputStream(in);byte[] buffer = new byte[1024];int offset = -1;while ((offset = ginzip.read(buffer)) != -1) {
out.write(buffer,0, offset);
}
decompressed=out.toString();
}catch(IOException e) {
e.printStackTrace();
}finally{if (ginzip != null) {try{
ginzip.close();
}catch(IOException e) {
}
}if (in != null) {try{
in.close();
}catch(IOException e) {
}
}if (out != null) {try{
out.close();
}catch(IOException e) {
}
}
}returndecompressed;
}/*** 使用zip进行压缩
*
*@paramstr 压缩前的文本
*@return返回压缩后的文本*/
public static finalString zip(String str) {if (str == null)return null;byte[] compressed;
ByteArrayOutputStream out= null;
ZipOutputStream zout= null;
String compressedStr= null;try{
out= newByteArrayOutputStream();
zout= newZipOutputStream(out);
zout.putNextEntry(new ZipEntry("0"));
zout.write(str.getBytes());
zout.closeEntry();
compressed=out.toByteArray();
compressedStr= newsun.misc.BASE64Encoder().encodeBuffer(compressed);
}catch(IOException e) {
compressed= null;
}finally{if (zout != null) {try{
zout.close();
}catch(IOException e) {
}
}if (out != null) {try{
out.close();
}catch(IOException e) {
}
}
}returncompressedStr;
}/*** 使用zip进行解压缩
*
*@paramcompressedStr 压缩后的文本
*@return解压后的字符串*/
public static finalString unzip(String compressedStr) {if (compressedStr == null) {return null;
}
ByteArrayOutputStream out= null;
ByteArrayInputStream in= null;
ZipInputStream zin= null;
String decompressed= null;try{byte[] compressed = newsun.misc.BASE64Decoder().decodeBuffer(compressedStr);
out= newByteArrayOutputStream();
in= newByteArrayInputStream(compressed);
zin= newZipInputStream(in);
zin.getNextEntry();byte[] buffer = new byte[1024];int offset = -1;while ((offset = zin.read(buffer)) != -1) {
out.write(buffer,0, offset);
}
decompressed=out.toString();
}catch(IOException e) {
decompressed= null;
}finally{if (zin != null) {try{
zin.close();
}catch(IOException e) {
}
}if (in != null) {try{
in.close();
}catch(IOException e) {
}
}if (out != null) {try{
out.close();
}catch(IOException e) {
}
}
}returndecompressed;
}public static void main(String[] args) throwsIOException {
String strOld=FileUtils.readFileToString(new File("D:/test.txt"),"utf-8");
System.out.println(strOld.length());
String gzip=zip(strOld);
System.out.println(gzip.length());
}
}