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;
}
* 功能说明:将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;
}
相关文章推荐
- Java中使用hql,sql查询返回的list<Object> 转成需要的实体对象--方法讲解!
- Java_集合操作_asList方法产生的List对象不可改变
- java中List对象排序通用方法
- java中List对象排序的通用方法
- scala调用java的方法,返回了一个对象链表List<Student>,在scala中遍历该链表获取指定Student的名字name
- java List对象排序方法
- java根据List内对象的属性排序方法
- Java基础-List集合共性方法、List和Set子接口、具体对象的特点
- Java_List对象排序通用方法
- java 对象 list 万能排序方法
- java在list集合指定位置插入对象和js在数组指定位置插入对象的方法
- 用Collections.sort方法对list排序有两种方法 第一种是list中的对象实现Comparable接口,如下: [java] view plain copy /** * 根据ord
- Java中Json字符串直接转换为对象的方法(包括多层List集合)
- java中List对象列表实现去重或取出及排序的方法
- java中List对象排序通用方法
- 黑马程序员:Java基础——List集合共性方法,Iterator迭代和集合具体对象的特点
- Java:集合,对列表(List)中的自定义对象按属性(字段)排序(正序、倒序)的方法
- 阿里巴巴 Java 开发手册:asList 的返回对象是一个 Arrays 内部类,并没有实现集合的修改方法
- Java持久化存储对象Properties的方法list、store、load
- Java方法进行解压/生成有密码保护的压缩文件(zip格式)