您的位置:首页 > 编程语言 > Java开发

java的压缩与解压方法以List对象为例

2008-11-23 13:44 435 查看
/**
* 功能说明:将List对象压缩转化为byte[]
* @param data
* @return
* @throws IOException
*/
public static byte[] compressList(List data) throws IOException {
byte[] result=null;
//序列化使用的输出流
ByteArrayOutputStream o = new ByteArrayOutputStream();

GZIPOutputStream gzout=new GZIPOutputStream(o);

//建立对象序列化输出流
ObjectOutputStream out = new ObjectOutputStream(gzout);
out.writeObject(data);
out.flush();
out.close();
gzout.close();
result=o.toByteArray();
o.close();
return result;

}
/**
* 功能说明:将byte[]数据解压成List对象于上面过程逆向
* @param data
* @return
* @throws IOException
*/
public static List uncompressList(byte[] data) throws IOException {
List result=null;

ByteArrayInputStream i = new ByteArrayInputStream(data);

GZIPInputStream gzin=new GZIPInputStream(i);

ObjectInputStream in = new ObjectInputStream(gzin);

try {
result=(List)in.readObject();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

i.close();
gzin.close();
in.close();
return result;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: