-
Notifications
You must be signed in to change notification settings - Fork 17
/
example.js
27 lines (20 loc) · 790 Bytes
/
example.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
const fs = require('fs')
const zlib = require('zlib')
const strs = require('stringstream')
const utf8Stream = fs.createReadStream('massiveLogFile.gz')
.pipe(zlib.createGunzip())
.pipe(strs('utf8'))
utf8Stream.pipe(process.stdout)
// Stream from utf8 to hex to base64... Why not, ay.
const hex64Stream = fs.createReadStream('myFile.txt')
.pipe(strs('utf8', 'hex'))
.pipe(strs('hex', 'base64'))
hex64Stream.pipe(process.stdout)
// Deals with base64 correctly by aligning chunks
const stream = fs.createReadStream('myFile.jpg').pipe(strs('base64'))
let base64Str = ''
stream.on('data', data => base64Str += data)
stream.on('end', () => {
console.log('My base64 encoded file is: ' + base64Str)
console.log('Original file size: ' + Buffer.from(base64Str, 'base64').length)
})