剧场模式
首页后端Nodejs轻松学 nodejs - 基础篇

轻松学 Node.js - 基础篇 #8 流和管道

求知小风 · 真仙发布于

注意

ls | grep app 这个命令在 linux 或 mac 才适合,或者 windows 的 git bash 也可以的。

如果是 windows 的命令提示符,对应的查找文件的命令应该是: dir | findstr app

// 解压
var crypto = require('crypto');
var fs = require('fs');
var zlib = require('zlib');

var password = new Buffer(process.env.PASS || 'password');
var decryptStream = crypto.createDecipher('aes-256-cbc', password);

var gzip = zlib.createGunzip();
var readStream = fs.createReadStream(__dirname + '/out.gz');

readStream // reads current file
    .pipe(gzip) // uncompresses
    .pipe(decryptStream) // decrypts
    .pipe(process.stdout) // writes to terminal
    .on('finish', function() { // finished
        console.log('done');
    });

// 压缩
var crypto = require('crypto');
var fs = require('fs');
var zlib = require('zlib');

var password = new Buffer(process.env.PASS || 'password');
var encryptStream = crypto.createCipher('aes-256-cbc', password);

var gzip = zlib.createGzip();
var readStream = fs.createReadStream(__dirname + "/readMe.txt"); // current file
var writeStream = fs.createWriteStream(__dirname + '/out.gz');

readStream // reads current file
    .pipe(encryptStream) // encrypts
    .pipe(gzip) // compresses
    .pipe(writeStream) // writes to out file
    .on('finish', function() { // all done
        console.log('done');
    });


// 流和管道
var fs = require('fs');

var myReadStream = fs.createReadStream(__dirname + '/readMe.txt');
var myWriteStream = fs.createWriteStream(__dirname + '/writeMe.txt');

myReadStream.pipe(myWriteStream);

var writeData = "hello world";
myWriteStream.write(writeData);
myWriteStream.end();
myWriteStream.on('finish', function() {
    console.log('finished');
})

myReadStream.setEncoding('utf8');

var data = ""

myReadStream.on('data', function(chunk) {
    // data += chunk;
    myWriteStream.write(chunk);
})

myReadStream.on('end', function() {
    // console.log(data);
})
10 条回复
加微信(qiuzhi99666)入群官方服务号
随机课程
Redux 入门教程(React 进阶)

Redux 入门教程(React 进阶)

20 个视频2 小时 18 分钟初级

Pro¥ 129.00¥ 103.20

Redux已完结

学员(115)
张琳 · 道祖zsh19950920 · 真仙vaveee · 道祖陈太太先生 · 太乙金陵小牛 · 元婴hanruiying · 道祖midoriya · 元婴WebWangFei · 元婴巴拉拉魔法叮 · 化神帅神orz · 大乘YQP · 元婴Cen Zhilin · 大乘
最新动态
张琳 · 道祖学习到了10:25
zsh19950920 · 真仙学习到了4:17
vaveee · 道祖学习到了10:49
陈太太先生 · 太乙学习到了10:49
金陵小牛 · 元婴学习到了10:31
统计信息
    学员: 15780
    视频数量: 918
    帖子数量: 406

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 在线学员:146

Top