Skip to content

Instantly share code, notes, and snippets.

@oucem
Forked from welshstew/groovy-zip.groovy
Created February 14, 2018 17:38
Show Gist options
  • Save oucem/d7cce64d7bf400114d4e58ff68029a75 to your computer and use it in GitHub Desktop.
Save oucem/d7cce64d7bf400114d4e58ff68029a75 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