Advertisement
chava_vm

Cortar con FS (NodeJS)

Apr 8th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const fs = require("fs");
  2. const filename = process.argv[2];
  3. const n = Number(process.argv[3] || 2);
  4.  
  5.  
  6. fs.readFile(filename, function(err, buff){
  7.     if(err){
  8.         console.log("Error al abrir el archivo", filename);
  9.         return;
  10.     }
  11.  
  12.     const size = buff.length;
  13.     const delta = Math.ceil(size/n); //devuelve el entero mรกs pequeรฑo mayor o igual a un nรบmero dado.
  14.  
  15.     var start;
  16.     var end;
  17.  
  18.     for(i=0; i<n; i++){
  19.         start = i * delta;
  20.         end = (i+1) * delta;
  21.         if(end > size){
  22.             end = size;
  23.         }
  24.         fs.writeFileSync(`${filename}.part${i+1}`, buff.slice(start, end));
  25.         console.log(`Escrito ${(100 * end/size).toFixed(1)}%`);
  26.     }
  27.     console.log("Archivo escrito");
  28. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement