-
Notifications
You must be signed in to change notification settings - Fork 0
/
stream.js
66 lines (47 loc) · 1.47 KB
/
stream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 读
// var fs = require('fs');
// var data = '';
// var readerStream = fs.createReadStream('input.txt');
// readerStream.setEncoding('utf8');
// readerStream.on('data', function(chunk) {
// data += chunk;
// });
// readerStream.on('end', function() {
// console.log(data);
// console.log('执行完毕');
// });
// readerStream.on('error', function(error) {
// console.log(error);
// });
// 写
// var fs = require('fs');
// var data = 'nodejs111\n222';
// var writerStream = fs.createWriteStream('output.txt');
// writerStream.write(data, 'utf8');
// writerStream.end();
// writerStream.on('finish', function() {
// console.log('执行完毕');
// });
// writerStream.on('error', function(err) {
// console.log(err.stack);
// });
// 管道
// var fs = require('fs');
// var readerStream = fs.createReadStream('input.txt');
// var writerStream = fs.createWriteStream('output.txt');
// readerStream.pipe(writerStream);
// console.log('执行完毕');
var fs = require("fs");
var zlib = require('zlib');
// 压缩 input.txt 文件为 input.txt.gz
fs.createReadStream('input.txt')
.pipe(zlib.createGzip())
.pipe(fs.createWriteStream('input.txt.gz'));
console.log("文件压缩完成。");
// var fs = require("fs");
// var zlib = require('zlib');
// // 解压 input.txt.gz 文件为 input.txt
// fs.createReadStream('input.txt.gz')
// .pipe(zlib.createGunzip())
// .pipe(fs.createWriteStream('input.txt'));
// console.log("文件解压完成。");