com.hina.sdk.util.GzipUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of HinaCloudSDK Show documentation
Show all versions of HinaCloudSDK Show documentation
The official Java SDK of Hina Analytics
The newest version!
package com.hina.sdk.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.zip.GZIPOutputStream;
public class GzipUtil {
public static String zip(String str) throws IOException {
byte[] compressed = compress(str);
String base64 = encode(compressed);
return base64;
}
private static byte[] compress(String str) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes(StandardCharsets.UTF_8));
gzip.close();
return out.toByteArray();
}
private static String encode(byte[] bytes) {
return Base64.getEncoder().encodeToString(bytes);
}
}