Java字符串的压缩与解压缩

 

应用场景

当字符串太长,

需要将字符串值存入数据库时,如果字段长度不够,则会出现插入失败;

或者需要进行Http传输时,由于参数长度过长造成http传输失败等。

字符串压缩与解压方法

方法一:用 Java8中的gzip

 
  1. /**

  2. * 使用gzip压缩字符串

  3. * @param str 要压缩的字符串

  4. * @return

  5. */

  6. public static String compress(String str) {

  7. if (str == null || str.length() == 0) {

  8. return str;

  9. }

  10. ByteArrayOutputStream out = new ByteArrayOutputStream();

  11. GZIPOutputStream gzip = null;

  12. try {

  13. gzip = new GZIPOutputStream(out);

  14. gzip.write(str.getBytes());

  15. } catch (IOException e) {

  16. e.printStackTrace();

  17. } finally {

  18. if (gzip != null) {

  19. try {

  20. gzip.close();

  21. } catch (IOException e) {

  22. e.printStackTrace();

  23. }

  24. }

  25. }

  26. return new sun.misc.BASE64Encoder().encode(out.toByteArray());

  27. }

  28.  
  29. /**

  30. * 使用gzip解压缩

  31. * @param compressedStr 压缩字符串

  32. * @return

  33. */

  34. public static String uncompress(String compressedStr) {

  35. if (compressedStr == null) {

  36. return null;

  37. }

  38.  
  39. ByteArrayOutputStream out = new ByteArrayOutputStream();

  40. ByteArrayInputStream in = null;

  41. GZIPInputStream ginzip = null;

  42. byte[] compressed = null;

  43. String decompressed = null;

  44. try {

  45. compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);

  46. in = new ByteArrayInputStream(compressed);

  47. ginzip = new GZIPInputStream(in);

  48. byte[] buffer = new byte[1024];

  49. int offset = -1;

  50. while ((offset = ginzip.read(buffer)) != -1) {

  51. out.write(buffer, 0, offset);

  52. }

  53. decompressed = out.toString();

  54. } catch (IOException e) {

  55. e.printStackTrace();

  56. } finally {

  57. if (ginzip != null) {

  58. try {

  59. ginzip.close();

  60. } catch (IOException e) {

  61. }

  62. }

  63. if (in != null) {

  64. try {

  65. in.close();

  66. } catch (IOException e) {

  67. }

  68. }

  69. if (out != null) {

  70. try {

  71. out.close();

  72. } catch (IOException e) {

  73. }

  74. }

  75. }

  76. return decompressed;

  77. }

方法二:用org.apache.commons.codec.binary.Base64

 
  1. /**

  2. * 使用org.apache.commons.codec.binary.Base64压缩字符串

  3. * @param str 要压缩的字符串

  4. * @return

  5. */

  6. public static String compress(String str) {

  7. if (str == null || str.length() == 0) {

  8. return str;

  9. }

  10. return Base64.encodeBase64String(str.getBytes());

  11. }

  12.  
  13. /**

  14. * 使用org.apache.commons.codec.binary.Base64解压缩

  15. * @param compressedStr 压缩字符串

  16. * @return

  17. */

  18. public static String uncompress(String compressedStr) {

  19. if (compressedStr == null) {

  20. return null;

  21. }

  22. return Base64.decodeBase64(compressedStr);

  23. }

注意事项

在web项目中,服务器端将加密后的字符串返回给前端,前端再通过ajax请求将加密字符串发送给服务器端处理的时候,在http传输过程中会改变加密字符串的内容,导致服务器解压压缩字符串发生异常:

java.util.zip.ZipException: Not in GZIP format

解决方法:

在字符串压缩之后,将压缩后的字符串BASE64加密,在使用的时候先BASE64解密再解压即可。

猜你喜欢

转载自blog.csdn.net/u011250186/article/details/113384741
今日推荐