隨筆 - 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 小一敗涂地 閱讀(3474) 評論(0)  編輯  收藏 所屬分類: java語言相關 、android+移動開發 、jsp,servlet相關

          主站蜘蛛池模板: 江孜县| 大邑县| 绩溪县| 阿克陶县| 大田县| 合阳县| 新昌县| 甘谷县| 翼城县| 沙河市| 平陆县| 兰坪| 延庆县| 建阳市| 郑州市| 全南县| 彭泽县| 昌吉市| 罗定市| 阿拉善左旗| 仪陇县| 浠水县| 石家庄市| 旅游| 天峨县| 敦化市| 军事| 双桥区| 辽中县| 滕州市| 民和| 古交市| 鞍山市| 建瓯市| 雅江县| 遵义市| 大足县| 应用必备| 呼和浩特市| 钦州市| 广水市|