1. package com.yundaex.common.exception.util;
  2.  
  3. import java.io.ByteArrayInputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.util.zip.GZIPInputStream;
  8. import java.util.zip.GZIPOutputStream;
  9. import java.util.zip.ZipEntry;
  10. import java.util.zip.ZipInputStream;
  11. import java.util.zip.ZipOutputStream;
  12.  
  13. public class ZipUtils {
  14.  
  15. /**
  16. *
  17. * 使用gzip进行压缩
  18. * @throws UnsupportedEncodingException
  19. */
  20. public static String gzip(String primStr) throws UnsupportedEncodingException {
  21. if (primStr == null || primStr.length() == 0) {
  22. return primStr;
  23. }
  24.  
  25. ByteArrayOutputStream out = new ByteArrayOutputStream();
  26.  
  27. GZIPOutputStream gzip = null;
  28. try {
  29. gzip = new GZIPOutputStream(out);
  30. gzip.write(primStr.getBytes());
  31. } catch (IOException e) {
  32. e.printStackTrace();
  33. } finally {
  34. if (gzip != null) {
  35. try {
  36. gzip.close();
  37. } catch (IOException e) {
  38. e.printStackTrace();
  39. }
  40. }
  41. }
  42. // System.out.println(out.toString("ISO-8859-1"));
  43. // System.out.println(Base64.encode(out.toString("ISO-8859-1")));
  44. System.out.println(out.toString("ISO-8859-1"));
  45. return out.toString("ISO-8859-1");
  46. }
  47.  
  48. /**
  49. *
  50. * <p>
  51. * Description:使用gzip进行解压缩
  52. * </p>
  53. *
  54. * @param compressedStr
  55. * @return
  56. */
  57. public static String gunzip(String compressedStr) {
  58. if (compressedStr == null) {
  59. return null;
  60. }
  61.  
  62. ByteArrayOutputStream out = new ByteArrayOutputStream();
  63. ByteArrayInputStream in = null;
  64. GZIPInputStream ginzip = null;
  65. byte[] compressed = null;
  66. String decompressed = null;
  67. try {
  68. compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
  69. in = new ByteArrayInputStream(compressed);
  70. ginzip = new GZIPInputStream(in);
  71.  
  72. byte[] buffer = new byte[1024];
  73. int offset = -1;
  74. while ((offset = ginzip.read(buffer)) != -1) {
  75. out.write(buffer, 0, offset);
  76. }
  77. decompressed = out.toString();
  78. } catch (IOException e) {
  79. e.printStackTrace();
  80. } finally {
  81. if (ginzip != null) {
  82. try {
  83. ginzip.close();
  84. } catch (IOException e) {
  85. }
  86. }
  87. if (in != null) {
  88. try {
  89. in.close();
  90. } catch (IOException e) {
  91. }
  92. }
  93. if (out != null) {
  94. try {
  95. out.close();
  96. } catch (IOException e) {
  97. }
  98. }
  99. }
  100.  
  101. return decompressed;
  102. }
  103.  
  104. /**
  105. * 使用zip进行压缩
  106. *
  107. * @param str
  108. * 压缩前的文本
  109. * @return 返回压缩后的文本
  110. */
  111. public static final String zip(String str) {
  112. if (str == null)
  113. return null;
  114. byte[] compressed;
  115. ByteArrayOutputStream out = null;
  116. ZipOutputStream zout = null;
  117. String compressedStr = null;
  118. try {
  119. out = new ByteArrayOutputStream();
  120. zout = new ZipOutputStream(out);
  121. zout.putNextEntry(new ZipEntry("0"));
  122. zout.write(str.getBytes());
  123. zout.closeEntry();
  124. compressed = out.toByteArray();
  125. compressedStr = new sun.misc.BASE64Encoder().encodeBuffer(compressed);
  126. } catch (IOException e) {
  127. compressed = null;
  128. } finally {
  129. if (zout != null) {
  130. try {
  131. zout.close();
  132. } catch (IOException e) {
  133. }
  134. }
  135. if (out != null) {
  136. try {
  137. out.close();
  138. } catch (IOException e) {
  139. }
  140. }
  141. }
  142. return compressedStr;
  143. }
  144.  
  145. /**
  146. * 使用zip进行解压缩
  147. *
  148. * @param compressed
  149. * 压缩后的文本
  150. * @return 解压后的字符串
  151. */
  152. public static final String unzip(String compressedStr) {
  153. if (compressedStr == null) {
  154. return null;
  155. }
  156.  
  157. ByteArrayOutputStream out = null;
  158. ByteArrayInputStream in = null;
  159. ZipInputStream zin = null;
  160. String decompressed = null;
  161. try {
  162. byte[] compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
  163. out = new ByteArrayOutputStream();
  164. in = new ByteArrayInputStream(compressed);
  165. zin = new ZipInputStream(in);
  166. zin.getNextEntry();
  167. byte[] buffer = new byte[1024];
  168. int offset = -1;
  169. while ((offset = zin.read(buffer)) != -1) {
  170. out.write(buffer, 0, offset);
  171. }
  172. decompressed = out.toString();
  173. } catch (IOException e) {
  174. decompressed = null;
  175. } finally {
  176. if (zin != null) {
  177. try {
  178. zin.close();
  179. } catch (IOException e) {
  180. }
  181. }
  182. if (in != null) {
  183. try {
  184. in.close();
  185. } catch (IOException e) {
  186. }
  187. }
  188. if (out != null) {
  189. try {
  190. out.close();
  191. } catch (IOException e) {
  192. }
  193. }
  194. }
  195. return decompressed;
  196. }
  197. }

