Here you can find the source of zip(String content)
Parameter | Description |
---|---|
content | String |
Parameter | Description |
---|---|
IOException | an exception |
public static byte[] zip(String content) throws IOException
//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(); } }