说明:

1、一般来说要实现压缩,那么返回方式一般是用byte[]数组。

2、研究发现byte[]数组在转成可读的String时,大小会还原回原来的。

3、如果采用压缩之后不可读的String时,互相转换大小会变小,唯一缺点就是转出的String不可读,需要再次解码之后才可读。

4、对于压缩一般最近常听的应该就是gzip这些。

实现一:

  1. /***
  2. * 压缩GZip
  3. *
  4. * @param data
  5. * @return
  6. */
  7. public static byte[] gZip(byte[] data) {
  8. byte[] b = null;
  9. try {
  10. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  11. GZIPOutputStream gzip = new GZIPOutputStream(bos);
  12. gzip.write(data);
  13. gzip.finish();
  14. gzip.close();
  15. b = bos.toByteArray();
  16. bos.close();
  17. } catch (Exception ex) {
  18. ex.printStackTrace();
  19. }
  20. return b;
  21. }
  22.  
  23. /***
  24. * 解压GZip
  25. *
  26. * @param data
  27. * @return
  28. */
  29. public static byte[] unGZip(byte[] data) {
  30. byte[] b = null;
  31. try {
  32. ByteArrayInputStream bis = new ByteArrayInputStream(data);
  33. GZIPInputStream gzip = new GZIPInputStream(bis);
  34. byte[] buf = new byte[1024];
  35. int num = -1;
  36. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  37. while ((num = gzip.read(buf, 0, buf.length)) != -1) {
  38. baos.write(buf, 0, num);
  39. }
  40. b = baos.toByteArray();
  41. baos.flush();
  42. baos.close();
  43. gzip.close();
  44. bis.close();
  45. } catch (Exception ex) {
  46. ex.printStackTrace();
  47. }
  48. return b;
  49. }
  50.  
  51. /***
  52. * 压缩Zip
  53. *
  54. * @param data
  55. * @return
  56. */
  57. public static byte[] zip(byte[] data) {
  58. byte[] b = null;
  59. try {
  60. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  61. ZipOutputStream zip = new ZipOutputStream(bos);
  62. ZipEntry entry = new ZipEntry("zip");
  63. entry.setSize(data.length);
  64. zip.putNextEntry(entry);
  65. zip.write(data);
  66. zip.closeEntry();
  67. zip.close();
  68. b = bos.toByteArray();
  69. bos.close();
  70. } catch (Exception ex) {
  71. ex.printStackTrace();
  72. }
  73. return b;
  74. }
  75.  
  76. /***
  77. * 解压Zip
  78. *
  79. * @param data
  80. * @return
  81. */
  82. public static byte[] unZip(byte[] data) {
  83. byte[] b = null;
  84. try {
  85. ByteArrayInputStream bis = new ByteArrayInputStream(data);
  86. ZipInputStream zip = new ZipInputStream(bis);
  87. while (zip.getNextEntry() != null) {
  88. byte[] buf = new byte[1024];
  89. int num = -1;
  90. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  91. while ((num = zip.read(buf, 0, buf.length)) != -1) {
  92. baos.write(buf, 0, num);
  93. }
  94. b = baos.toByteArray();
  95. baos.flush();
  96. baos.close();
  97. }
  98. zip.close();
  99. bis.close();
  100. } catch (Exception ex) {
  101. ex.printStackTrace();
  102. }
  103. return b;
  104. }
  105.  
  106. /***
  107. * 压缩BZip2
  108. *
  109. * @param data
  110. * @return
  111. */
  112. public static byte[] bZip2(byte[] data) {
  113. byte[] b = null;
  114. try {
  115. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  116. CBZip2OutputStream bzip2 = new CBZip2OutputStream(bos);
  117. bzip2.write(data);
  118. bzip2.flush();
  119. bzip2.close();
  120. b = bos.toByteArray();
  121. bos.close();
  122. } catch (Exception ex) {
  123. ex.printStackTrace();
  124. }
  125. return b;
  126. }
  127.  
  128. /***
  129. * 解压BZip2
  130. *
  131. * @param data
  132. * @return
  133. */
  134. public static byte[] unBZip2(byte[] data) {
  135. byte[] b = null;
  136. try {
  137. ByteArrayInputStream bis = new ByteArrayInputStream(data);
  138. CBZip2InputStream bzip2 = new CBZip2InputStream(bis);
  139. byte[] buf = new byte[1024];
  140. int num = -1;
  141. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  142. while ((num = bzip2.read(buf, 0, buf.length)) != -1) {
  143. baos.write(buf, 0, num);
  144. }
  145. b = baos.toByteArray();
  146. baos.flush();
  147. baos.close();
  148. bzip2.close();
  149. bis.close();
  150. } catch (Exception ex) {
  151. ex.printStackTrace();
  152. }
  153. return b;
  154. }
  155.  
  156. /**
  157. * 把字节数组转换成16进制字符串
  158. *
  159. * @param bArray
  160. * @return
  161. */
  162. public static String bytesToHexString(byte[] bArray) {
  163. StringBuffer sb = new StringBuffer(bArray.length);
  164. String sTemp;
  165. for (int i = 0; i < bArray.length; i++) {
  166. sTemp = Integer.toHexString(0xFF & bArray[i]);
  167. if (sTemp.length() < 2)
  168. sb.append(0);
  169. sb.append(sTemp.toUpperCase());
  170. }
  171. return sb.toString();
  172. }
  173.  
  174. /**
  175. *jzlib 压缩数据
  176. *
  177. * @param object
  178. * @return
  179. * @throws IOException
  180. */
  181. public static byte[] jzlib(byte[] object) {
  182. byte[] data = null;
  183. try {
  184. ByteArrayOutputStream out = new ByteArrayOutputStream();
  185. ZOutputStream zOut = new ZOutputStream(out,
  186. JZlib.Z_DEFAULT_COMPRESSION);
  187. DataOutputStream objOut = new DataOutputStream(zOut);
  188. objOut.write(object);
  189. objOut.flush();
  190. zOut.close();
  191. data = out.toByteArray();
  192. out.close();
  193. } catch (IOException e) {
  194. e.printStackTrace();
  195. }
  196. return data;
  197. }
  198. /**
  199. *jzLib压缩的数据
  200. *
  201. * @param object
  202. * @return
  203. * @throws IOException
  204. */
  205. public static byte[] unjzlib(byte[] object) {
  206. byte[] data = null;
  207. try {
  208. ByteArrayInputStream in = new ByteArrayInputStream(object);
  209. ZInputStream zIn = new ZInputStream(in);
  210. byte[] buf = new byte[1024];
  211. int num = -1;
  212. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  213. while ((num = zIn.read(buf, 0, buf.length)) != -1) {
  214. baos.write(buf, 0, num);
  215. }
  216. data = baos.toByteArray();
  217. baos.flush();
  218. baos.close();
  219. zIn.close();
  220. in.close();
  221.  
  222. } catch (IOException e) {
  223. e.printStackTrace();
  224. }
  225. return data;
  226. }
  227. public static void main(String[] args) {
  228. String s = "this is a test";
  229.  
  230. byte[] b1 = zip(s.getBytes());
  231. System.out.println("zip:" + bytesToHexString(b1));
  232. byte[] b2 = unZip(b1);
  233. System.out.println("unZip:" + new String(b2));
  234. byte[] b3 = bZip2(s.getBytes());
  235. System.out.println("bZip2:" + bytesToHexString(b3));
  236. byte[] b4 = unBZip2(b3);
  237. System.out.println("unBZip2:" + new String(b4));
  238. byte[] b5 = gZip(s.getBytes());
  239. System.out.println("bZip2:" + bytesToHexString(b5));
  240. byte[] b6 = unGZip(b5);
  241. System.out.println("unBZip2:" + new String(b6));
  242. byte[] b7 = jzlib(s.getBytes());
  243. System.out.println("jzlib:" + bytesToHexString(b7));
  244. byte[] b8 = unjzlib(b7);
  245. System.out.println("unjzlib:" + new String(b8));
  246. }
  247. }

实现二:

  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.util.zip.GZIPInputStream;
  5. import java.util.zip.GZIPOutputStream;
  6.  
  7. // 将一个字符串按照zip方式压缩和解压缩
  8. public class ZipUtil {
  9.  
  10. // 压缩
  11. public static String compress(String str) throws IOException {
  12. if (str == null || str.length() == 0) {
  13. return str;
  14. }
  15. ByteArrayOutputStream out = new ByteArrayOutputStream();
  16. GZIPOutputStream gzip = new GZIPOutputStream(out);
  17. gzip.write(str.getBytes());
  18. gzip.close();
  19. return out.toString("ISO-8859-1");
  20. }
  21.  
  22. // 解压缩
  23. public static String uncompress(String str) throws IOException {
  24. if (str == null || str.length() == 0) {
  25. return str;
  26. }
  27. ByteArrayOutputStream out = new ByteArrayOutputStream();
  28. ByteArrayInputStream in = new ByteArrayInputStream(str
  29. .getBytes("ISO-8859-1"));
  30. GZIPInputStream gunzip = new GZIPInputStream(in);
  31. byte[] buffer = new byte[256];
  32. int n;
  33. while ((n = gunzip.read(buffer)) >= 0) {
  34. out.write(buffer, 0, n);
  35. }
  36. // toString()使用平台默认编码,也可以显式的指定如toString("GBK")
  37. return out.toString();
  38. }
  39.  
  40. // 测试方法
  41. public static void main(String[] args) throws IOException {
  42. System.out.println(ZipUtil.uncompress(ZipUtil.compress("中国China")));
  43. }
  44.  
  45. }

