オブジェクトの圧縮と解凍

1728 ワード

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);
		
	}
}