javascript 在 node.js 中生成受密码保护的 ZIP 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14829782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generate a password protected ZIP file in node.js
提问by greuze
I need to create a ZIP file in node.js, protected by a password.
我需要在 node.js 中创建一个受密码保护的 ZIP 文件。
I am using "node-zip" module, that unfortunately doesn't support password protection:
我正在使用“node-zip”模块,不幸的是它不支持密码保护:
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.
查看其他节点模块来创建 ZIP 文件,我还没有找到任何支持密码保护的模块。
回答by user568109
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.
如果您在 linux 上工作,那么您可以在 zip(大多数 linux 发行版中的命令行实用程序)的帮助下完成。只需在您的应用程序中包含以下内容。
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.
如果要压缩文件夹,只需在文件夹路径而不是文件路径之前放置另一个参数“-r”。
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
请记住,这会从主进程中产生单独的线程,因此它是非阻塞的。有关child_process
查看这里的更多信息http://nodejs.org/api/child_process.html
回答by james
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
对于像我一样在这里结束的人,我在 node 中尝试了几个包,但最终使用了这个: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.
它支持密码(使用 AES)并且非常易于使用。我很惊讶它没有那么多下载,因为它是我发现的唯一一个支持密码的下载。
回答by yozh
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 archiverpackage. Pure JS, no external zip software needed.
我遇到了同样的问题,找不到可以执行此操作的包,因此我自己编写了一个,作为存档程序包的插件。纯JS,无需外部压缩软件。
Here it is - https://www.npmjs.com/package/archiver-zip-encrypted. Supports both legacy Zip 2.0 encryption and AES-256 encryption from WinZip.
这是 - https://www.npmjs.com/package/archiver-zip-encrypted。支持 WinZip 的旧式 Zip 2.0 加密和 AES-256 加密。
回答by greuze
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.
它会生成一个临时 zip 文件(无密码),然后使用几个命令,upzip、带密码的 zip 和删除临时文件。