5

I'm using a websocket to send a Jpeg image in blob form. I've compressed (gzipped) the blob but I'm unable to find a way to decompress it using JavaScript. Does anyone know how?

This is the code I'm using to read the blob and then convert it into a base64 string:

var r = new FileReader();

r.onload = function(){
    draw(btoa(r.result));
};
r.readAsBinaryString(e.data);

The draw() function basically draws the image using the base64 string, onto a HTML5 canvas.

I've found this library to inflate strings, but it doesn't support blobs. (As far as I know / have tested) If anyone knows of a way, I would appriciate it. :) Thanks!

2 Answers 2

3

I've done a very similar thing for my particle system editor, it has support for reading Cocos2D plist files. In the plist file is texture data, which is a png that has been compressed with gzip and then converted to base64. Here is how I do it:

var input = 'byu9g1RZpINUpVtoKoiKIqJYoris...'; // really long string

// from base64 to ascii binary string
var decodedAsString = atob(input);

// ascii string to bytes in gzipped format
var data = this._binaryStringToArray(decodedAsString);

// raw, uncompressed, binary image data into an array using gzip.js
var uncompressed = require('gzip-js').unzip(data);

// now take the raw uncompressed png and convert it
// back to base64 to use it as a data url
var asString = this._arrayToBinaryString(uncompressed);
var encodedForDataUrl = btoa(asString);

var img = new Image();
img.src = encodedForDataUrl;

I am using gzip.js to do the heavy lifting. You can see all this in action here, you can get some plists to load into it here. BoilingFoam.plist will work, just load it in the "Import/Export" section.

In the above code I call two methods, here they are:

 _binaryStringToArray: function(binaryString) {
        var data = [];

        for (var i = 0; i < binaryString.length; ++i) {
            data[i] = binaryString.charCodeAt(i);
        }

        return data;
    },

    _arrayToBinaryString: function(array) {
        var str = '';
        for (var i = 0; i < array.length; ++i) {
            str += String.fromCharCode(array[i]);
        }
        return str;
    },

The whole shebang is here

4
  • Thanks! I see it uses require() so does that mean I'd need to use node.js? Jan 14, 2013 at 0:24
  • 1
    It's using RequireJS, which is for the browser. gzip.js is challenging to get going. You might have some luck just grabbing my built version of it here: github.com/city41/particle.js/blob/plist/public/javascripts/…, then just do requiregzip('gzip-js').unzip(...) to use it.
    – Matt Greer
    Jan 14, 2013 at 3:50
  • Wow, this works really well! Thanks Matt. Wish I could give you more upvotes. :P Thanks again! Jan 14, 2013 at 13:09
  • Glad it helped! It took me a bit to figure out the workflow, so happy to see it help others too.
    – Matt Greer
    Jan 14, 2013 at 15:28
1

Get a byteArray and you can decode it using pako: http://jsfiddle.net/9yH7M/

1
  • Thanks for the constructive comment about why I am being downvoted.
    – Redsandro
    Aug 3, 2014 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.