JAVA字符串压缩解压缩方法

Java代码 复制代码  收藏代码
  1. package com.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. // 将一个字符串按照zip方式压缩和解压缩   
  10. public class ZipUtil {   
  11.   
  12.   // 压缩   
  13.   public static String compress(String str) throws IOException {   
  14.     if (str == null || str.length() == 0) {   
  15.       return str;   
  16.     }   
  17.     ByteArrayOutputStream out = new ByteArrayOutputStream();   
  18.     GZIPOutputStream gzip = new GZIPOutputStream(out);   
  19.     gzip.write(str.getBytes());   
  20.     gzip.close();   
  21.     return out.toString("ISO-8859-1");   
  22.   }   
  23.   
  24.   // 解压缩   
  25.   public static String uncompress(String str) throws IOException {   
  26.     if (str == null || str.length() == 0) {   
  27.       return str;   
  28.     }   
  29.     ByteArrayOutputStream out = new ByteArrayOutputStream();   
  30.     ByteArrayInputStream in = new ByteArrayInputStream(str   
  31.         .getBytes("ISO-8859-1"));   
  32.     GZIPInputStream gunzip = new GZIPInputStream(in);   
  33.     byte[] buffer = new byte[256];   
  34.     int n;   
  35.     while ((n = gunzip.read(buffer)) >= 0) {   
  36.       out.write(buffer, 0, n);   
  37.     }   
  38.     // toString()使用平台默认编码,也可以显式的指定如toString("GBK")  
  39.     return out.toString();   
  40.   }   
  41.   
  42.   // 测试方法   
  43.   public static void main(String[] args) throws IOException {   
  44.          
  45.       //测试字符串   
  46.       String str="%5B%7B%22lastUpdateTime%22%3A%222011-10-28+9%3A39%3A41%22%2C%22smsList%22%3A%5B%7B%22liveState%22%3A%221";   
  47.          
  48.       System.out.println("原长度:"+str.length());     
  49.          
  50.       System.out.println("压缩后:"+ZipUtil.compress(str).length());     
  51.          
  52.     System.out.println("解压缩:"+ZipUtil.uncompress(ZipUtil.compress(str)));   
  53.   }   
  54.   
  55. }  

 

 

 

 

 

 

 

  1. /**
  2. * 压缩字符串为 byte[]
  3. * 储存可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)方法
  4. * 保存为字符串
  5. *
  6. * @param str 压缩前的文本
  7. * @return
  8. */
  9. public static final byte[] compress(String str) {
  10. if(str == null)
  11. return null;
  12. byte[] compressed;
  13. ByteArrayOutputStream out = null;
  14. ZipOutputStream zout = null;
  15. try {
  16. out = new ByteArrayOutputStream();
  17. zout = new ZipOutputStream(out);
  18. zout.putNextEntry(new ZipEntry("0"));
  19. zout.write(str.getBytes());
  20. zout.closeEntry();
  21. compressed = out.toByteArray();
  22. } catch(IOException e) {
  23. compressed = null;
  24. } finally {
  25. if(zout != null) {
  26. try{zout.close();} catch(IOException e){}
  27. }
  28. if(out != null) {
  29. try{out.close();} catch(IOException e){}
  30. }
  31. }
  32. return compressed;
  33. }
  34. /**
  35. * 将压缩后的 byte[] 数据解压缩
  36. *
  37. * @param compressed 压缩后的 byte[] 数据
  38. * @return 解压后的字符串
  39. */
  40. public static final String decompress(byte[] compressed) {
  41. if(compressed == null)
  42. return null;
  43. ByteArrayOutputStream out = null;
  44. ByteArrayInputStream in = null;
  45. ZipInputStream zin = null;
  46. String decompressed;
  47. try {
  48. out = new ByteArrayOutputStream();
  49. in = new ByteArrayInputStream(compressed);
  50. zin = new ZipInputStream(in);
  51. ZipEntry entry = zin.getNextEntry();
  52. byte[] buffer = new byte[1024];
  53. int offset = -1;
  54. while((offset = zin.read(buffer)) != -1) {
  55. out.write(buffer, 0, offset);
  56. }
  57. decompressed = out.toString();
  58. } catch (IOException e) {
  59. decompressed = null;
  60. } finally {
  61. if(zin != null) {
  62. try {zin.close();} catch(IOException e) {}
  63. }
  64. if(in != null) {
  65. try {in.close();} catch(IOException e) {}
  66. }
  67. if(out != null) {
  68. try {out.close();} catch(IOException e) {}
  69. }
  70. }
  71. return decompressed;
  72. }

这里需要特别注意的是,如果你想把压缩后的byte[]保存到字符串中,不能直接使用new String(byte)或者byte.toString(),因为这样转换之后容量是增加的。同样的道理,如果是字符串的话,也不能直接使用new String().getBytes()获取byte[]传入到decompress中进行解压缩。
  如果保存压缩后的二进制,可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)将其转换为字符串。同样解压缩的时候首先使用new BASE64Decoder().decodeBuffer 方法将字符串转换为字节,然后解压就可以了。


 

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值