在node.js中生成受密码保护的ZIP文件 [英] Generate a password protected ZIP file in node.js

查看:420
本文介绍了在node.js中生成受密码保护的ZIP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在node.js中创建一个ZIP文件,受密码保护。

I need to create a ZIP file in node.js, protected by a password.

我使用的是node-zip模块,遗憾的是支持密码保护:

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'});

查看其他节点模块以创建ZIP文件,我还没有找到任何支持密码保护。

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

推荐答案

如果您使用的是Linux,那么您可以借助zip(大多数Linux发行版中的命令行实用程序)来完成。只需在您的应用中包含以下内容。

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
});

如果你想压缩文件夹,只需在文件夹路径之前添加另一个参数'-r'文件路径。

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

请记住,这会从主进程生成单独的线程,因此它是非阻塞的。有关 child_process 的更多信息,请访问 http://nodejs.org/api/ child_process.html

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

这篇关于在node.js中生成受密码保护的ZIP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