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

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

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

    posts - 495,comments - 227,trackbacks - 0
    http://www.cnblogs.com/gengaixue/archive/2013/09/04/3300658.html


    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.util.zip.GZIPInputStream;
    import java.util.zip.GZIPOutputStream;
    import java.util.zip.ZipEntry;
    import java.util.zip.ZipInputStream;
    import java.util.zip.ZipOutputStream;

    public class ZipUtils {

    /**

    * 使用gzip進(jìn)行壓縮
    */
    public static String gzip(String primStr) {
    if (primStr == null || primStr.length() == 0) {
    return primStr;
    }

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    GZIPOutputStream gzip=null;
    try {
    gzip = new GZIPOutputStream(out);
    gzip.write(primStr.getBytes());
    } catch (IOException e) {
    e.printStackTrace();
    }finally{
    if(gzip!=null){
    try {
    gzip.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }


    return new sun.misc.BASE64Encoder().encode(out.toByteArray());
    }

    /**
    *
    * <p>Description:使用gzip進(jìn)行解壓縮</p>
    * @param compressedStr
    * @return
    */
    public static String gunzip(String compressedStr){
    if(compressedStr==null){
    return null;
    }

    ByteArrayOutputStream out= new ByteArrayOutputStream();
    ByteArrayInputStream in=null;
    GZIPInputStream ginzip=null;
    byte[] compressed=null;
    String decompressed = null;
    try {
    compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
    in=new ByteArrayInputStream(compressed);
    ginzip=new GZIPInputStream(in);

    byte[] buffer = new byte[1024];
    int offset = -1;
    while ((offset = ginzip.read(buffer)) != -1) {
    out.write(buffer, 0, offset);
    }
    decompressed=out.toString();
    } catch (IOException e) {
    e.printStackTrace();
    } finally {
    if (ginzip != null) {
    try {
    ginzip.close();
    } catch (IOException e) {
    }
    }
    if (in != null) {
    try {
    in.close();
    } catch (IOException e) {
    }
    }
    if (out != null) {
    try {
    out.close();
    } catch (IOException e) {
    }
    }
    }

    return decompressed;
    }

    /**
    * 使用zip進(jìn)行壓縮
    * @param str 壓縮前的文本
    * @return 返回壓縮后的文本
    */
    public static final String zip(String str) {
    if (str == null)
    return null;
    byte[] compressed;
    ByteArrayOutputStream out = null;
    ZipOutputStream zout = null;
    String compressedStr = null;
    try {
    out = new ByteArrayOutputStream();
    zout = new ZipOutputStream(out);
    zout.putNextEntry(new ZipEntry("0"));
    zout.write(str.getBytes());
    zout.closeEntry();
    compressed = out.toByteArray();
    compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
    } catch (IOException e) {
    compressed = null;
    } finally {
    if (zout != null) {
    try {
    zout.close();
    } catch (IOException e) {
    }
    }
    if (out != null) {
    try {
    out.close();
    } catch (IOException e) {
    }
    }
    }
    return compressedStr;
    }

    /**
    * 使用zip進(jìn)行解壓縮
    * @param compressed 壓縮后的文本
    * @return 解壓后的字符串
    */
    public static final String unzip(String compressedStr) {
    if (compressedStr == null) {
    return null;
    }

    ByteArrayOutputStream out = null;
    ByteArrayInputStream in = null;
    ZipInputStream zin = null;
    String decompressed = null;
    try {
    byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
    out = new ByteArrayOutputStream();
    in = new ByteArrayInputStream(compressed);
    zin = new ZipInputStream(in);
    zin.getNextEntry();
    byte[] buffer = new byte[1024];
    int offset = -1;
    while ((offset = zin.read(buffer)) != -1) {
    out.write(buffer, 0, offset);
    }
    decompressed = out.toString();
    } catch (IOException e) {
    decompressed = null;
    } finally {
    if (zin != null) {
    try {
    zin.close();
    } catch (IOException e) {
    }
    }
    if (in != null) {
    try {
    in.close();
    } catch (IOException e) {
    }
    }
    if (out != null) {
    try {
    out.close();
    } catch (IOException e) {
    }
    }
    }
    return decompressed;
    }
    }
    posted on 2014-06-10 15:58 SIMONE 閱讀(4609) 評論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 亚洲精品美女网站| 一区二区三区在线观看免费| 日本黄色免费观看| 三级网站免费观看| 久久综合久久综合亚洲| 亚洲精品无码日韩国产不卡?V | 成人在线免费视频| 亚洲视频精品在线观看| 国产一区二区免费在线| 免费av一区二区三区| 亚洲欧美日韩国产精品一区| 亚洲高清有码中文字| 亚洲午夜爱爱香蕉片| 一本岛高清v不卡免费一三区| 男人和女人高潮免费网站 | 国产精品亚洲专区一区| 亚洲va中文字幕无码久久| 国语成本人片免费av无码| 中国一级特黄的片子免费 | 国产精品视频白浆免费视频| 亚洲自偷自偷在线成人网站传媒| 国产亚洲?V无码?V男人的天堂| 亚洲一级毛片免费观看| 中文字幕的电影免费网站| 亚洲欧美国产欧美色欲| 亚洲天天在线日亚洲洲精| 啊v在线免费观看| 99精品国产免费久久久久久下载| 中文字幕成人免费高清在线视频 | 18禁男女爽爽爽午夜网站免费| 深夜福利在线视频免费| 在线亚洲高清揄拍自拍一品区| 亚洲国产另类久久久精品| 免费乱码中文字幕网站| 亚洲精品动漫免费二区| 99久久久国产精品免费牛牛四川| 思思久久99热免费精品6| 亚洲AV无码一区二区一二区| 亚洲欧洲国产视频| 亚洲av色福利天堂| 亚洲国产精品SSS在线观看AV|