Java Zip String zip(String content)

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

Description

Method zip

License

Open Source License

Parameter

Parameter Description
content String

Exception

Parameter Description
IOException an exception

Return

byte[]

Declaration

public static byte[] zip(String content) throws IOException 

Method Source Code


//package com.java2s;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.DeflaterOutputStream;

public class Main {
    /**/*from  www.  j a va  2s  .  com*/
     * Field BUF_LEN
     */
    private static int BUF_LEN = 1024;

    /**
     * Method zip
     * @param content String
     * @return byte[]
     * @throws IOException
     */
    public static byte[] zip(String content) throws IOException {
        return deflate(content.getBytes());
    }

    /**
     * Method deflate
     * @param input byte[]
     * @return byte[]
     * @throws IOException
     */
    public static byte[] deflate(byte[] input) throws IOException {
        ByteArrayInputStream bis = new ByteArrayInputStream(input);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DeflaterOutputStream dos = new DeflaterOutputStream(bos);
        byte[] buffer = new byte[BUF_LEN];
        int bytesRead = bis.read(buffer);

        while (bytesRead > 0) {
            dos.write(buffer, 0, bytesRead);
            bytesRead = bis.read(buffer);
        }

        dos.close();

        return bos.toByteArray();
    }
}

Related

  1. addStringToZip(String text, String entryName, ZipOutputStream zOut)
  2. generateZip(String kmlContents)
  3. zip(String content)
  4. zip(String str)
  5. zip(String toCompress)
  6. zip_Str(String in_str)