实现三:

  1. import java.io.ByteArrayInputStream;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.IOException;
  4. import java.util.zip.ZipEntry;
  5. import java.util.zip.ZipInputStream;
  6. import java.util.zip.ZipOutputStream;
  7.  
  8. public class StringCompress {
  9. public static final byte[] compress(String paramString) {
  10. if (paramString == null)
  11. return null;
  12. ByteArrayOutputStream byteArrayOutputStream = null;
  13. ZipOutputStream zipOutputStream = null;
  14. byte[] arrayOfByte;
  15. try {
  16. byteArrayOutputStream = new ByteArrayOutputStream();
  17. zipOutputStream = new ZipOutputStream(byteArrayOutputStream);
  18. zipOutputStream.putNextEntry(new ZipEntry("0"));
  19. zipOutputStream.write(paramString.getBytes());
  20. zipOutputStream.closeEntry();
  21. arrayOfByte = byteArrayOutputStream.toByteArray();
  22. } catch (IOException localIOException5) {
  23. arrayOfByte = null;
  24. } finally {
  25. if (zipOutputStream != null)
  26. try {
  27. zipOutputStream.close();
  28. } catch (IOException localIOException6) {
  29. }
  30. if (byteArrayOutputStream != null)
  31. try {
  32. byteArrayOutputStream.close();
  33. } catch (IOException localIOException7) {
  34. }
  35. }
  36. return arrayOfByte;
  37. }
  38.  
  39. @SuppressWarnings("unused")
  40. public static final String decompress(byte[] paramArrayOfByte) {
  41. if (paramArrayOfByte == null)
  42. return null;
  43. ByteArrayOutputStream byteArrayOutputStream = null;
  44. ByteArrayInputStream byteArrayInputStream = null;
  45. ZipInputStream zipInputStream = null;
  46. String str;
  47. try {
  48. byteArrayOutputStream = new ByteArrayOutputStream();
  49. byteArrayInputStream = new ByteArrayInputStream(paramArrayOfByte);
  50. zipInputStream = new ZipInputStream(byteArrayInputStream);
  51. ZipEntry localZipEntry = zipInputStream.getNextEntry();
  52. byte[] arrayOfByte = new byte[1024];
  53. int i = -1;
  54. while ((i = zipInputStream.read(arrayOfByte)) != -1)
  55. byteArrayOutputStream.write(arrayOfByte, 0, i);
  56. str = byteArrayOutputStream.toString();
  57. } catch (IOException localIOException7) {
  58. str = null;
  59. } finally {
  60. if (zipInputStream != null)
  61. try {
  62. zipInputStream.close();
  63. } catch (IOException localIOException8) {
  64. }
  65. if (byteArrayInputStream != null)
  66. try {
  67. byteArrayInputStream.close();
  68. } catch (IOException localIOException9) {
  69. }
  70. if (byteArrayOutputStream != null)
  71. try {
  72. byteArrayOutputStream.close();
  73. } catch (IOException localIOException10) {
  74. }
  75. }
  76. return str;
  77. }
  78. }

参考:

https://www.cnblogs.com/dongzhongwei/p/5964758.html(以上内容部分转自此篇文章)

http://www.blogjava.net/fastunit/archive/2008/04/25/195932.html(以上内容部分转自此篇文章)

