Skip to content

Instantly share code, notes, and snippets.

@welshstew
Created March 5, 2015 04:41
Show Gist options
  • Save welshstew/3d1fbff954f94182477b to your computer and use it in GitHub Desktop.
Save welshstew/3d1fbff954f94182477b to your computer and use it in GitHub Desktop.
groovy script to zip and unzip text/String content
// Simple groovy script to compress / decompress Strings
import java.util.zip.GZIPInputStream
import java.util.zip.GZIPOutputStream
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()
}
def unzip(String compressed){
def inflaterStream = new GZIPInputStream(new ByteArrayInputStream(compressed.decodeBase64()))
def uncompressedStr = inflaterStream.getText('UTF-8')
return uncompressedStr
}
String inString = 'whatever'
String zipString = zip(inString)
String unzippedString = unzip(zipString)
assert(inString == unzippedString)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment