Skip to content

Instantly share code, notes, and snippets.

@piaoger
Created July 23, 2015 03:22
Show Gist options
  • Save piaoger/0d591be74f7bf1a8ced4 to your computer and use it in GitHub Desktop.
Save piaoger/0d591be74f7bf1a8ced4 to your computer and use it in GitHub Desktop.
copyfileSync in node.js
function copyFileSync(srcFile, destFile) {
var BUF_LENGTH, buff, bytesRead, fdr, fdw, pos;
BUF_LENGTH = 64 * 1024;
buff = new Buffer(BUF_LENGTH);
fdr = fs.openSync(srcFile, 'r');
fdw = fs.openSync(destFile, 'w');
bytesRead = 1;
pos = 0;
while (bytesRead > 0) {
bytesRead = fs.readSync(fdr, buff, 0, BUF_LENGTH, pos);
fs.writeSync(fdw, buff, 0, bytesRead);
pos += bytesRead;
}
fs.closeSync(fdr);
return fs.closeSync(fdw);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment