package com.miitgxt.common.util;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
*
*
@author wenhao
*
*
*/
public class ZipUtils {
/**
*
* 使用gzip進行壓縮
*/
public static String gzip(String primStr) {
if (primStr == null || primStr.length() == 0) {
return primStr;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = null;
try {
gzip = new GZIPOutputStream(out);
gzip.write(primStr.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (gzip != null) {
try {
gzip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return new sun.misc.BASE64Encoder().encode(out.toByteArray());
}
/**
*
* <p>
* Description:使用gzip進行解壓縮
* </p>
*
*
@param compressedStr
*
@return
*/
public static String gunzip(String compressedStr) {
if (compressedStr == null) {
return null;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = null;
GZIPInputStream ginzip = null;
byte[] compressed = null;
String decompressed = null;
try {
compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
in = new ByteArrayInputStream(compressed);
ginzip = new GZIPInputStream(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) {
}
}
}
return decompressed;
}
/**
* 使用zip進行壓縮
*
*
@param str
* 壓縮前的文本
*
@return 返回壓縮後的文本 */ public static final String zip(String str) { if (str == null) return null; byte[] compressed; ByteArrayOutputStream out = null; ZipOutputStream zout = null; String compressedStr = null; try { out = new ByteArrayOutputStream(); zout = new ZipOutputStream(out); zout.putNextEntry(new ZipEntry("0")); zout.write(str.getBytes("UTF-8")); zout.closeEntry(); compressed = out.toByteArray(); compressedStr = new sun.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) { } } } return compressedStr; } /** * 使用zip進行解壓縮 * * @param compressed * 壓縮後的文本 * @return 解壓後的字符串 */ public static final String unzip(String compressedStr) { if (compressedStr == null) { return null; } ByteArrayOutputStream out = null; ByteArrayInputStream in = null; ZipInputStream zin = null; String decompressed = null; try { byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr); out = new ByteArrayOutputStream(); in = new ByteArrayInputStream(compressed); zin = new ZipInputStream(in); zin.getNextEntry(); byte[] buffer = new byte[1024]; int offset = -1; while ((offset = zin.read(buffer)) != -1) { out.write(buffer, 0, offset); } decompressed = new String(out.toByteArray(), "utf-8"); } 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) { } } } return decompressed; } public static void main(String[] args) { String zipstr = "0999999999999999ewqrewr是發送到發送到"; zipstr = ZipUtils.zip(zipstr); System.out.println("壓縮後的字符串:" + zipstr); String jia = AESUtils.encryptAES(zipstr, "111111"); System.out.println("加密後:" + jia); // 解密 ---base64轉換--解壓 byte[] hou = AESUtils.decryptAES(jia, "111111"); System.out.println("解密後:" + new String(hou)); zipstr = ZipUtils.unzip(new String(hou)); System.out.println("解壓後的字符串:" + zipstr); } public static String readXmlFile(String filePath) throws IOException { String encoding = "UTF-8"; File file = new File(filePath); StringBuffer sb = new StringBuffer(); if (file.isFile() && file.exists()) { // 判斷文件是否存在 InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考慮到編碼格式 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; while ((lineTxt = bufferedReader.readLine()) != null) { sb.append(lineTxt + "\n"); } read.close(); } else { System.out.println("找不到指定的文件"); } return sb.toString(); } }