Groovy web console

subscribe to the feed Subscribe
to this
site

compress

Published 1 month ago by Ram with tags Compress
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.io.ByteArrayOutputStream;
import java.util.zip.Deflater;
import java.io.*;
 
public class MyByteArrayCompress {
 
public byte[] compressByteArray(byte[] bytes){
 
ByteArrayOutputStream baos = null;
Deflater dfl = new Deflater();
dfl.setLevel(Deflater.BEST_COMPRESSION);
dfl.setInput(bytes);
dfl.finish();
baos = new ByteArrayOutputStream();
byte[] tmp = new byte[4*1024];
try{
while(!dfl.finished()){
int size = dfl.deflate(tmp);
baos.write(tmp, 0, size);
}
} catch (Exception ex){
 
} finally {
try{
if(baos != null) baos.close();
} catch(Exception ex){}
}
 
return baos.toByteArray();
}
 
public static void main(String[] a){
 
MyByteArrayCompress mbc = new MyByteArrayCompress();
byte[] content = mbc.compressByteArray("Rama".getBytes());
System.out.println(new String(content));
    
}
}