Having Trouble with Utilities.ungzip()

260 views
Skip to first unread message

Stephen Hind

unread,
Oct 7, 2021, 6:59:53 AM10/7/21
to Google Apps Script Community
I'm having trouble with ungzip so I must be missing something obvious - here's my test function:

function testGzip() {
const sampleJSON = '[{"id":"117844470217365366262","name":"Alec Lambert"},{"id":"115506690682157649790","name":"Alex Wilder"}]'
const textBlob = Utilities.newBlob(sampleJSON)
const zipBlob = Utilities.gzip(textBlob)
const b64enc = Utilities.base64Encode(zipBlob.getBytes())
console.log({b64enc})
const zippedBlob = Utilities.base64Decode(b64enc)
console.log({zippedBlob})
const unzippedBlob = Utilities.ungzip(zippedBlob)
console.log({unzippedBlob})
}

I'm getting this error at the moment

Exception: The parameters (number[]) don't match the method signature for Utilities.ungzip.

This means it gzips correctly but fails at the ungzip call. I've tried various things (e.g. putting zippedBloc into another blob before submitting to ungzip) but I haven't found the correct process yet.

Any help would be appreciated.

Tanaike

unread,
Oct 7, 2021, 8:27:02 PM10/7/21
to Google Apps Script Community
I think that in your script, `const unzippedBlob = Utilities.ungzip(zippedBlob)` is required to be modified. The argument of `Utilities.ungzip()` is Blob. But in your script, `zippedBlob` is the byte array. By this, such error occurs. In order to avoid this issue, how about the following modified script?

function testGzip() {
  const sampleJSON = '[{"id":"117844470217365366262","name":"Alec Lambert"},{"id":"115506690682157649790","name":"Alex Wilder"}]'
  const textBlob = Utilities.newBlob(sampleJSON)
  const zipBlob = Utilities.gzip(textBlob)
  const b64enc = Utilities.base64Encode(zipBlob.getBytes())
  console.log({ b64enc })
  const zippedBlob = Utilities.base64Decode(b64enc)
  console.log({ zippedBlob })
  const unzippedBlob = Utilities.ungzip(Utilities.newBlob(zippedBlob, zipBlob.getContentType()));
  console.log(unzippedBlob.getDataAsString())
}

In this modification, I used the mimeType from `zipBlob.getContentType()`. But `application/x-gzip` can also be directly used.


Reply all
Reply to author
Forward
0 new messages