1 22 23 package org.continuent.sequoia.common.stream.encoding; 24 25 import java.io.ByteArrayInputStream ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.IOException ; 28 import java.util.zip.Deflater ; 29 import java.util.zip.DeflaterOutputStream ; 30 import java.util.zip.Inflater ; 31 import java.util.zip.InflaterInputStream ; 32 33 39 public class ZipEncoding 40 { 41 48 public static final byte[] encode(byte[] data) throws IOException 49 { 50 ByteArrayInputStream bais = new ByteArrayInputStream (data); 51 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 52 DeflaterOutputStream zipOutputStream = new DeflaterOutputStream (baos, 54 new Deflater (Deflater.BEST_COMPRESSION, true)); 55 56 byte[] bdata = new byte[1024]; 58 int byteCount; 59 while ((byteCount = bais.read(bdata, 0, 1024)) > -1) 60 { 61 zipOutputStream.write(bdata, 0, byteCount); 62 } 63 zipOutputStream.flush(); 64 zipOutputStream.finish(); 65 zipOutputStream.close(); 66 return baos.toByteArray(); 67 } 68 69 76 public static final byte[] decode(byte[] data) throws IOException 77 { 78 InflaterInputStream input = new InflaterInputStream ( 79 new ByteArrayInputStream (data), new Inflater (true)); 80 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 81 82 byte[] bdata = new byte[1024]; 83 int byteCount; 84 while ((byteCount = input.read(bdata, 0, 1024)) > -1) 85 baos.write(bdata, 0, byteCount); 86 baos.flush(); 87 baos.close(); 88 89 return baos.toByteArray(); 90 } 91 } | Popular Tags |