1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| package com.ss.task.scheme.utils;
import org.apache.commons.codec.binary.Base64;
import java.io.*; import java.util.Map; import java.util.zip.DeflaterOutputStream; import java.util.zip.InflaterOutputStream;
public class CompressUtils {
private final static String FLOWVOS_KEY = "flowvos"; private final static String FLOWIDS_KEY = "flowids"; private final static String ISCOMPRESSED_KEY = "iscompressed";
public static void compressFlowVos(Map<String, String> data) { compress(data, FLOWVOS_KEY); }
public static void compressFlowIds(Map<String, String> data) { compress(data, FLOWIDS_KEY); }
public static void compress(Map<String, String> data, String key) { if(data == null || data.get(key) == null) { return; } String flowData = data.get(key); flowData = compressData(flowData);
data.put(key, flowData); data.put(ISCOMPRESSED_KEY, "Y"); }
public static String compressData(String data) { try { if(data == null || data.length() == 0) { return data; }
ByteArrayOutputStream bos = new ByteArrayOutputStream(); DeflaterOutputStream zos = new DeflaterOutputStream(bos); zos.write(data.getBytes()); zos.close(); return encodeBase64(bos.toByteArray()); } catch (Exception ex) { return "ZIP_ERR"; } }
public static String decompressData(String encodeData) { try { if(encodeData == null || encodeData.length() == 0) { return encodeData; }
ByteArrayOutputStream bos = new ByteArrayOutputStream(); InflaterOutputStream zos = new InflaterOutputStream(bos); zos.write(decodeBASE64(encodeData)); zos.close(); return bos.toString(); } catch (Exception ex) { return "UNZIP_ERR"; } }
public static String encodeBase64(byte [] b) { if (b == null) { return null; } return new String((new Base64()).encode(b)); }
public static byte[] decodeBASE64(String s) { if (s == null) { return null; } return new Base64().decode(s.getBytes()); } }
|