<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
    主站蜘蛛池模板: 亚洲精品一区二区三区四区乱码 | 免费无码一区二区| 免费专区丝袜脚调教视频| 久久亚洲精品成人777大小说| 四虎成人精品国产永久免费无码| 爽爽日本在线视频免费| 亚洲色一区二区三区四区| 成人女人A级毛片免费软件| 亚洲欧洲精品一区二区三区| 久草免费手机视频| 亚洲丝袜美腿视频| 蜜臀AV免费一区二区三区| 精品亚洲AV无码一区二区| 久久不见久久见中文字幕免费| 亚洲剧场午夜在线观看| 好男人www免费高清视频在线| 亚洲乱码一区二区三区国产精品| 在线免费观看中文字幕| 亚洲aⅴ无码专区在线观看 | 亚洲最大的黄色网| 免费大片黄在线观看yw| 亚洲人成色77777在线观看 | 亚洲毛片不卡av在线播放一区| 曰批全过程免费视频观看免费软件| 亚洲国产午夜中文字幕精品黄网站 | 久久久受www免费人成| 亚洲AV无码成人专区片在线观看| 免费人成视频在线观看网站| 久久久久亚洲AV无码永不| 欧洲黑大粗无码免费| 无码日韩人妻AV一区免费l| 亚洲AV电影院在线观看| 免费不卡视频一卡二卡| 黄色网址大全免费| 亚洲欧洲成人精品香蕉网| 7x7x7x免费在线观看| 国产成人综合久久精品亚洲| 亚洲日韩欧洲乱码AV夜夜摸| 美女网站免费福利视频| 成人福利在线观看免费视频| 亚洲欧洲一区二区|