Java Zip File zip(String payload)

Here you can find the source of zip(String payload)

Description

zip

License

Apache License

Parameter

Parameter Description
payload the payload to zip

Exception

Parameter Description
UnsupportedEncodingException The Character Encoding is not supported.

Return

the zipped payaload

Declaration

public static byte[] zip(String payload) throws UnsupportedEncodingException, IOException 

Method Source Code

//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();

    }
}

Related

  1. zip(String filesDirToBeZipped, String destFileName, String manifest)
  2. zip(String inputStr)
  3. zip(String outputFileName, String inputFileName)
  4. Zip(String path, File file)
  5. zip(String path, String zipFilePath)
  6. zip(String src, String des)
  7. zip(String src, String dest, PrintStream stream)
  8. zip(String srcPath)
  9. zip(String zipFileName, Map entries)