<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    www.fastunit.com

      BlogJava :: 首頁 :: 聯(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();
      }

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

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

    Feedback

    # re: 字符串的壓縮和解壓縮[未登錄] 2008-04-25 22:01 Matthew Chen
    toString()不是采用平臺(tái)默認(rèn)編碼,而是就是unicode,而且("中國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 菜鳥
    我想問問,為什么壓縮后的字符串怎么更長了?
    System.out.println("中國China".length());
    System.out.println(ZipUtil.compress("中國China").length());
    未壓縮前衛(wèi)7,壓縮后為29,怎么回事?  回復(fù)  更多評(píng)論
      

    # re: 字符串的壓縮和解壓縮[未登錄] 2009-10-20 18:06 菜菜鳥
    @菜鳥
    字符串超過一定長度時(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)論
      

    主站蜘蛛池模板: 日韩免费视频网站| 九九九精品成人免费视频| 免费在线一级毛片| 亚洲精品GV天堂无码男同| 亚洲一区二区久久| 亚洲成人在线免费观看| 免费中文字幕不卡视频| 国产精品亚洲一区二区三区在线观看 | 88av免费观看| 亚洲?V乱码久久精品蜜桃 | 亚洲天天做日日做天天欢毛片| 亚洲xxxx18| fc2免费人成在线视频| 亚洲男人av香蕉爽爽爽爽| 91在线亚洲综合在线| 日本免费中文视频| 自怕偷自怕亚洲精品| 99久久成人国产精品免费| 亚洲国产成人片在线观看| 一级日本高清视频免费观看| 大陆一级毛片免费视频观看| 午夜亚洲国产精品福利| 亚洲精品无码久久久影院相关影片| 国产麻豆一精品一AV一免费| 亚洲成电影在线观看青青| 午夜精品在线免费观看| 一道本在线免费视频| 亚洲午夜免费视频| 四虎www成人影院免费观看| 有码人妻在线免费看片| 久久亚洲国产伦理| 韩国二级毛片免费播放| 国产永久免费高清在线| 亚洲制服丝袜中文字幕| 亚洲福利中文字幕在线网址| 污视频网站在线观看免费| 国产精一品亚洲二区在线播放| 免费福利网站在线观看| 一级毛片不卡免费看老司机| 亚洲成年人电影网站| 亚洲第一区精品观看|