Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Description

Below are a couple of quick methods for zipping and unziping java String values.  Optionally (with true as the second param) the method 'chunks' the resulting  base64 encoded zipped string. Chunking just means long lines are broken at a specific column (line feeds inserted) to make the encoded data more easy to manage (think SSH keys). 

Code

Paste the below into a file "zipString.groovy" and run:

Code Block
titleCalling the script
> groovy zipString.groovy

and now for the code:

Code Block
titleScript Code
import java.util.zip.*


def plaintext      = '''\
- "Why is a raven like a writing-desk?"
  "Have you guessed the riddle yet?" the Hatter said, turning to Alice again.
- "No, I give it up," Alice replied: "What’s the answer?"
- "I haven’t the slightest idea," said the Hatter'''


def zipped         = zipString(plaintext)
def zippedChunked  = zipString(plaintext, true)
def decoded        = unZipString(zipped)
def decodedChunked = unZipString(zippedChunked, true) 

report "plaintext", plaintext
report "zipped", zipped
report "zippedChunked", zippedChunked
report "decoded", decoded
report "decodedChunked", decodedChunked


def report(String title, String data) {
  println title
  println data
  println "---"
}


def unZipString(String stringData, chunked=false) {
  if (chunked) {
    //'unchunk' the string, i.e. remove line feeds
    stringData = stringData.readLines().join("")
  }
  
  // base64 decode and unzip the string
  def zippedByteStream = new ByteArrayInputStream(stringData.decodeBase64())
  new GZIPInputStream(zippedByteStream).text
}


def zipString(String s, chunked=false) {  
  def targetStream = new ByteArrayOutputStream()
  
  // write string to zip stream 
  def zipStream = new GZIPOutputStream(targetStream)  
  zipStream.write(s.bytes)
  zipStream.close()


  // get hold of the zipped content
  def zipped = targetStream.toByteArray()
  targetStream.close()


  // base64 encode the zipped data and optionally chunk it
  StringWriter w = new StringWriter()
  zipped.encodeBase64(chunked).writeTo(w)
  
  w.toString()
}

Executing the script gives the following output: 

Code Block
$ groovy zipString.groovy
plaintext
- "Why is a raven like a writing-desk?"
  "Have you guessed the riddle yet?" the Hatter said, turning to Alice again.
- "No, I give it up," Alice replied: "What’s the answer?"
- "I haven’t the slightest idea," said the Hatter
---
zipped
H4sIAAAAAAAAAE2OMQ7CMAxFd07xlbnlACwVW7uwMkfESq1GaRU7VN24BtfjJLgVA6P/t59fC3cfN7DAo/gnZSSeyIa1sHKObSCZOncCXG81trkiVhKhAB0JhUNIFpN27gh6r0oF4jk00FqyQaAzrokfxo2e8/nUwt3mBgMiG5MVdWncb6XQkpjCZRfz+nm95eD6LCsVM7HbAeOuap0enSSOo5IoOJA30v79z+YLtRhemOUAAAA=
---
zippedChunked
H4sIAAAAAAAAAE2OMQ7CMAxFd07xlbnlACwVW7uwMkfESq1GaRU7VN24BtfjJLgVA6P/t59fC3cf
N7DAo/gnZSSeyIa1sHKObSCZOncCXG81trkiVhKhAB0JhUNIFpN27gh6r0oF4jk00FqyQaAzrokf
xo2e8/nUwt3mBgMiG5MVdWncb6XQkpjCZRfz+nm95eD6LCsVM7HbAeOuap0enSSOo5IoOJA30v79
z+YLtRhemOUAAAA=

---
decoded
- "Why is a raven like a writing-desk?"
  "Have you guessed the riddle yet?" the Hatter said, turning to Alice again.
- "No, I give it up," Alice replied: "What’s the answer?"
- "I haven’t the slightest idea," said the Hatter
---
decodedChunked
- "Why is a raven like a writing-desk?"
  "Have you guessed the riddle yet?" the Hatter said, turning to Alice again.
- "No, I give it up," Alice replied: "What’s the answer?"
- "I haven’t the slightest idea," said the Hatter
---

Author: Matias Bjarland, Iteego Inc