ZipUtils的更多相关文章

  1. 压缩工具类 - ZipUtils.java

    压缩工具类,提供压缩文件.解压文件的方法. 源码如下:(点击下载 - ZipUtils.java .FolderUtils.java.ant-1.7.0.jar.commons-io-2.4.jar. ...

  2. 项目实战工具类(二):ZipUtils(压缩/解压缩文件相关)

    import android.content.Context; import android.util.Log; import java.io.File; import java.io.FileInp ...

  3. Android总结之Gzip/Zip压缩

    前言: 做过Android网络开发的都知道,在网络传输中我们一般都会开启GZIP压缩,但是出于刨根问底的天性仅仅知道如何开启就不能满足俺的好奇心的,所以想着写个demo测试一下比较常用的两个数据压缩方 ...

  4. 图片资源的加密和cocos2d-x中的解密

    主要处理png图片,其他格式图片也是一样的原理.阅读前可以简略了解一下png格式图片的Data trunck. 首先使用python脚本去掉png的PNG SIG(8 bytes) 以及末尾的PNGI ...

  5. ZIP4J---ZIP文件压缩与解压缩学习

    package com.wbh.common.utils; import java.io.File; import java.io.FileInputStream; import java.io.IO ...

  6. Java实现zip压缩多个文件下载

    为了更好的演示,首先创建一个文件实体FileBean,包含了文件路径和文件名称: package com.javaweb.entity; import java.io.Serializable; /* ...

  7. 破解TexturePacker加密资源

    http://blog.csdn.net/ynnmnm/article/details/38392795 最近我们要开一个新项目,UI与交互打算借鉴当前正火的<圣火英雄传>,程序开发为了和 ...

  8. cocos2dx 3.x(TexturePacker进行图片加密)

    游戏开发过程中要涉及到大量的图片,使用TexturePacker可以把小图合成大图.这是我们使用最多的功能,但是TexturePacker还带有对图片加密的功能.之前还是对加密不慎了解,所以写下来分享 ...

  9. java GZIP压缩和解压

    最近碰到了一个按GZIP解压指定的输入流数据,备份下 import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream ...

随机推荐

  1. JS工具函数汇总

    备注:http://phpjs.org/  这个站点把PHP常用的方法用js实现了,推荐一下 1.从数组中随机获取几个不重复项 //从一个给定的数组arr中,随机返回num个不重复项 function ...

  2. 分享知识-快乐自己:SpringMVC 结合使用拦截器(判断是否用户是否已登陆)

    基础拦截器操作: 拦截器是一种AOP操作实现,那么在AOP之中用户一定不需要去关注拦截器的存在,用户只需要按照自己已经习惯的处理方式进行代码的编写即可. 首先我们先创建一个自定义的拦截器: packa ...

  3. 单元测试:TESTNG和powermock的使用

    pom文件: <properties>        <testng.version>6.8</testng.version>        <powermo ...

  4. mysql密码过期的修改方法(your password has expired)

    今天打开SQLyog提示密码过期:Your password has expired 解决方法:    1.  启动MySQL服务 2.  启动MySQL后台 3.  执行以下命令 step 1: S ...

  5. java对象的初始化过程和创建对象的几种方式

    1.加载父类,加载父类的静态属性和静态代码块 2.加载子类,加载子类的静态属性和静态代码块 3.初始化父类中的非静态属性并赋初值,执行父类非静态代码块,执行父类构造. 4.初始化子类中的非静态属性并赋 ...

  6. Linux 查看并删除.svn目录

    1. find . -type d -name ".svn"|xargs rm -rf

  7. Vmware ESXi 6.5 安装手册

    1          安装前准备 1.1          硬件环境准备 无 备注: 本指导书以虚拟光驱.虚拟软驱为例,如使用物理光驱.物理软驱安装系统操作则以实际系统光盘.软盘代替. 1.2     ...

  8. 树——平衡二叉树插入和查找的JAVA实现(2):增加删除方法

    package com.tomsnail.data.tree; /** * AVL二叉平衡树 * @author tomsnail * @date 2015年3月30日 下午4:35:50 */ pu ...

  9. 《TCP/IP详解卷一:协议》 概述

    分层           TCP/IP协议族是一组不同层次上的多个协议的组合.TCP/IP通常被认为是一个四层次协议系统.   链路层(数据链路层或网络接口层):通常包括操作系统中的设备驱动程序和计算 ...

  10. HDU4027(线段树单点更新区间)

    Can you answer these queries? Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65768/65768 K ...