【问题标题】:Unable to inflate SessionStorage data; getting either 'incorrect header check' or 'invalid stored block lengths' depending on the inflation method无法膨胀 SessionStorage 数据;根据通货膨胀方法获得“不正确的标头检查”或“无效的存储块长度”
【发布时间】:2021-09-14 15:23:52
【问题描述】:

我有超过 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.inflate 时,我得到“不正确的标题检查”。我也尝试过 inflateRaw,在这种情况下我得到“无效的存储块长度”。我在这里使用 TextEncoder/Decoder,因为尝试将 Uint8Array 直接存储到 SessionStorage 会强制 SessionStorage 超出其配额,尽管计算的大小低于 5MB。我认为这个问题与 Storage API 是关于存储键值字符串对的事实有关。

【问题讨论】:

  • 更多信息:我们从 SessionStorage 正确检索 TextDecoded 数据,当我们通过 TextEncoder 运行它时,它返回一个与原始压缩数据相同的 Uint8Array。如果有人可以帮助解释为什么我可能会得到“不正确的标题检查”,那就太好了。

标签: javascript session-storage pako


【解决方案1】:

当您对 zip 文件进行编码/解码时,似乎 zip 标头会导致错误,因为 pako 返回的 Uint8Array 和 TextEncoder 返回的值不同。

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'}))

【讨论】:

  • 我首先 deflateRaw 我的数据。然后我对其进行解码并将其存储在 SessionStorage 中。然后我检索我的数据并对其进行编码。最后,我尝试 inflateRaw 编码数据。这是我收到错误“无效的块类型”
  • @ThomasPreston 在您的示例中,您使用 deflate/inflate 而不是 deflateRaw 和 inflateRaw
  • 是的,在原始示例中,我使用的是 inflate/deflate,但在我当前的测试中,我使用的是 inflateRaw/deflateRaw,正如我在之前的评论中所描述的那样。这就是我得到“无效块类型”错误的方式。
  • 可能与您的数据有关,您能否分享一个您的 5mb 数据样本,导致您出现此错误?
猜你喜欢
  • 2019-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-06
  • 2015-02-25
  • 1970-01-01
  • 2019-10-01
相关资源
最近更新 更多