Generate a password protected ZIP file in node.js

前端 未结 4 1009
长情又很酷
长情又很酷 2021-01-17 16:15

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:

<         


        
4条回答
  •  抹茶落季
    2021-01-17 17:07

    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

提交回复
热议问题