隨筆 - 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相關

          主站蜘蛛池模板: 巴林左旗| 延安市| 通州市| 迭部县| 温泉县| 濮阳市| 库车县| 巨野县| 长沙市| 阿拉善右旗| 邳州市| 应城市| 鄢陵县| 青岛市| 抚顺市| 武乡县| 永宁县| 阳高县| 深水埗区| 大新县| 石渠县| 四会市| 江西省| 资讯 | 六盘水市| 霍城县| 富顺县| 菏泽市| 宁晋县| 葵青区| 成武县| 麻阳| 新乐市| 宁明县| 桃园县| 沁源县| 锦州市| 潍坊市| 钟祥市| 禄丰县| 夹江县|