1

I ultimately have to consume some data from a Javascript file that looks as follows:

Note: The base64 is illustrative only.

function GetTripsDataCompressed() { return 'QlpoOTFBWSZTWdXoWuEDCAgfgBAHf/.....=='; }

GetTripsDataCompressed() returns a base64 string that is derived as an array of objects converted to JSON using JSON.NET and the resulting string then compressed to bzip2 using SharpCompress with the resulting memory stream Base64 encoded.

This is what I have and cannot change it.

I am struggling to find a bzip2 JavaScript implementation that will take the result of:

var rawBzip2Data = atob(GetTripsDataCompressed());

and convert rawBzip2Data back into the string that is the JSON array. I cannot use something like compressjs as I need to support IE 10 and as it uses typed arrays that means IE10 support is out.

So it appears that my best option is https://github.com/antimatter15/bzip2.js however because I have not created an archive and only bzip2 a string it raises an error of Uncaught No magic number found after doing:

var c = GetTripsDataCompressed();
c = atob(c);
var arr = new Uint8Array(c);
var bitstream = bzip2.array(arr);
bzip2.simple(bitstream);

So can anyone help me here to decompress a BZip2, Base64 encoded string from JavaScript using script that is IE 10 compliant? Ultimately I don't care whether it uses https://github.com/antimatter15/bzip2.js or some other native JavaScript implementation.

Sabith
  • 1,628
  • 2
  • 20
  • 38
TheEdge
  • 9,291
  • 15
  • 67
  • 135

1 Answers1

1

It seems to me the answer is in the readme:

decompress(bitstream, size[, len]) does the main decompression of a single block. It'll return -1 if it detects that it's the final block, otherwise it returns a string with the decompressed data. If you want to cap the output to a certain number of bytes, set the len argument.

Also, keep in mind the repository doesn't have a license attached. You'll need to reach out to the author if you want to use the code. That might be tricky given that the repository is eight years old.

On the other hand, the Bzip2 algorithm itself is open-source (BSD-like license), so you can just reimplement it yourself in Javascript. It's just a few hundred lines of relatively straight-forward code.

Luaan
  • 62,244
  • 7
  • 97
  • 116
  • is not related to the original question but your note about the licence, By default, public project in github doesn't have the "open source" status ? If you have any information about this case, please send me :). – Yanis-git Apr 08 '19 at 07:01
  • @Yanis-git Yup, if you don't provide a license on Github, it uses the default copyright laws in the country of origin. Those are rarely permissive :) It's described on Github's help page - https://help.github.com/en/articles/licensing-a-repository. – Luaan Apr 08 '19 at 07:12