<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) 評(píng)論(0)  編輯  收藏 所屬分類: JAVA
    主站蜘蛛池模板: 99久在线国内在线播放免费观看| 亚洲乱亚洲乱妇无码| 免费精品国产自产拍在线观看 | 中文字幕在线免费播放| 一本久到久久亚洲综合| 一级特级aaaa毛片免费观看 | 国产精品亚洲一区二区在线观看| 国产三级在线观看免费| 亚洲欧美日韩中文二区| 真实乱视频国产免费观看| 亚洲Av永久无码精品黑人| heyzo亚洲精品日韩| 久久久精品国产亚洲成人满18免费网站 | 最新亚洲精品国偷自产在线| 久久天天躁狠狠躁夜夜免费观看| 色在线亚洲视频www| 免费观看的av毛片的网站| 麻豆va在线精品免费播放| 国产亚洲精品自在线观看| 免费成人在线视频观看| 亚洲女人初试黑人巨高清| 免费无码不卡视频在线观看| 极品色天使在线婷婷天堂亚洲| 狠狠亚洲狠狠欧洲2019| 亚洲精品免费在线观看| 国产成人精品日本亚洲专| 免费国产小视频在线观看| 国产午夜无码片免费| 亚洲免费黄色网址| 又黄又爽无遮挡免费视频| 国产精品99精品久久免费| 亚洲人成77777在线观看网| 免费在线看片网站| 亚洲午夜免费视频| 亚洲av永久中文无码精品| 亚洲精品一品区二品区三品区| 我的小后妈韩剧在线看免费高清版| 亚洲国产AV无码一区二区三区| 中文字幕亚洲无线码a| 免费在线观看视频网站| 污污污视频在线免费观看|