GzipUtil.java 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package com.jiayue.ipfcst.console.util;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.apache.commons.codec.binary.Base64OutputStream;
  4. import java.io.ByteArrayOutputStream;
  5. import java.io.IOException;
  6. import java.nio.charset.Charset;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.zip.GZIPOutputStream;
  10. /**
  11. * @author wll
  12. * @date 2020-04-20 10:55:39
  13. */
  14. @Slf4j
  15. public class GzipUtil {
  16. private static int packSize = 800;
  17. public static List<String> zipList(String result) throws IOException {
  18. return zipList(result, packSize);
  19. }
  20. private static List<String> zipList(String result, int packSize) throws IOException {
  21. String zipResult = zip(result);
  22. zipResult = zipResult.replace("\r\n", "");
  23. double length = zipResult.length();
  24. int packageNumber = (int) Math.ceil(length / packSize);
  25. List<String> list = new ArrayList<String>();
  26. for (int i = 0; i < packageNumber; i++) {
  27. if (i == packageNumber - 1) {
  28. list.add(zipResult.substring(i * packSize, (int) length));
  29. } else if (i == 0) {
  30. list.add(zipResult.substring(0, packSize));
  31. } else {
  32. list.add(zipResult.substring(i * packSize, (i + 1) * packSize));
  33. }
  34. }
  35. return list;
  36. }
  37. public static String zip(String result) throws IOException {
  38. log.debug("qrCode默认编码:"+Charset.defaultCharset().name());
  39. log.debug("file.encoding:"+System.getProperty("file.encoding"));
  40. byte[] bytes = result.getBytes("utf-8");
  41. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  42. Base64OutputStream b64os = new Base64OutputStream(bos);
  43. GZIPOutputStream gout = new GZIPOutputStream(b64os);
  44. gout.write(bytes);
  45. gout.close();
  46. b64os.close();
  47. byte b1[] = bos.toByteArray();
  48. bos.close();
  49. return new String(b1, "utf-8");
  50. }
  51. }