0

I've been trying to make a simple copy/paste program in Node JS but can't do it because of a problem. I am able to copy the contents of source file as Buffer and write it to another file but while writing, node js messes with newlines and therefore destination file is not as same as source file. The code I use:

var fs = require('fs')
var lazy = require('lazy')

var readStream = fs.createReadStream("SOURCE_FILE.EXE")
var writeStream = fs.createWriteStream("DESTINATION_FILE.EXE", 'binary', { flags : 'w' })

var write = function(line) {
    writeStream.write(line)
}

new lazy(readStream)
    .lines
    .forEach(function(line) {
        console.log(line) // Buffer of each line of file

        write(line)
    })

I use writing buffer of each line one by one so that it doesn't hang on big files. All data is same in SOURCE_FILE and DESTINATION_FILE except some newlines.

Help is really appreciated. Thanks in advance.

z3ron3
  • 29
  • 5
  • 1
    why are you using `lines`? copying binary file should not be dependant upon a newline character. I don't think lazy is suitable for this. Just use a buffer and write the buffer. – Aritra Chakraborty Aug 05 '18 at 15:14
  • Arita Chakraborty, I've tried copying the buffer directly and writing it to a file and it works but only for smaller files. Doing this on big files crashes the program. So that's why went with lazy which gives me buffer of each line of the file. – z3ron3 Aug 05 '18 at 15:50
  • https://stackoverflow.com/questions/11293857/fastest-way-to-copy-file-in-node-js – Aritra Chakraborty Aug 05 '18 at 16:17

0 Answers0