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

gzip对字符串的压缩和解压的更多相关文章

  1. 使用zlib实现gzip格式数据的压缩和解压

    注意代码中的注释部分,这里设置是专门针对gzip的,缺少了就不行了,gzip压缩格式和其他格式的区别就在这里. Bytef 就是 unsigned char,uLong就是 unsigned long ...

  2. 对数据进行GZIP压缩和解压

    public class GzipUtils { /** * 对字符串进行gzip压缩 * @param data * @return * @throws IOException */ public ...

  3. C#实现通过Gzip来对数据进行压缩和解压

    C#实现通过Gzip来对数据进行压缩和解压 internal static byte[] Compress(byte[] data) { using (var compressedStream = n ...

  4. VB6进行GZIP解压&C#进行GZIP压缩和解压

    VB进行GZIP解压的,DLL是系统的,如果没有 [点击下载] Option Explicit 'GZIP API '----------------------------------------- ...

  5. 使用pako.js实现gzip的压缩和解压

    poko.js可至Github下载:https://github.com/nodeca/pako 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...

  6. Linux 时间日期类、搜索查找类、 压缩和解压类指令

    l 时间日期类 date指令-显示当前日期 基本语法 1) date (功能描述:显示当前时间) 2) date +%Y (功能描述:显示当前年份) 3) date +%m (功能描述:显示当前月份) ...

  7. linux常用命令:4文件压缩和解压命令

    文件压缩和解压命令 压缩命令:gzip.tar[-czf].zip.bzip2 解压缩命令:gunzip.tar[-xzf].unzip.bunzip2 1. 命令名称:gzip 命令英文原意:GNU ...

  8. .net文件压缩和解压及中文文件夹名称乱码问题

    /**************************注释区域内为引用http://www.cnblogs.com/zhaozhan/archive/2012/05/28/2520701.html的博 ...

  9. Linux下的压缩和解压

    1. gzip, bzip2 能否直接压缩目录呢?不可以 2. 请快速写出,使用gzip和bzip2压缩和解压一个文件的命令.压缩:gzip 1.txt bzip2 1.txt解压:gzip -d 1 ...

随机推荐

  1. 剑指offer——python【第4题】重建二叉树

    题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...

  2. java基础 第八章课后习题

    1.什么是二重循环?在内层循环中使用continue和break语句,程序如何跳转? 答:二重循环就是一个循环结构体内又包含另一个完整的循环结构. continue语句跳转时是跳过了内层循环中的剩余语 ...

  3. 简单数据库开发之dao层开发

    数据库 dao层是用来与底层数据库连接的一系列代码,它因上层service层调用而调用底层数据库,因为一般的数据库不会只存在一到几张表格,所以必须定义出dao层的接口协议,方便各种表格的操作. dao ...

  4. docsis cm 上线过程(bigwhite)

    扫描与同步下行(SYNC消息) 获取上行参数(UCD消息.MAP消息)  通过测距完成时间偏移等的调整(RNG消息) 设备类型鉴定(可选,DCI消息) 建立IP通道(DHCP)  同步系统时间(TOD ...

  5. RobotFramework环境配置:默认以管理员权限运行cmd

    设置cmd以管理员权限运行 目的:创建或删除文件等命令时,需要管理员权限运行cmd(linux以root用户登录).   例如,创建日志目录.   方法一: 1.激活administrator用户 2 ...

  6. 上传及更新代码到github(以及如何在vscode上提交自己的代码)

    上传本地代码 第一步:去github上创建自己的Repository,创建页面如下图所示: 红框为新建的仓库的https地址 第二步: echo "# Test" >> ...

  7. 关于ico图标

    ico图标可以作为网页标签上显示的小logo,比如: 要获取一个网站的ico图标,只需要在url后输入/favicon.ico即可,比如   https://www.baidu.com/favicon ...

  8. Exp5 MSF基础应用 20164320 王浩

    1. 实践目标 本实践目标是掌握metasploit的基本应用方式,重点常用的三种攻击方式的思路.具体需要完成: 1.1一个主动攻击实践,如ms08_067; (1分) 1.2 一个针对浏览器的攻击, ...

  9. allegro17.2 错误记录

    1.网表导入PCB时出现如下错误::error with pin number 'P11'in device ' lfbga176'::Unable to find pin name in adfnc ...

  10. python类与对象-如何让对象支持上下文管理

    如何让对象支持上下文管理 问题举例 一个telnet客户端的类TelnetClient, 调用实例的connect(),login(),interact方法 启动客户端与服务器交互,交互完毕后需要调用 ...