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

          主站蜘蛛池模板: 宿迁市| 墨江| 平武县| 旬阳县| 察隅县| 丰顺县| 基隆市| 拜泉县| 威海市| 邯郸县| 洞头县| 靖江市| 南城县| 南部县| 兴仁县| 南宫市| 汾阳市| 新晃| 基隆市| 乡宁县| 建平县| 浮山县| 康乐县| 余庆县| 皋兰县| 申扎县| 资兴市| 方城县| 巨野县| 临安市| 崇礼县| 昆明市| 平潭县| 阿坝| 和平区| 克东县| 桂平市| 瓮安县| 乌兰浩特市| 松阳县| 新邵县|