Skip to content

Instantly share code, notes, and snippets.

@unixc3t
Created December 9, 2018 15:22
Show Gist options
  • Save unixc3t/d666754155e04015ce6870e65eeef2ba to your computer and use it in GitHub Desktop.
Save unixc3t/d666754155e04015ce6870e65eeef2ba to your computer and use it in GitHub Desktop.
nodejs copy file
const fs = require("fs");
const util = require('util');
const path = require('path');
const source = path.resolve(".", "src/1.mp4")
const target = path.resolve(".", "src/bak.mp4")
let postion = 0;
let list = [];
fs.open(source, "r", function (err, fr) {
function rea() {
let buffer = Buffer.alloc(4096);
fs.read(fr, buffer, 0, 4096, postion, function (err, bytesRead) {
if (bytesRead > 0) {
let temp = buffer.slice(0, bytesRead)
fs.open(target,"a",function(err,fw){
fs.write(fw,temp,0,bytesRead,postion,function(err,byteswrite){
postion += bytesRead
rea()
})
})
}
})
}
rea()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment