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

          主站蜘蛛池模板: 浦江县| 香河县| 水富县| 昭觉县| 枞阳县| 庆云县| 中西区| 福海县| 青河县| 上林县| 陆河县| 信丰县| 灵石县| 玛曲县| 内乡县| 灵璧县| 清远市| 长治县| 中西区| 东莞市| 基隆市| 行唐县| 宿松县| 远安县| 英吉沙县| 盖州市| 安乡县| 灌阳县| 遵义县| 淳化县| 宁夏| 常山县| 阳新县| 厦门市| 肃宁县| 伊吾县| 沐川县| 常山县| 宕昌县| 蒙阴县| 莲花县|