Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1063446
  • 博文数量: 284
  • 博客积分: 8223
  • 博客等级: 中将
  • 技术积分: 3188
  • 用 户 组: 普通用户
  • 注册时间: 2008-12-01 13:26
文章分类

全部博文(284)

文章存档

2012年(18)

2011年(33)

2010年(83)

2009年(147)

2008年(3)

分类: 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);
    }

}


阅读(568) | 评论(0) | 转发(0) |
0

上一篇:java rmi 例子

下一篇:webservice介绍

给主人留下些什么吧!~~