I need to create a ZIP file in node.js, protected by a password.
I am using \"node-zip\" module, that unfortunately doesn\'t support password protection:
<
The solution I'm using (I don't a better way to do it) is:
var contenido1 = 'contenido super secreto';
var contenido2 = 'otro contenido';
var password = 'pass';
var nombreFichero = 'fichero'
var nodezip = new require('node-zip')();
var fs = require("fs");
nodezip.file('test1.txt', contenido1);
nodezip.file('test2.txt', contenido2);
var data = nodezip.generate({base64:false,compression:'DEFLATE'});
fs.writeFile(nombreFichero + '.zip', data, 'binary');
var exec = require('child_process').exec,
child;
child = exec('unzip ' + nombreFichero + '.zip -d ' + nombreFichero +
' && zip -junk-paths --password ' + password + ' ' + nombreFichero + '-p.zip ' + nombreFichero + '/*' +
' && rm -rf ' + nombreFichero + ' && rm -f ' + nombreFichero + '.zip',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
It generates a temporary zip file (without password), and then with several commands, upzip, zip with password and remove temporary files.