首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >合并大量UInt8Arrays

合并大量UInt8Arrays
EN

Stack Overflow用户
提问于 2020-04-21 06:06:47
回答 1查看 87关注 0票数 0

我这里有一些代码,作为示例,可以使用js-ipfs在浏览器中下载Arch Linux。它目前正在工作。

代码语言:javascript
复制
async function start(event) {

  console.log("Starting IPFS...");
  node = await Ipfs.create();

  for await (const file of node.get('QmQxBX5ZKRY8k6W2UqYTMxhdFTvkmNw8X7GJN3t5UiyBpe')) {
    console.log("Starting");
    var content = [];
    for await (const chunk of file.content) {
      console.log("Gathering");
      content = mergeTypedArrays(content, chunk); // slow
    }
    console.log("Assembling");
    saveFile("arch.iso", "application/octet-stream", content);
    console.log("Done");
  };
}

// https://stackoverflow.com/a/35633935/2700296
function mergeTypedArrays(a, b) {
  // Checks for truthy values on both arrays
  if(!a && !b) throw 'Please specify valid arguments for parameters a and b.';  

  // Checks for truthy values or empty arrays on each argument
  // to avoid the unnecessary construction of a new array and
  // the type comparison
  if(!b || b.length === 0) return a;
  if(!a || a.length === 0) return b;

  // Make sure that both typed arrays are of the same type
  if(Object.prototype.toString.call(a) !== Object.prototype.toString.call(b))
      throw 'The types of the two arguments passed for parameters a and b do not match.';

  var c = new a.constructor(a.length + b.length);
  c.set(a);
  c.set(b, a.length);

  return c;
}

// https://stackoverflow.com/a/36899900/2700296
function saveFile (name, type, data) {
  if (data !== null && navigator.msSaveBlob) {
    return navigator.msSaveBlob(new Blob([data], { type: type }), name);
  }
  var a = document.createElement('a');
  a.style.display = "none";
  var url = window.URL.createObjectURL(new Blob([data], {type: type}));
  a.setAttribute("href", url);
  a.setAttribute("download", name);
  document.body.appendChild(a);
  a.click();
  window.URL.revokeObjectURL(url);
  a.remove();
}

问题是,当前组装UInt8Array以发送到saveFile的方法需要在下载的所有~2600个块上重新创建一个新的UInt8Array,这真的很慢,效率也很低。我尝试过将所有这些块推入一个数组,然后再组合它,但是我不知道如何获取一个~2600个UInt8Arrays的数组,并将它们展平为一个UInt8Array。有人对我有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2020-04-21 10:30:13

想得太多了。最终直接从UInt8Array数组创建了一个blob。

代码语言:javascript
复制
async function start(event) {

  console.log("Starting IPFS...");
  node = await Ipfs.create();

  for await (const file of node.get('QmQxBX5ZKRY8k6W2UqYTMxhdFTvkmNw8X7GJN3t5UiyBpe')) {
    console.log("Starting");
    var content = [];
    for await (const chunk of file.content) {
      console.log("Gathering");
      content.push(chunk);
    }
    console.log("Assembling");
    saveFile(content, "archlinux-2020.04.01-x86_64.iso");
    console.log("Done");
  };

}

// https://stackoverflow.com/a/36899900/2700296
function saveFile(data, fileName) {
  if (data !== null && navigator.msSaveBlob) {
    return navigator.msSaveBlob(new Blob(data, { "type": "application/octet-stream" }), fileName);
  }
  var a = document.createElement('a');
  a.style.display = "none";
  var url = window.URL.createObjectURL(new Blob(data, {type: "application/octet-stream"}));
  a.setAttribute("href", url);
  a.setAttribute("download", fileName);
  document.body.appendChild(a);
  a.click();
  window.URL.revokeObjectURL(url);
  a.remove();
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61332837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档