Created
March 5, 2015 04:41
-
-
Save welshstew/3d1fbff954f94182477b to your computer and use it in GitHub Desktop.
groovy script to zip and unzip text/String content
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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