Java使用GZIP进行压缩和解压缩(GZIPOutputStream,GZIPInputStream)

本文展示了如何使用Java实现GZIP压缩和解压缩。`GZIPUtils`类包含两个静态方法:`compress`用于将字符串压缩为GZIP字节数组,`uncompress`用于对GZIP字节数组进行解压缩。示例代码中提供了详细的使用方法,包括异常处理和日志记录。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用GZIPOutputStream进行GZIP压缩:

 

 
  1. public static byte[] compress(String str, String encoding) {

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

  3. return null;

  4. }

  5. ByteArrayOutputStream out = new ByteArrayOutputStream();

  6. GZIPOutputStream gzip;

  7. try {

  8. gzip = new GZIPOutputStream(out);

  9. gzip.write(str.getBytes(encoding));

  10. gzip.close();

  11. } catch (IOException e) {

  12. ApiLogger.error("gzip compress error.", e);

  13. }

  14. return out.toByteArray();

  15. }


 

 

 

使用GZIPInputStream进行GZIP解压缩:

 

 
  1. public static byte[] uncompress(byte[] bytes) {

  2. if (bytes == null || bytes.length == 0) {

  3. return null;

  4. }

  5. ByteArrayOutputStream out = new ByteArrayOutputStream();

  6. ByteArrayInputStream in = new ByteArrayInputStream(bytes);

  7. try {

  8. GZIPInputStream ungzip = new GZIPInputStream(in);

  9. byte[] buffer = new byte[256];

  10. int n;

  11. while ((n = ungzip.read(buffer)) >= 0) {

  12. out.write(buffer, 0, n);

  13. }

  14. } catch (IOException e) {

  15. ApiLogger.error("gzip uncompress error.", e);

  16. }

  17.  
  18. return out.toByteArray();

  19. }


 

 

 

完整代码:

 

 
  1. package com.weibo.api.proxy.server.util;

  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.  
  9. import org.apache.commons.codec.binary.StringUtils;

  10.  
  11. import cn.sina.api.commons.util.ApiLogger;

  12.  
  13. /**

  14. *

  15. * @author wenqi5

  16. *

  17. */

  18. public class GZIPUtils {

  19.  
  20. public static final String GZIP_ENCODE_UTF_8 = "UTF-8";

  21.  
  22. public static final String GZIP_ENCODE_ISO_8859_1 = "ISO-8859-1";

  23.  
  24. /**

  25. * 字符串压缩为GZIP字节数组

  26. *

  27. * @param str

  28. * @return

  29. */

  30. public static byte[] compress(String str) {

  31. return compress(str, GZIP_ENCODE_UTF_8);

  32. }

  33.  
  34. /**

  35. * 字符串压缩为GZIP字节数组

  36. *

  37. * @param str

  38. * @param encoding

  39. * @return

  40. */

  41. public static byte[] compress(String str, String encoding) {

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

  43. return null;

  44. }

  45. ByteArrayOutputStream out = new ByteArrayOutputStream();

  46. GZIPOutputStream gzip;

  47. try {

  48. gzip = new GZIPOutputStream(out);

  49. gzip.write(str.getBytes(encoding));

  50. gzip.close();

  51. } catch (IOException e) {

  52. ApiLogger.error("gzip compress error.", e);

  53. }

  54. return out.toByteArray();

  55. }

  56.  
  57. /**

  58. * GZIP解压缩

  59. *

  60. * @param bytes

  61. * @return

  62. */

  63. public static byte[] uncompress(byte[] bytes) {

  64. if (bytes == null || bytes.length == 0) {

  65. return null;

  66. }

  67. ByteArrayOutputStream out = new ByteArrayOutputStream();

  68. ByteArrayInputStream in = new ByteArrayInputStream(bytes);

  69. try {

  70. GZIPInputStream ungzip = new GZIPInputStream(in);

  71. byte[] buffer = new byte[256];

  72. int n;

  73. while ((n = ungzip.read(buffer)) >= 0) {

  74. out.write(buffer, 0, n);

  75. }

  76. } catch (IOException e) {

  77. ApiLogger.error("gzip uncompress error.", e);

  78. }

  79.  
  80. return out.toByteArray();

  81. }

  82.  
  83. /**

  84. *

  85. * @param bytes

  86. * @return

  87. */

  88. public static String uncompressToString(byte[] bytes) {

  89. return uncompressToString(bytes, GZIP_ENCODE_UTF_8);

  90. }

  91.  
  92. /**

  93. *

  94. * @param bytes

  95. * @param encoding

  96. * @return

  97. */

  98. public static String uncompressToString(byte[] bytes, String encoding) {

  99. if (bytes == null || bytes.length == 0) {

  100. return null;

  101. }

  102. ByteArrayOutputStream out = new ByteArrayOutputStream();

  103. ByteArrayInputStream in = new ByteArrayInputStream(bytes);

  104. try {

  105. GZIPInputStream ungzip = new GZIPInputStream(in);

  106. byte[] buffer = new byte[256];

  107. int n;

  108. while ((n = ungzip.read(buffer)) >= 0) {

  109. out.write(buffer, 0, n);

  110. }

  111. return out.toString(encoding);

  112. } catch (IOException e) {

  113. ApiLogger.error("gzip uncompress to string error.", e);

  114. }

  115. return null;

  116. }

  117.  
  118. public static void main(String[] args) {

  119. String str =

  120. "%5B%7B%22lastUpdateTime%22%3A%222011-10-28+9%3A39%3A41%22%2C%22smsList%22%3A%5B%7B%22liveState%22%3A%221";

  121. System.out.println("原长度:" + str.length());

  122. System.out.println("压缩后字符串:" + GZIPUtils.compress(str).toString().length());

  123. System.out.println("解压缩后字符串:" + StringUtils.newStringUtf8(GZIPUtils.uncompress(GZIPUtils.compress(str))));

  124. System.out.println("解压缩后字符串:" + GZIPUtils.uncompressToString(GZIPUtils.compress(str)));

  125. }

  126. }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值