All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hina.sdk.util.GzipUtil Maven / Gradle / Ivy

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);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy