文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>java-压缩

java-压缩

时间:2010-09-03  来源:cj_gameboy

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

}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载