AngularJs 后台 纯异步nodejs文件夹(目录)复制

思路:

1、callback 驱动

2、递归所有需要复制文件

3、在一定阀值下并发复制文件

4、运行需要安装 async.js     npm install async

 

代码如下:

Js代码   收藏代码
  1. var async = require("async");  
  2. var fs = require("fs");  
  3. var path = require("path");  
  4. // cursively make dir   
  5. function mkdirs(p, mode, f, made) {  
  6.     if (typeof mode === 'function' || mode === undefined) {  
  7.         f = mode;  
  8.         mode = '0777' & (~process.umask());  
  9.     }  
  10.     if (!made)  
  11.         made = null;  
  12.   
  13.     var cb = f || function () {};  
  14.     if (typeof mode === 'string')  
  15.         mode = parseInt(mode, 8);  
  16.     p = path.resolve(p);  
  17.   
  18.     fs.mkdir(p, mode, function (er) {  
  19.         if (!er) {  
  20.             made = made || p;  
  21.             return cb(null, made);  
  22.         }  
  23.         switch (er.code) {  
  24.         case 'ENOENT':  
  25.             mkdirs(path.dirname(p), mode, function (er, made) {  
  26.                 if (er) {  
  27.                     cb(er, made);  
  28.                 } else {  
  29.                     mkdirs(p, mode, cb, made);  
  30.                 }  
  31.             });  
  32.             break;  
  33.   
  34.             // In the case of any other error, just see if there's a dir  
  35.             // there already.  If so, then hooray!  If not, then something  
  36.             // is borked.  
  37.         default:  
  38.             fs.stat(p, function (er2, stat) {  
  39.                 // if the stat fails, then that's super weird.  
  40.                 // let the original error be the failure reason.  
  41.                 if (er2 || !stat.isDirectory()) {  
  42.                     cb(er, made);  
  43.                 } else {  
  44.                     cb(null, made)  
  45.                 };  
  46.             });  
  47.             break;  
  48.         }  
  49.     });  
  50. }  
  51. // single file copy  
  52. function copyFile(file, toDir, cb) {  
  53.     async.waterfall([  
  54.             function (callback) {  
  55.                 fs.exists(toDir, function (exists) {  
  56.                     if (exists) {  
  57.                         callback(nullfalse);  
  58.                     } else {  
  59.                         callback(nulltrue);  
  60.                     }  
  61.                 });  
  62.             }, function (need, callback) {  
  63.                 if (need) {  
  64.                     mkdirs(path.dirname(toDir), callback);  
  65.                 } else {  
  66.                     callback(nulltrue);  
  67.                 }  
  68.             }, function (p, callback) {  
  69.                 var reads = fs.createReadStream(file);  
  70.                 var writes = fs.createWriteStream(path.join(path.dirname(toDir), path.basename(file)));  
  71.                 reads.pipe(writes);  
  72.                 //don't forget close the  when  all the data are read  
  73.                 reads.on("end"function () {  
  74.                     writes.end();  
  75.                     callback(null);  
  76.                 });  
  77.                 reads.on("error"function (err) {  
  78.                     console.log("error occur in reads");  
  79.                     callback(true, err);  
  80.                 });  
  81.   
  82.             }  
  83.         ], cb);  
  84.   
  85. }  
  86.   
  87. // cursively count the  files that need to be copied  
  88.   
  89. function _ccoutTask(from, to, cbw) {  
  90.     async.waterfall([  
  91.             function (callback) {  
  92.                 fs.stat(from, callback);  
  93.             },  
  94.             function (stats, callback) {  
  95.                 if (stats.isFile()) {  
  96.                     cbw.addFile(from, to);  
  97.                     callback(null, []);  
  98.                 } else if (stats.isDirectory()) {  
  99.                     fs.readdir(from, callback);  
  100.                 }  
  101.             },  
  102.             function (files, callback) {  
  103.                 if (files.length) {  
  104.                     for (var i = 0; i < files.length; i++) {  
  105.                         _ccoutTask(path.join(from, files[i]), path.join(to, files[i]), cbw.increase());  
  106.                     }  
  107.                 }  
  108.                 callback(null);  
  109.             }  
  110.         ], cbw);  
  111.   
  112. }  
  113. // wrap the callback before counting  
  114. function ccoutTask(from, to, cb) {  
  115.     var files = [];  
  116.     var count = 1;  
  117.   
  118.     function wrapper(err) {  
  119.         count--;  
  120.         if (err || count <= 0) {  
  121.             cb(err, files)  
  122.         }  
  123.     }  
  124.     wrapper.increase = function () {  
  125.         count++;  
  126.         return wrapper;  
  127.     }  
  128.     wrapper.addFile = function (file, dir) {  
  129.         files.push({  
  130.             file : file,  
  131.             dir : dir  
  132.         });  
  133.     }  
  134.   
  135.     _ccoutTask(from, to, wrapper);  
  136. }  
  137.   
  138.   
  139. function copyDir(from, to, cb) {  
  140.     if(!cb){  
  141.       cb=function(){};  
  142.     }  
  143.     async.waterfall([  
  144.             function (callback) {  
  145.                 fs.exists(from, function (exists) {  
  146.                     if (exists) {  
  147.                         callback(nulltrue);  
  148.                     } else {  
  149.                         console.log(from + " not exists");  
  150.                         callback(true);  
  151.                     }  
  152.                 });  
  153.             },  
  154.             function (exists, callback) {  
  155.                 fs.stat(from, callback);  
  156.             },  
  157.             function (stats, callback) {  
  158.                 if (stats.isFile()) {  
  159.                     // one file copy  
  160.                     copyFile(from, to, function (err) {  
  161.                         if (err) {  
  162.                             // break the waterfall  
  163.                             callback(true);  
  164.                         } else {  
  165.                             callback(null, []);  
  166.                         }  
  167.                     });  
  168.                 } else if (stats.isDirectory()) {  
  169.                     ccoutTask(from, to, callback);  
  170.                 }  
  171.             },  
  172.             function (files, callback) {      
  173.                 // prevent reaching to max file open limit            
  174.                 async.mapLimit(files, 10, function (f, cb) {  
  175.                     copyFile(f.file, f.dir, cb);  
  176.                 }, callback);  
  177.             }  
  178.         ], cb);  
  179. }  
  180.   
  181. var start = new Date().getTime();  
  182.   //文件夹必须不为空,至少包含一个子文件
  183. copyDir("E:\\zxf""F:\\zxf"function (err) {  
  184.     if (err) {  
  185.         console.log("error ocur");  
  186.         console.dir(err);  
  187.     } else {  
  188.   
  189.         console.log("copy ok");  
  190.         console.log("consume time:" + (new Date().getTime() - start))  
  191.     }  
  192. });  
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值