public class QueryPropertyGroup {
/**
* 把对象压缩后转换为字节流
* @param obj
* @return
*/
public static byte[] enCompObject(Object obj) {
byte[] data = null;
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
try {
//字节输出流
GZIPOutputStream gzipOut = new GZIPOutputStream(byteOut);
//创建压缩输出流
ObjectOutputStream objOut = new ObjectOutputStream(gzipOut);
//对象输出流
objOut.writeObject(obj);
objOut.flush();
objOut.close();
gzipOut.close();
data = byteOut.toByteArray();
byteOut.close();
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
/**
* 解压文件
* @param data
* @return
*/
public static Object deComObject(byte[] data){
Object obj = null;
try {
//字节输入流
ByteArrayInputStream byteInput = new ByteArrayInputStream(data);
//压缩输入流
GZIPInputStream gzipInput = new GZIPInputStream(byteInput);
//对象输入流
ObjectInputStream objInput = new ObjectInputStream(gzipInput);
obj = objInput.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return obj;
}
/**
* 测试
* @param args
*/
public static void main(String[] args) {
String kk = new String("测试");
byte[] data = enCompObject(kk);
System.out.println(" data's siz is: " + data.length);
Object obj = deComObject(data);
System.out.println(" obj is: " + obj.getClass() + " " + obj);
}
}