Groovy web console

subscribe to the feed Subscribe
to this
site

zipping data

Published 2 months ago by Ram with tags zip
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

String str = "zipping data"

def val = zip(str)


def zip(String s){
     def targetStream = new ByteArrayOutputStream()
     def zipStream = new GZIPOutputStream(targetStream)
     zipStream.write(s.getBytes('UTF-8'))
     zipStream.close()
     def zippedBytes = targetStream.toByteArray()
     targetStream.close()
     return zippedBytes.encodeBase64()
}