Team, I am trying to compress a (long) string that...
# suitescript
a
Team, I am trying to compress a (long) string that I am passing as a parameter to a Map/Reduce script. Anyone knows how I could accomplish this?
b
how long is it expected to be
a
more than the parameter of type Long Text supports (1000000 chars), that's why I would like to compress/decompress
b
put it in a file instead
a
well, a file would be an option and gzip it
I have tried that, but how do I convert the file into a serialized string?
so that I can unzip it afterwards?
I have tried these functions, but the unzip not working
Copy code
function compressPayloadString(payloadString)
{
    // create temp file uncompressed
    var unsavedTxtFileObj = file.create({
        fileType: file.Type.PLAINTEXT,
        name: 'tempFile.txt',
        contents: payloadString
    });
    // compress file
    var gzippedFile = compress.gzip({
        file: unsavedTxtFileObj,
        level: 9 // max compression
    });
    // get compressed contents
    return gzippedFile.getContents();
}
function unCompressPayloadString(compressedPayloadString)
{
    // create temp file with compressed contents
    var unsavedCompressedFile = file.create({
        fileType: file.Type.GZIP,
        name: 'tempFileCompressed.gzip',
        contents: compressedPayloadString
    });
    // uncompress file
    var gunzippedFile = compress.gunzip({
        file: unsavedCompressedFile
    });
    // return uncompressed contents
    return gunzippedFile.getContents();
}
t
save your file to the file cabinet and you will get its internal id. that is the value that you would pass to your MR script. on get input data load your file to get the string content. on summary stage delete your file
a
thanks @tech_ph2019 that would be an option... maybe I will have to do that
but it would be better not having to save the file
I just want the contents to be compressed/uncompressed
b
dont expect compression to save you
those typically work in binary, and you cant use that in a text field
it forces encoding like base64, which basically makes the content 33% bigger
a
but 33% bigger of the compressed text would be still quite good
do you see anything missing on my functions?
b
whats wrong with it
a
well, if I call them with a simple string, like "test string", I would like to get it compressed, then uncompressed
the when I call the second function I get a encoded string, not the original
const outputObject = "hello there"; const compressedString = compressPayloadString(outputObject); log.debug("compressedString", compressedString); const uncompressedString = unCompressPayloadString(compressedString); log.debug("uncompressedString", uncompressedString);
b
decode the string
a
please tell me how
b
a
yeah, I have looked into that, but don't know what to do...
b
its a way of converting encodings
your string is probably base 64 encoded
so you need to convert it back to the original
a
ok, I can try that
I think that worked
trying now with a bigger string
yes, it works!! thanks a lot @battk
I tested with a file containing 267521 chars
the compressed string returned by the first function has a length of 14156
so I think it does help
for a file almost 10 MB, with 9834000 chars, the compressed string length is 352028
so, much better
here are the functions again working, for reference
Copy code
function compressPayloadString(payloadString)
{
    // create temp file uncompressed
    var unsavedTxtFileObj = file.create({
        fileType: file.Type.PLAINTEXT,
        name: 'tempFile.txt',
        contents: payloadString
    });
    // compress file
    var gzippedFile = compress.gzip({
        file: unsavedTxtFileObj,
        level: 9 // max compression
    });
    // get compressed contents
    return gzippedFile.getContents();
}

function unCompressPayloadString(compressedPayloadString)
{
    // create temp file with compressed contents
    var unsavedCompressedFile = file.create({
        fileType: file.Type.GZIP,
        name: 'tempFileCompressed.gzip',
        contents: compressedPayloadString
    });
    // uncompress file
    var gunzippedFile = compress.gunzip({
        file: unsavedCompressedFile
    });
    // file content is BASE_64 encoded, need to unencode
    var unEncodedString = encode.convert({
        string: gunzippedFile.getContents(),
        inputEncoding: encode.Encoding.BASE_64,
        outputEncoding:  encode.Encoding.UTF_8
    });
    // return uncompressed string
    return unEncodedString;
}
thanks a lot guys!!!