houyouliang / SpringCloudConfig

springcloudconfig
0 stars 0 forks source link

gzip #10

Open houyouliang opened 7 years ago

houyouliang commented 7 years ago

package com.hat.util.gzip;

import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.List; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream;

public class GZipUtil { 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.finish(); 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;
}

}