Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate a password protected ZIP file in node.js

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.

like image 750
greuze Avatar asked Feb 12 '13 09:02

greuze


People also ask

How do I create a password protected zip file?

Right-click on the Zip file you wish to password protect. Choose WinZip. Click Encrypt. WinZip will ask for a password and then encrypt all files currently in the Zip file.

Can we password protect a zip file?

Can you put a password on a zip file? Windows doesn't have an option to protect your zipped file with a password, so there's no other way but to use third-party tools. You can choose from various trusted software options, such as WinZip, WinRAR, and 7-Zip.

How do I password protect a zip file in Unix?

Create a Password-Protected ZIP File in Linux Using GUIStep 1: Go to the file location and right-click on the file. Step 2: Then click on the compress option. Step 3: Then click on the other option and set your password and click on Create option.


1 Answers

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

like image 83
user568109 Avatar answered Sep 19 '22 08:09

user568109