Java快速開發(fā)平臺(tái)

          www.fastunit.com

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            23 Posts :: 0 Stories :: 273 Comments :: 0 Trackbacks
          數(shù)據(jù)傳輸時(shí),有時(shí)需要將數(shù)據(jù)壓縮和解壓縮,本例使用GZIPOutputStream/GZIPInputStream實(shí)現(xiàn)。

          1、使用ISO-8859-1作為中介編碼,可以保證準(zhǔn)確還原數(shù)據(jù)
          2、字符編碼確定時(shí),可以在uncompress方法最后一句中顯式指定編碼
          import java.io.ByteArrayInputStream;
          import java.io.ByteArrayOutputStream;
          import java.io.IOException;
          import java.util.zip.GZIPInputStream;
          import java.util.zip.GZIPOutputStream;

          // 將一個(gè)字符串按照zip方式壓縮和解壓縮
          public class ZipUtil {

            
          // 壓縮
            public static String compress(String str) throws IOException {
              
          if (str == null || str.length() == 0) {
                
          return str;
              }
              ByteArrayOutputStream out 
          = new ByteArrayOutputStream();
              GZIPOutputStream gzip 
          = new GZIPOutputStream(out);
              gzip.write(str.getBytes());
              gzip.close();
              
          return out.toString("ISO-8859-1");
            }

            
          // 解壓縮
            public static String uncompress(String str) throws IOException {
              
          if (str == null || str.length() == 0) {
                
          return str;
              }
              ByteArrayOutputStream out 
          = new ByteArrayOutputStream();
              ByteArrayInputStream in 
          = new ByteArrayInputStream(str
                  .getBytes(
          "ISO-8859-1"));
              GZIPInputStream gunzip 
          = new GZIPInputStream(in);
              
          byte[] buffer = new byte[256];
              
          int n;
              
          while ((n = gunzip.read(buffer)) >= 0) {
                out.write(buffer, 
          0, n);
              }
              
          // toString()使用平臺(tái)默認(rèn)編碼,也可以顯式的指定如toString("GBK")
              return out.toString();
            }

            
          // 測(cè)試方法
            public static void main(String[] args) throws IOException {
              System.out.println(ZipUtil.uncompress(ZipUtil.compress(
          "中國(guó)China")));
            }

          }
          posted on 2008-04-25 12:37 FastUnit 閱讀(23064) 評(píng)論(8)  編輯  收藏 所屬分類: Java

          Feedback

          # re: 字符串的壓縮和解壓縮[未登錄] 2008-04-25 22:01 Matthew Chen
          toString()不是采用平臺(tái)默認(rèn)編碼,而是就是unicode,而且("中國(guó)China")也是unicode,java內(nèi)部字符串編碼只是這種類型,對(duì)外實(shí)現(xiàn)讀取和寫入文本才是使用平臺(tái)默認(rèn)編碼的。  回復(fù)  更多評(píng)論
            

          # re: 字符串的壓縮和解壓縮 2008-04-25 23:51 FastUnit
          @Matthew Chen
          看一下ByteArrayOutputStream.toString()的源碼,最終使用的編碼是Converters.getDefaultEncodingName(),不同環(huán)境下編碼可能不同,我的是GBK。  回復(fù)  更多評(píng)論
            

          # re: 字符串的壓縮和解壓縮 2008-04-29 16:22 曉秋
          我的輸出結(jié)果是這樣的.
          ??China
          我的編輯器的系統(tǒng)編碼是:utf-8

          若指定編碼時(shí)報(bào)錯(cuò).GZIP is not fomat.  回復(fù)  更多評(píng)論
            

          # re: 字符串的壓縮和解壓縮 2008-05-06 14:07 FastUnit
          @曉秋
          不要修改過渡編碼“ISO-8859-1”;
          傳入字符的編碼是什么,解壓的時(shí)候就指定這個(gè)編碼,在uncompress方法的最后一句中指定,如“return out.toString("UTF-8");”  回復(fù)  更多評(píng)論
            

          # re: 字符串的壓縮和解壓縮[未登錄] 2009-09-04 13:58 菜鳥
          我想問問,為什么壓縮后的字符串怎么更長(zhǎng)了?
          System.out.println("中國(guó)China".length());
          System.out.println(ZipUtil.compress("中國(guó)China").length());
          未壓縮前衛(wèi)7,壓縮后為29,怎么回事?  回復(fù)  更多評(píng)論
            

          # re: 字符串的壓縮和解壓縮[未登錄] 2009-10-20 18:06 菜菜鳥
          @菜鳥
          字符串超過一定長(zhǎng)度時(shí),壓縮才有效!  回復(fù)  更多評(píng)論
            

          # re: 字符串的壓縮和解壓縮[未登錄] 2012-04-20 14:17 yy
          gzip.write(str.getBytes());

          return out.toString();

          此兩處建議默認(rèn)加上相應(yīng)的字符集編碼,以在不同平臺(tái)的不同默認(rèn)字符集下的壓縮和解壓縮結(jié)果完全相同和正確。  回復(fù)  更多評(píng)論
            

          # re: 字符串的壓縮和解壓縮 2016-01-30 19:49 www我
          lz,InputStream為什么輸出卻是GZIPInputStream型,這也導(dǎo)致數(shù)據(jù)流錯(cuò)誤,有辦法解決嗎?  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 河间市| 庆阳市| 武隆县| 阿合奇县| 赫章县| 南京市| 阿拉善右旗| 中卫市| 灌云县| 驻马店市| 黔东| 民和| 开原市| 吉隆县| 洛川县| 思茅市| 徐闻县| 桑植县| 灵宝市| 威远县| 郴州市| 衡水市| 嘉义市| 阳谷县| 建德市| 永登县| 安远县| 香港| 北流市| 禹城市| 皋兰县| 东辽县| 揭东县| 沧源| 阿尔山市| 江北区| 金昌市| 密云县| 西城区| 讷河市| 木兰县|