我有超过5MB的数据需要存储在会话存储中。为此,我使用pako来压缩数据。
首先,我们有一个Angular应用程序,它从API接收数据,并将其添加到哈希"cachedLookups":
const stringifiedLookups = JSON.stringify(this.cachedLookups)
const compressedLookups = new TextDecoder().decode(pako.deflate(stringifiedLookups));
sessionStorage.setItem(this.sessionStorageKey, compressedLookups);
然后,我们在同一浏览器窗口中有一个AngularJS应用程序,它从会话存储中检索这些数据:
const compressedLookups = localStorageService.get("cachedLookups");
const compressedLookupsUint8Array = new TextEncoder().encode(compressedLookups);
const stringifiedLookups = pako.inflate(compressedLookupsUint8Array, { to: 'string' });
当我打pako.ampure时,我得到了"错误的标题检查"。我还尝试过充气Raw,在这种情况下,我会得到"无效的存储块长度"。我在这里使用TextEncoder/Decoder,因为试图将Uint8Array直接存储到SessionStorage中会迫使SessionStorage超过其配额,尽管其计算容量低于5MB。我认为这个问题与存储API是关于存储键值字符串对的事实有关。
在对zip文件进行编码/解码时,zip标头似乎会导致错误,因为pako返回的Uint8Array和TextEncoder返回的Uint 8Array的值不相同。
var data = "a";
var deflated = pako.deflate(data)
var textEncoded = new TextDecoder().decode(deflated)
var binary = new TextEncoder().encode(textEncoded)
// Not the same
console.log(deflated)
console.log(binary)
// ERROR
console.log(pako.inflate(binary))
如果你使用deflateRaw,它没有添加zip标头,那么就可以正常工作
var data = "a";
var deflated = pako.deflateRaw(data)
var textEncoded = new TextDecoder().decode(deflated)
var binary = new TextEncoder().encode(textEncoded)
// Same content
console.log(deflated)
console.log(binary)
// SUCCESS
console.log(pako.inflateRaw(binary, {to: 'string'}))