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

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

海外散仙厉飞雨 · 真仙发布于
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 条回复
学员
dechuangchang · 金仙后期lgz · 金仙后期不乱方寸 · 真仙lzcdev · 真仙mengnan579 · 真仙lilili001 · 金仙后期heyengineer · 真仙foreverhot · 真仙修 · 真仙zjacker · 真仙薄情人 · 道祖00Jane · 金仙后期kelly · 大罗samuelshang · 大罗itlaowang520 · 太乙巅峰wangzhe123 · 太乙巅峰mike652638 · 真仙Super-Ps · 金仙后期与UIuoi89898 · 道祖qiaochunmei · 真仙qianduan · 真仙liaokui · 真仙深lin人不知 · 真仙Geassu · 道祖lihoubai · 真仙sishenguodong · 金仙后期zbx7858 · 道祖
最新动态
dechuangchang · 金仙后期学习到了2:12
lgz · 金仙后期学习到了9:08
不乱方寸 · 真仙学习到了10:42
lzcdev · 真仙学习到了6:33
mengnan579 · 真仙学习到了10:49

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

Top