简体   繁体   English

用JavaScript解压缩bzip2数据

[英]Decompressing bzip2 data in Javascript

I ultimately have to consume some data from a Javascript file that looks as follows: 我最终必须从Javascript文件中获取一些数据,如下所示:

Note: The base64 is illustrative only. 注意: base64仅用于说明。

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. GetTripsDataCompressed()返回被推导为使用JSON.NET转换为JSON对象的数组,并将所得串然后压缩到使用的bzip2一个base64SharpCompress与编码所得到的存储器的Base64流。

This is what I have and cannot change it. 这就是我拥有的,无法更改。

I am struggling to find a bzip2 JavaScript implementation that will take the result of: 我正在努力寻找一个将实现以下结果的bzip2 JavaScript实现:

var rawBzip2Data = atob(GetTripsDataCompressed());

and convert rawBzip2Data back into the string that is the JSON array. 并将rawBzip2Data转换回为JSON数组的字符串。 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. 我不能使用诸如compressjs之类的东西,因为我需要支持IE 10,并且因为它使用类型化数组 ,这意味着对IE10的支持已经消失了。

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: 所以看来,最好的选择是https://github.com/antimatter15/bzip2.js,但是因为我还没有创建档案,而只是bzip2一个字符串,所以在执行以下操作后Uncaught No magic number found错误的Uncaught No magic number found

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? 因此,有人可以在这里帮助我使用符合IE 10的脚本从JavaScript解压缩BZip2,Base64编码的字符串吗? Ultimately I don't care whether it uses https://github.com/antimatter15/bzip2.js or some other native JavaScript implementation. 最终,我不在乎它是否使用https://github.com/antimatter15/bzip2.js或其他一些本机JavaScript实现。

It seems to me the answer is in the readme: 在我看来,答案在自述文件中:

decompress(bitstream, size[, len]) does the main decompression of a single block. decompress(bitstream,size [,len])对单个块进行主要的解压缩。 It'll return -1 if it detects that it's the final block, otherwise it returns a string with the decompressed data. 如果检测到它是最后一个块,它将返回-1,否则它将返回包含解压缩数据的字符串。 If you want to cap the output to a certain number of bytes, set the len argument. 如果要将输出限制为一定数量的字节,请设置len参数。

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. 另一方面,Bzip2算法本身是开源的(类似于BSD的许可证),因此您可以自己在Javascript中重新实现它。 It's just a few hundred lines of relatively straight-forward code. 仅有几百行相对简单的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM