'Merge multiple binary files in Node.js

I want to merge sliced files into one, but I can't find a way to do so. I tried merge-files but it doesn't work for certain formats. I also tried to make my own but it ended up not working. So... how do I merge multiple (it can go up to thousands) binary files?



Solution 1:[1]

I recently encountered the issue - basically you read all the files as buffers then concat them and write the result:

import * as fse from 'fs-extra';


function mergeExample(filePathsArray,outputFilePath)
{

let bufferArr=[];
filePathsArray.forEach(file=>{
let currentFileBuffer=fse.readFileSync(file);
bufferArr.push(currentFileBuffer);
});
let bufferConcat = Buffer.concat(bufferArr);
fse.writeFileSync(outputFilePath, bufferConcat);
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gabriel H