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 閱讀(4613) 評論(0)  編輯  收藏 所屬分類: JAVA
          主站蜘蛛池模板: 旬阳县| 清苑县| 鄂托克前旗| 宁都县| 古田县| 渭源县| 霍山县| 平罗县| 鄱阳县| 小金县| 隆德县| 巴彦淖尔市| 施秉县| 双柏县| 永登县| 怀柔区| 博爱县| 南充市| 水城县| 台中市| 岢岚县| 浪卡子县| 临泽县| 彰化县| 米林县| 贵溪市| 桃园县| 韶关市| 柳林县| 剑川县| 仪征市| 布拖县| 招远市| 沿河| 海口市| 沙河市| 长兴县| 桂东县| 富蕴县| 东辽县| 鲁山县|