<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
    主站蜘蛛池模板: 两个人的视频高清在线观看免费 | 亚洲AV无码不卡在线观看下载| 亚洲首页在线观看| 可以免费观看的国产视频| 中文字幕第一页亚洲| 国产精品高清免费网站| 亚洲综合av永久无码精品一区二区| 极品美女一级毛片免费| 亚洲欧洲一区二区三区| 国产免费区在线观看十分钟| 亚洲欧洲自拍拍偷午夜色无码| 免费国产污网站在线观看| 日本亚洲欧洲免费天堂午夜看片女人员 | 3344在线看片免费| 久久亚洲免费视频| 亚洲高清免费在线观看| 亚洲免费观看在线视频| 永久免费av无码网站韩国毛片| 国产精品亚洲自在线播放页码| 免费网站看v片在线香蕉| 日日狠狠久久偷偷色综合免费 | 亚洲一级毛片在线观| 妞干网免费观看视频| 美女18毛片免费视频| 亚洲韩国精品无码一区二区三区| 免费观看久久精彩视频| 亚洲国产品综合人成综合网站| 午夜成年女人毛片免费观看| 成年免费大片黄在线观看com| 国产亚洲一区二区三区在线观看 | 全部免费国产潢色一级| 99在线热播精品免费99热| 亚洲精品国产啊女成拍色拍| 在线天堂免费观看.WWW| 一级毛片正片免费视频手机看| 亚洲av午夜福利精品一区人妖| 在线a毛片免费视频观看| 搡女人免费免费视频观看| 亚洲国产精品综合久久20| 亚洲国产成人VA在线观看| 中文字幕在线免费观看|