package com.util; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.StreamCorruptedException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import org.kobjects.base64.Base64; public class IoUtil { /*zip解压缩*/ public static Object byte_obj(byte[] b) throws StreamCorruptedException, IOException, ClassNotFoundException { ByteArrayInputStream bin = null; GZIPInputStream gzip = null; ObjectInputStream oin = null; try { bin = new ByteArrayInputStream(b); gzip = new GZIPInputStream(bin); oin = new ObjectInputStream(gzip); return oin.readObject(); } finally { if (oin != null) { oin.close(); } if (gzip != null) { gzip.close(); } if (bin != null) { bin.close(); } System.out.println("流关闭---"); } } public static Object byte_obj2(byte[] b) throws StreamCorruptedException, IOException, ClassNotFoundException { ByteArrayInputStream bin = null; ObjectInputStream oin = null; try { bin = new ByteArrayInputStream(b); oin = new ObjectInputStream(bin); return oin.readObject(); } finally { if (oin != null) { oin.close(); } if (bin != null) { bin.close(); } System.out.println("留关闭"); } } /*gzip压缩*/ public static byte[] getbyte(Object ob) throws Exception { ByteArrayOutputStream byteOut = null; GZIPOutputStream gziOut = null; ObjectOutputStream objectOut = null; try { byteOut = new ByteArrayOutputStream(); gziOut = new GZIPOutputStream(byteOut); objectOut = new ObjectOutputStream(gziOut); objectOut.writeObject(ob); objectOut.flush(); objectOut.close(); gziOut.close(); byteOut.close(); return byteOut.toByteArray(); } finally { // if(objectOut!=null){ // objectOut.close(); // } // if(gziOut!=null){ //// gziOut.finish(); // gziOut.close(); // } // if(byteOut!=null){ // byteOut.close(); // } } } public static String ob_base64(Object ob) throws IOException { ByteArrayOutputStream bOut = null; ObjectOutputStream objOut = null; try { bOut = new ByteArrayOutputStream(); objOut = new ObjectOutputStream(bOut); objOut.writeObject(ob); return Base64.encode(bOut.toByteArray()); } finally { objOut.flush(); objOut.close(); bOut.flush(); bOut.close(); } } }