http://blog.csdn.net/xyw591238/article/details/51720016(以上内容部分转自此篇文章)

Java压缩字符串的方法收集的更多相关文章

  1. Java里字符串split方法

    Java中的split方法以"."切割字符串时,需要转义 String str[] = s.split("\\.");

  2. Java压缩字符串工具类

    StringCompressUtils.java package javax.utils; import java.io.ByteArrayInputStream; import java.io.By ...

  3. Java 压缩字符串

    1.引言 最近在做项目中,平台提供一个http服务给其他系统调用,然后我接收到其他系统的json格式的报文后去解析,然后用拿到的数据去调用corba服务,我再把corba的返回值封装完成json字符串 ...

  4. [Java] - 格式字符串替换方法

    Java 字符串格式替换方法有两种,一种是使用String.format(...),另一种是使用MessageFormat.format(...) 如下: import java.text.Messa ...

  5. Java中字符串替换方法

    replaceAll方法 public String replaceAll(String regex, String replacement) replace方法 public String repl ...

  6. 案例1:写一个压缩字符串的方法,例如aaaabbcxxx,则输出a4b2c1x3。

    public static String zipString(String str){ String result = "";//用于拼接新串的变量 char last = str ...

  7. JavaScript字符串分割方法

    使用split('')方法.此方法与Java的字符串分割方法方法名一样.

  8. Java实现字符串反转的8种方法

    /** * */ package com.wsheng.aggregator.algorithm.string; import java.util.Stack; /** * 8 种字符串反转的方法, ...

  9. paip.截取字符串byLastDot方法总结uapi python java php c# 总结

    paip.截取字符串byLastDot方法总结uapi python java php c# 总结 ========uapi   left_byLastDot   right_byLastDot 目前 ...

随机推荐

  1. webpack 引入 html-webpack-plugin 报错

    配置webpack当中,出现一个问题: 引入html-webpack-plugin 插件报错. 这时需要本地(也就是当前项目下)安装一下webpack就可以解决问题了. 注意:现在是webpack4版 ...

  2. Rem与em的简单理解

    Rem与em的简单理解 Em单位与像素px的转换 所得的像素值 = 当前元素的font-size * em的值 比如:div的font-size:12px 10em等同于120px 12*10 =12 ...

  3. 数学:乘法逆元-拓展GCD

    乘法逆元应用在组合数学取模问题中,这里给出的实现不见得好用 给出拓展GCD算法: 扩展欧几里得算法是指对于两个数a,b 一定能找到x,y(均为整数,但不满足一定是正数) 满足x*a+y*b=gcd(a ...

  4. 汕头市队赛 SRM 07 B 好玩的麻将

    B 好玩的麻将 SRM 07 背景&&描述 天才麻将少女KPM立志要在日麻界闯出一番名堂.     KPM上周又打了n场麻将,又控了分使得自己的排名是1..n的一个排列.     但她 ...

  5. CDQ 学习笔记

    CDQ分治 CDQ(陈丹琦)分治是一种特殊的分治方法. 它只能处理非强制在线的问题. CDQ分治在维护一些动态的凸包.半平面交问题也有一定应用,然而本渣渣并不会. CDQ分治基于时间分治,整体二分基于 ...

  6. 使用腾讯云 GPU 学习深度学习系列之二:Tensorflow 简明原理【转】

    转自:https://www.qcloud.com/community/article/598765?fromSource=gwzcw.117333.117333.117333 这是<使用腾讯云 ...

  7. appium===Python+Appium环境部署教程

    *前提是你已经安装好python,以及python的pip工具 *安装python请自行百度教程~ 1.安装安卓sdk 安装包:http://tools.android-studio.org/inde ...

  8. JS 判断某变量是否为某数组中的一个值 的几种方法

    1.正则表达式 js 中判断某个元素是否存在于某个 js 数组中,相当于 PHP 语言中的 in_array 函数. }; 用法如下: var arr=new Array([‘b’,2,‘a‘,4]) ...

  9. IE6 下的HTML5兼容问题

    下面列举IE6中10个不得不注意的问题: 1. 使用 DOCTYPE你需要在HTML页面的最顶部加上DOCTYPE类型,当然, strict版是值得推荐的,例如: <!DOCTYPE HTML ...

  10. AngularJS 入门教程

    1. 简介:AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了.所以我做了一些工作(你也可以觉得是 ...