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. BZOJ 1647 [Usaco2007 Open]Fliptile 翻格子游戏:部分枚举 位运算

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1647 题意: 在一个n*m(1 <= n,m <= 15)的棋盘上,每一个格子 ...

  2. BZOJ 3400 [Usaco2009 Mar]Cow Frisbee Team 奶牛沙盘队:dp【和为f的倍数】

    题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1375 题意: 给你n个数,你可以从中选任意多个,但不能不选.问你所选数字之和为f的倍数 ...

  3. laravel 在apache或nginx的配置

    laravel 下载后,如何运行起来呢,根据自己的应用,记录了几个关键点: 1.apache 配置: 打开http.conf文件,将mod_rewrite前面的#去掉(启用重写模块): 2.nginx ...

  4. ECMAScript Obejct 属性操作API

    Object 创建对象 我们有很多种方法创建一个对象,Object.create, Object.assign Object.create //字面量 var o = {}; // 等同于 var o ...

  5. 2015推荐的Android框架

    一.Guava Google的基于java1.6的类库集合的扩展项目,包括collections, caching, primitives support, concurrency libraries ...

  6. onhashchange

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 1026 Table Tennis (30)(30 分)

    A table tennis club has N tables available to the public. The tables are numbered from 1 to N. For a ...

  8. .NETFramework:WebClient

    ylbtech-.NETFramework:WebClient 1.程序集 System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5 ...

  9. Poco 编译mysql

    POCO mysql需要自己添加connecter的header和lib MySQL Client: For the MySQL connector, the MySQL client librari ...

  10. Advanced R之函数

    转载请注明出处,谢谢. 再次声明下,本人水平有些,错误之处敬请指正. 函数 函数是R基本的块结构单元:为了掌握本书中的更高级技术,你需要对函数有扎实的了解.也许你已经写过一些函数,并了解函数的基本知识 ...