![[Node.js] Gzip + crypto in stream [Node.js] Gzip + crypto in stream](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
We can using gzip and crypto with stream:
const fs = require('fs')
const zlib = require('zlib')
const file = process.argv[2];
const crypto = require('crypto');
const {Transform} = require('stream'); const progress = new Transform({
transform(chunk, encoding, callback) {
process.stdout.write('.')
callback(null, chunk)
}
}); //crypto + gzip
fs.createReadStream(file)
.pipe(zlib.createGzip())
.pipe(crypto.createCipher('aes192', 'a_secret'))
.pipe(progress)
//.on('data', () => process.stdout.write('.')) // loading / processing
.pipe(fs.createWriteStream(file + '.zz'))
.on('finish', () => console.log('DONE'));
Also unzip it:
// uncrypto + unzip
fs.createReadStream(file)
.pipe(crypto.createCipher('aes192', 'a_secret'))
.pipe(zlib.createGunzip())
.pipe(progress)
.pipe(fs.createWriteStream(file.slice(0, -3)))
.on('finish', () => console.log('DONE'))