Last active
August 29, 2015 13:56
-
-
Save lyuehh/9320384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var iconv = require('iconv-lite'); | |
| function readGBKText(pathname) { | |
| var bin = fs.readFileSync(pathname); | |
| return iconv.decode(bin, 'gbk'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function readText(pathname) { | |
| var bin = fs.readFileSync(pathname); | |
| if (bin[0] === 0xEF && bin[1] === 0xBB && bin[2] === 0xBF) { | |
| bin = bin.slice(3); | |
| } | |
| return bin.toString('utf-8'); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var rs = fs.createReadStream(src); | |
| var ws = fs.createWriteStream(dst); | |
| rs.on('data', function (chunk) { | |
| if (ws.write(chunk) === false) { | |
| rs.pause(); | |
| } | |
| }); | |
| rs.on('end', function () { | |
| ws.end(); | |
| }); | |
| ws.on('drain', function () { | |
| rs.resume(); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function travel(dir, callback, finish) { | |
| fs.readdir(dir, function (err, files) { | |
| (function next(i) { | |
| if (i < files.length) { | |
| var pathname = path.join(dir, files[i]); | |
| fs.stat(pathname, function (err, stats) { | |
| if (stats.isDirectory()) { | |
| travel(pathname, callback, function () { | |
| next(i + 1); | |
| }); | |
| } else { | |
| callback(pathname, function () { | |
| next(i + 1); | |
| }); | |
| } | |
| }); | |
| } else { | |
| finish && finish(); | |
| } | |
| }(0)); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment