剧场模式
首页后端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);
})
11 条回复
官方服务号
随机课程
GraphQL + React + React Router + Apollo 实战教程

GraphQL + React + React Router + Apollo 实战教程

10 个视频1 小时 12 分钟初级

Pro¥ 138.00¥ 110.40

GraphQL已完结新课程

学员(142)
ahatom · 练虚Summer · 元婴mrj · 大罗j921216063 · 道祖叶 · 太乙半颗心ღ · 大乘皮卡丘 · 合体zev91 · 大乘@你的好友 · 元婴caitengfei520 · 元婴招财加菲猫 · 大罗oyhk · 大乘
最新动态
ahatom · 练虚学习到了10:34
Summer · 元婴学习到了10:37
mrj · 大罗学习到了10:32
j921216063 · 道祖学习到了10:37
叶 · 太乙学习到了10:39
统计信息
    学员: 17786
    视频数量: 1049
    帖子数量: 427

© 汕尾市求知科技有限公司 | 粤ICP备19038915号 | 关注我们 | 在线学员:116

Top