压缩并设置密码:
const fs = require('fs');
const crypto = require('crypto');
const archiver = require('archiver');
const output = fs.createWriteStream('demo.zip');
const archive = archiver('zip', {
zlib: { level: 9 }
});
output.on('close', function() {
console.log('压缩完成');
});
const password = 'password';
const cipher = crypto.createCipher('aes-256-cbc', password);
archive.directory('target/', false);
archive.pipe(cipher).pipe(output);
archive.finalize();
解压压缩包:
const fs = require('fs');
const crypto = require('crypto');
const archiver = require('archiver');
const inputZipPath = fs.createReadStream('压缩包路径/demo.zip');
const outPutZipPath = fs.createWriteStream('解密压缩包路径/demo.zip');
const password = 'password';
const decipher = crypto.createDecipher('aes-256-cbc', password);
input.pipe(decipher).pipe(output);
output.on('close', function() {
console.log('解压完成!');
});
const unzipper = archiver('zip');
unzipper.on('close', function() {
console.log('解压完成');
});
unzipper.pipe(fs.createWriteStream('export/'));
unzipper.directory('export-decrypted/', false);
unzipper.finalize();