Here you can find the source of zip(String payload)
Parameter | Description |
---|---|
payload | the payload to zip |
Parameter | Description |
---|---|
UnsupportedEncodingException | The Character Encoding is not supported. |
public static byte[] zip(String payload) throws UnsupportedEncodingException, IOException
//package com.java2s; //License from project: Apache License import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.zip.*; public class Main { public static final String CHARACTER_ENCODING_UTF_8 = "UTF-8"; /**/*from ww w . jav a 2s.c o m*/ * * @param payload the payload to zip * @return the zipped payaload * @throws UnsupportedEncodingException The Character Encoding is not supported. */ public static byte[] zip(String payload) throws UnsupportedEncodingException, IOException { // Encode a String into bytes byte[] data = payload.getBytes(CHARACTER_ENCODING_UTF_8); // Compress the bytes ByteArrayOutputStream output = new ByteArrayOutputStream(); Deflater d = new Deflater(); try (DeflaterOutputStream dout = new DeflaterOutputStream(output, d)) { dout.write(data); } return output.toByteArray(); } }