Node.js将大文件拆分为多个部分,并对这些部分进行迭代

Node.js Split large file into parts and iterate over the parts

本文关键字:些部 迭代 个部 js 文件 拆分 Node      更新时间:2023-09-26

Node.JS在linux上运行。测试大于100MB的文件,并将其拆分为100MB的块。我可以使用exec

// executes `split`
child = exec("split -d -b 104857600k  $file_name", function (error, stdout, stderr) {
  sys.print('stdout: ' + stdout);
  sys.print('stderr: ' + stderr);
  if (error !== null) {
    console.log('exec error: ' + error);
  }
});

但是,我如何知道生成了多少个部分,以及如何对它们进行迭代?

您可以启用--verbose在stdout结果文件中打印。

child = exec("split -d -b 104857600k --verbose $file_name", function (error, stdout,stderr) {
  if (!error) {
    // Get only file names from the output 
    var resultFiles = stdout.match(/x('d+)/g);
  } else {
    console.log('exec error: ' + error);
  }
});