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

          主站蜘蛛池模板: 临桂县| 南投县| 唐山市| 桂林市| 眉山市| 靖边县| 叙永县| 嫩江县| 石林| 洪洞县| 汶上县| 黄梅县| 兰考县| 苏尼特左旗| 泾川县| 藁城市| 涟源市| 柏乡县| 城口县| 京山县| 闻喜县| 县级市| 拜泉县| 万源市| 蒙自县| 刚察县| 南昌县| 图木舒克市| 绥阳县| 临沧市| 达孜县| 泾川县| 勃利县| 罗甸县| 新和县| 万盛区| 荆州市| 濉溪县| 保康县| 铁岭市| 沁源县|