David Toy's blogtoydavid.blog.chinaunix.net
cj_gameboy
全部博文(284)
joomla(1)
git(8)
TWave(1)
项目(1)
介绍(6)
错误处理(1)
练习(25)
utf-8(1)
GTK(17)
X-Windows(1)
linux驱动程序(1)
yacc与lex(1)
压缩(1)
信号处理(1)
函数(2)
shuffle压缩算法(1)
旋转门压缩(1)
哈夫曼压缩(4)
小波压缩(2)
压缩(0)
静态库与动态库(4)
调试技术(8)
杂谈(4)
oracle(3)
socket(7)
文件系统相关(1)
多线程(8)
多进程(1)
网络相关(0)
数据库操作(0)
ipc相关(7)
mysql(7)
ftp(2)
算法(1)
基本应用(1)
编程练习(2)
进程列表(1)
direct(1)
MFC(1)
visual studio(1)
DLL技术(1)
win核心技术(2)
UDP(1)
TCPIP(1)
有名管道(1)
邮槽技术(2)
win-IPC(0)
string分解(1)
STL相关(1)
fstream(4)
boost相关(6)
实时数据库(1)
MSserver(2)
oracle技术(20)
2012年(18)
2011年(33)
2010年(83)
2009年(147)
2008年(3)
maoyg082
小雅贝贝
diaoopad
Moonecho
hjshajsh
浪花小雨
cynthia
学无止境
fxtxkktv
麻烦的c
安然之风
12044626
分类: Java
2010-09-03 15:21:34
import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.ObjectInputStream;import java.io.ObjectOutputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipInputStream;import java.util.zip.ZipOutputStream;import java.util.Date;class DataCompression { public static final byte[] compress(Object obj) { if (obj == null) return null; byte[] compressed; ByteArrayOutputStream out = null; ZipOutputStream zout = null; try { out = new ByteArrayOutputStream(); zout = new ZipOutputStream(out); zout.putNextEntry(new ZipEntry("0")); ByteArrayOutputStream out1 = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(out1); oos.writeObject(obj); byte[] bytes = out1.toByteArray(); zout.write(bytes); zout.closeEntry(); compressed = out.toByteArray(); } catch (IOException e) { e.printStackTrace(); compressed = null; } finally { if (zout != null) { try { zout.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } return compressed; } public static final Object decompress(byte[] compressed) { if (compressed == null) return null; ByteArrayOutputStream out = null; ByteArrayInputStream in = null; ZipInputStream zin = null; Object decompressed; try { out = new ByteArrayOutputStream(); in = new ByteArrayInputStream(compressed); zin = new ZipInputStream(in); ZipEntry entry = zin.getNextEntry(); byte[] buffer = new byte[1024]; int offset = -1; while ((offset = zin.read(buffer)) != -1) { out.write(buffer, 0, offset); } ByteArrayInputStream in1 = new ByteArrayInputStream(out.toByteArray()); ObjectInputStream ois = new ObjectInputStream(in1); decompressed = ois.readObject(); } catch (Exception e) { e.printStackTrace(); decompressed = null; } finally { if (zin != null) { try { zin.close(); } catch (IOException e) { } } if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } return decompressed; } }public class javaCompress { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub DataCompression cc = new DataCompression(); float val[] = new float[2000]; float [] tt = null; byte[] dd = null; for(int i=0;i<2000;i++) val[i]=i; dd = cc.compress(val); System.out.println("byte:"+dd.length/4); tt = (float[])(cc.decompress(dd)); System.out.println("tt:"+tt.length); }}
上一篇:java rmi 例子
下一篇:webservice介绍
登录 注册