为什么Array的Blob小于Uint8Array的Blob? [英] Why is Blob of Array smaller than Blob of Uint8Array?

查看:248
本文介绍了为什么Array的Blob小于Uint8Array的Blob?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 FileReader.readAsArrayBuffer 读取文件,然后执行以下操作:

I read a file using FileReader.readAsArrayBuffer and then do something like this:

  var compressedData = pako.gzip(new Uint8Array(this.result));
  var blob1 = new Blob([compressedData]); // size = 1455338 bytes
  var blob2 = new Blob(compressedData);   // size = 3761329 bytes

例如:如果结果有4194304字节,压缩后它将是大小1455338字节。但由于某种原因,Uint8Array需要包装在一个数组中。这是为什么?

As an example: if result has 4194304 bytes, after compression it will be size 1455338 bytes. But for some reason the Uint8Array needs to be wrapped in an Array. Why is this?

推荐答案

Cf。 BLOB构造函数的文档:

Cf. documentation for BLOB constructor:

https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob


[第一个参数]是一个ArrayBuffer,ArrayBufferView,Blob,DOMString对象的数组,或任何这些对象的混合,将放在Blob中。 DOMStrings编码为UTF-8。

[the first argument] is an Array of ArrayBuffer, ArrayBufferView, Blob, DOMString objects, or a mix of any of such objects, that will be put inside the Blob. DOMStrings are encoded as UTF-8.

我不确定它是如何工作的,但基本上构造函数需要一个数组它将打包到BLOB中的东西。所以,在第一种情况下,你构造一个单独部分的BLOB(即你的ArrayBuffer),而在第二种情况下,你构建它是从1455338个部分(即每个字节分开)。

I'm not sure how it works under the hood, but basically the constructor expects an array of things it will pack into the BLOB. So, in the first case, you're constructing a BLOB of a single part (i.e. your ArrayBuffer), whereas in the second you're constructing it from 1455338 parts (i.e. each byte separately).

由于文档说BLOB部分只能是数组或字符串,它可能最终将ArrayBuffer中的每个字节值转换为UTF-8字符串,这意味着不是每个数字使用1个字节,它使用每个十进制数1个字节(两个结果大小的比率似乎支持这个,因为单字节值是1-3位长,而较大的BLOB大约是较小的2.5倍)。这不仅浪费,我很确定它也会使你的ZIP无法使用。

Since the documentation says the BLOB parts can only be arrays or strings, it probably ends up converting each of the byte values inside your ArrayBuffer into UTF-8 strings, which means instead of using 1 byte per number, it uses 1 byte per decimal digit (the ratio of the two result sizes seems to support this, since single byte values are 1-3 digits long, and the larger BLOB is about 2.5 times the size of the smaller). Not only is that wasteful, I'm pretty sure it also renders your ZIP unusable.

所以,最重要的是,第一个版本是正确的方法。

So, bottom line is, the first version is the correct way to go.

这篇关于为什么Array的Blob小于Uint8Array的Blob?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