zoukankan      html  css  js  c++  java
  • [Node.js] Gzip + crypto in stream

    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'))
  • 相关阅读:
    [开发笔记usbTOcan]PyUSB访问设备
    spring之web.xml
    SpringMVC中Controller如何将数据返回
    总结
    流的append
    对象,构造方法、类
    多态
    类的多态性
    环境变量
    构造方法和成员方法的区别
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10516660.html
Copyright © 2011-2022 走看看