127e956375b480e6b111bff63b3ad695d36082c3.svn-base 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package com.chinacreator.videoalliance.order.util;
  2. import java.io.ByteArrayInputStream;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.UnsupportedEncodingException;
  6. import java.util.zip.GZIPInputStream;
  7. import java.util.zip.GZIPOutputStream;
  8. import com.chinacreator.common.util.Base64;
  9. public class ZipUtils {
  10. /**
  11. *
  12. * 使用gzip进行压缩
  13. */
  14. public static String gzip(String primStr) {
  15. if (primStr == null || primStr.length() == 0) {
  16. return primStr;
  17. }
  18. ByteArrayOutputStream out = new ByteArrayOutputStream();
  19. GZIPOutputStream gzip = null;
  20. try {
  21. gzip = new GZIPOutputStream(out);
  22. gzip.write(primStr.getBytes());
  23. } catch (IOException e) {
  24. e.printStackTrace();
  25. } finally {
  26. if (gzip != null) {
  27. try {
  28. gzip.close();
  29. } catch (IOException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. }
  34. return new sun.misc.BASE64Encoder().encode(out.toByteArray());
  35. }
  36. /**
  37. *
  38. * <p>
  39. * Description:使用gzip进行解压缩
  40. * </p>
  41. *
  42. * @param compressedStr
  43. * @return
  44. */
  45. public static String gunzip(String compressedStr) {
  46. if (compressedStr == null) {
  47. return null;
  48. }
  49. ByteArrayOutputStream out = new ByteArrayOutputStream();
  50. ByteArrayInputStream in = null;
  51. GZIPInputStream ginzip = null;
  52. byte[] compressed = null;
  53. String decompressed = null;
  54. try {
  55. compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedStr);
  56. in = new ByteArrayInputStream(compressed);
  57. ginzip = new GZIPInputStream(in);
  58. byte[] buffer = new byte[1024];
  59. int offset = -1;
  60. while ((offset = ginzip.read(buffer)) != -1) {
  61. out.write(buffer, 0, offset);
  62. }
  63. decompressed = out.toString();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. } finally {
  67. if (ginzip != null) {
  68. try {
  69. ginzip.close();
  70. } catch (IOException e) {
  71. }
  72. }
  73. if (in != null) {
  74. try {
  75. in.close();
  76. } catch (IOException e) {
  77. }
  78. }
  79. if (out != null) {
  80. try {
  81. out.close();
  82. } catch (IOException e) {
  83. }
  84. }
  85. }
  86. return decompressed;
  87. }
  88. public static void main(String[] args) throws IOException {
  89. String loaclbeas = Base64.encodeBase64String("我wo阿斯u安萨地方".getBytes());
  90. System.out.println(loaclbeas);
  91. System.out.println(new String(Base64.decodeBase64(loaclbeas.getBytes()),"UTF-8"));
  92. }
  93. }