Java examples for java.lang:byte Array Compress
Use java gzip to compress input content
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.FileInputStream; import java.io.IOException; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; import java.util.zip.InflaterInputStream; import org.apache.log4j.Logger; public class Main{ public static void main(String[] argv) throws Exception{ byte[] content = new byte[]{34,35,36,37,37,37,67,68,69}; System.out.println(java.util.Arrays.toString(gzip(content))); }/*w ww .j av a2 s. co m*/ /** * Log4j logger */ static Logger logger = Logger.getLogger(CompressUtil.class); /** * Use java gzip to compress input content * * @param content * @return null if IOException occurs */ public static byte[] gzip(byte[] content) { byte[] compressedContent = null; if (content == null) return compressedContent; ByteArrayOutputStream baos = new ByteArrayOutputStream(1024); try { GZIPOutputStream gzipStream = new GZIPOutputStream(baos); gzipStream.write(content, 0, content.length); gzipStream.flush(); gzipStream.finish(); compressedContent = baos.toByteArray(); gzipStream.close(); baos.close(); } catch (IOException e) { logger.warn("gzip error: " + e.getMessage(), e); } return compressedContent; } }