随笔 - 0, 文章 - 264, 评论 - 170, 引用 - 0
数据加载中……

使用java实现网络传输数据的压缩(来源网络)

import   java.io.*;
import   java.util.zip.*;

public   class   GZIProc   {
    public   static   void   main(String[]   args)   {
       
        byte[]   b1   ,b2   =   null;
        try   {
         
            BufferedInputStream   in   =   new   BufferedInputStream(   new   FileInputStream(args[0]));
            ByteArrayOutputStream   bout   =   new   ByteArrayOutputStream();
            GZIPOutputStream   out   =   new   GZIPOutputStream(bout);
            ObjectOutputStream   oout   =   new   ObjectOutputStream(out);
            //   读取源文件
            b1   =   new   byte[in.available()];    
            in.read(b1,0,b1.length);            
            System.out.println( "压缩前:\n "   +   new   String(b1));
                       
            //   压缩
            //out.write(b1,0,   b1.length);
            oout.writeObject(new   String(b1));
            oout.flush();
            oout.close();
           
            //   读取压缩字节流
            b2   =   bout.toByteArray();
           
            System.out.println( "压缩后:\n "   +   new   String(b2));
            //   关闭数据流
            in.close();
                 
            //   解压
            ObjectInputStream   in2   =   new   ObjectInputStream(new   GZIPInputStream(new   ByteArrayInputStream(b2)));
            byte[]   b3   =   ((String)in2.readObject()).getBytes();
            in2.close();
           
            //   输出
            System.out.println(   new   String(b3));
        }   catch(Exception   e)   {
            e.printStackTrace();
        }
    }
}

posted on 2011-04-17 15:59 小一败涂地 阅读(3439) 评论(0)  编辑  收藏 所属分类: java语言相关android+移动开发jsp,servlet相关


只有注册用户登录后才能发表评论。


网站导航: