Generate a password protected ZIP file in node.js

只谈情不闲聊 提交于 2019-12-19 16:51:11

问题


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:

var zip = new require('node-zip')();
zip.file('test.file', 'hello there');
var data = zip.generate({base64:false,compression:'DEFLATE'});

Looking other node modules to create ZIP files, I haven't found any that support password protection.


回答1:


If you work on linux then you can do it with the help of zip (command line utility in most linux distributions). Just include the following in you app.

spawn = require('child_process').spawn;
zip = spawn('zip',['-P', 'password' , 'archive.zip', 'complete path to archive file']);
zip .on('exit', function(code) {
...// Do something with zipfile archive.zip
...// which will be in same location as file/folder given
});

If you want to zip a folder just put another argument '-r', before the folder path instead of file path.

Remember this spawns separate thread, from main process, so it is non blocking. For more info on child_process look here http://nodejs.org/api/child_process.html




回答2:


For anyone who ends up here like I did, I tried several packages in node but ended up using this one: https://www.npmjs.com/package/minizip-asm.js

It supports passwords (using AES) and is really easy to use. I'm surprised it doesn't have that many downloads given that it's the only one I found supporting passwords.




回答3:


I had the same issue and couldn't find the package to do it, so I've written one on my own, as a plugin to archiver package. Pure JS, no external zip software needed.

Here it is - https://www.npmjs.com/package/archiver-zip-encrypted. Supports both legacy Zip 2.0 encryption and AES-256 encryption from WinZip.




回答4:


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.



来源:https://stackoverflow.com/questions/14829782/generate-a-password-protected-zip-file-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!