example.js 814 B

123456789101112131415161718192021222324252627
  1. var fs = require('fs')
  2. var zlib = require('zlib')
  3. var strs = require('stringstream')
  4. var utf8Stream = fs.createReadStream('massiveLogFile.gz')
  5. .pipe(zlib.createGunzip())
  6. .pipe(strs('utf8'))
  7. utf8Stream.pipe(process.stdout)
  8. // Stream from utf8 to hex to base64... Why not, ay.
  9. var hex64Stream = fs.createReadStream('myFile')
  10. .pipe(strs('utf8', 'hex'))
  11. .pipe(strs('hex', 'base64'))
  12. hex64Stream.pipe(process.stdout)
  13. // Deals with base64 correctly by aligning chunks
  14. var stream = fs.createReadStream('myFile').pipe(strs('base64'))
  15. var base64Str = ''
  16. stream.on('data', function(data) { base64Str += data })
  17. stream.on('end', function() {
  18. console.log('My base64 encoded file is: ' + base64Str) // Wouldn't work with setEncoding()
  19. console.log('Original file is: ' + new Buffer(base64Str, 'base64'))
  20. })