惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 叶小钗
罗磊的独立博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Help Net Security
Help Net Security
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
S
Schneier on Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
S
Securelist
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
N
News and Events Feed by Topic
AI
AI
T
Tenable Blog
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX - 技术
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 三生石上(FineUI控件)
C
Comments on: Blog
G
GRAHAM CLULEY

博客园 - window07

Hibernate缓存机制 Facebook 的 Scaling Out 经验 软件架构设计 mysql的FLUSH句法(清除缓存) 优化MySQL数据库性能 MySQL系统变量interactive_timeout 与 wait_timeout INSERT … ON DUPLICATE KEY UPDATE Howto change runtime variables without restart MySQL Server MySQL 5.1新特性之事件调度器(Event Scheduler) GROUP_CONCAT()妙用 MYSQL命令行导出XML格式数据 - window07 - 博客园 Mysql参数配置优化说明 MySQL 备份和恢复 error: Failed dependencies: MySQLconflicts flash的socket连接安全策略文件 缓存系统MemCached的Java客户端优化历程 Java和flash通信中数据的zlib压缩与解压缩 反序列化时出现“base-64 字符数组的无效长度”错误提示的解决 关于GC垃圾回收
使用java.util.zip对字符串进行压缩和解压缩
window07 · 2009-08-22 · via 博客园 - window07
  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.  
  13. byte[] compressed;
  14. ByteArrayOutputStream out = null;
  15. ZipOutputStream zout = null;
  16.  
  17. try {
  18. out = new ByteArrayOutputStream();
  19. zout = new ZipOutputStream(out);
  20. zout.putNextEntry(new ZipEntry("0"));
  21. zout.write(str.getBytes());
  22. zout.closeEntry();
  23. compressed = out.toByteArray();
  24. } catch(IOException e) {
  25. compressed = null;
  26. } finally {
  27. if(zout != null) {
  28. try{zout.close();} catch(IOException e){}
  29. }
  30. if(out != null) {
  31. try{out.close();} catch(IOException e){}
  32. }
  33. }
  34.  
  35. return compressed;
  36. }
  37.  
  38. /**
  39. * 将压缩后的 byte[] 数据解压缩
  40. *
  41. * @param compressed 压缩后的 byte[] 数据
  42. * @return 解压后的字符串
  43. */
  44. public static final String decompress(byte[] compressed) {
  45. if(compressed == null)
  46. return null;
  47.  
  48. ByteArrayOutputStream out = null;
  49. ByteArrayInputStream in = null;
  50. ZipInputStream zin = null;
  51. String decompressed;
  52. try {
  53. out = new ByteArrayOutputStream();
  54. in = new ByteArrayInputStream(compressed);
  55. zin = new ZipInputStream(in);
  56. ZipEntry entry = zin.getNextEntry();
  57. byte[] buffer = new byte[1024];
  58. int offset = -1;
  59. while((offset = zin.read(buffer)) != -1) {
  60. out.write(buffer, 0, offset);
  61. }
  62. decompressed = out.toString();
  63. } catch (IOException e) {
  64. decompressed = null;
  65. } finally {
  66. if(zin != null) {
  67. try {zin.close();} catch(IOException e) {}
  68. }
  69. if(in != null) {
  70. try {in.close();} catch(IOException e) {}
  71. }
  72. if(out != null) {
  73. try {out.close();} catch(IOException e) {}
  74. }
  75. }
  76.  
  77. return decompressed;
  78. }

这里需要特别注意的是,如果你想把压缩后的byte[]保存到字符串中,不能直接使用new String(byte)或者byte.toString(),因为这样转换之后容量是增加的。同样的道理,如果是字符串的话,也不能直接使用new String().getBytes()获取byte[]传入到decompress中进行解压缩。
  如果保存压缩后的二进制,可以使用new sun.misc.BASE64Encoder().encodeBuffer(byte[] b)将其转换为字符串。同样解压缩的时候首先使用new BASE64Decoder().decodeBuffer 方法将字符串转换为字节,然后解压就可以了。
  
关于使用 java.util.zip 操作文件和目录,请参考这里:http://www.baidu.com/s?wd=java.util.zip&cl=3