是否有一个库可以用来解压缩受密码保护的文件(网站在下载文件时让我在文件上输入密码)?有很多库可以解压缩普通文件,但我找不到可以用密码解压的库。
Here我找到了一些有用的开胃菜。但我宁愿不使用 child_process
并使用内置的 unix 解压缩功能,但这可能是我最后的手段。我什至会自己对加密代码进行操作,但我什至无法找出如何确定加密类型(这似乎很标准,因为我可以在终端中完成)。
再次,我宁愿不这样做,但恐怕这是我唯一的选择,所以我尝试了以下方法:
var fs = require('fs')
, unzip = require('unzip')
, spawn = require('child_process').spawn
, expand = function(filepath, cb) {
var self = this
, unzipStream = fs.createReadStream(filepath).pipe(unzip.Parse())
, xmlData = '';
unzipStream.on('entry', function (entry) {
var filename = entry.path;
// first zip contains files and one password protected zipfile.
// Here I can just do pipe(unzip.Parse()) again, but then i just get a giant encoded string that I don't know how to handle, so i tried other things bellow.
if(filename.match(/\.zip$/)){
entry.on('data', function (data) {
var funzip = spawn('funzip','-P','ThisIsATestPsswd','-');
var newFile = funzip.stdin.write(data);
// more closing code...
然后我有点不知所措了。我尝试将 newFile
写入文件,但这只是说 [object]
.
然后我尝试通过将最后 3 行更改为来做一些更简单的事情
fs.writeFile('./tmp/this.zip', data, 'binary');
var newFile = spawn('unzip','-P','ThisIsATest','./tmp/this.zip');
console.log('Data: ' + data);
但数据并没有什么用处,只是再次[object Object]
。我不知道下一步该怎么做才能将这个新的解压缩文件放入工作文件或可读字符串。
我是 Node
的 super 新手,所有被其他进程触发的异步/监听器仍然有点令人困惑,所以如果其中任何一个没有意义,我很抱歉。非常感谢您的帮助!
编辑:
我现在添加了以下代码:
var fs = require('fs')
, unzip = require('unzip')
, spawn = require('child_process').spawn
, expand = function(filepath, cb) {
var self = this
, unzipStream = fs.createReadStream(filepath)
.pipe(unzip.Parse())
, xmlData = '';
unzipStream.on('entry', function (entry) {
var filename = entry.path
, type = entry.type // 'Directory' or 'File'
, size = entry.size;
console.log('Filename: ' + filename);
if(filename.match(/\.zip$/)){
entry.on('data', function (data) {
fs.writeFile('./lib/mocks/tmp/this.zip', data, 'binary');
var newFile = spawn('unzip','-P','ThisIsATestPassword', '-','../tmp/this.zip');
newFile.stdout.on('data', function(data){
fs.writeFile('./lib/mocks/tmp/that.txt', data); //This needs to be something different
//The zip file contains an archive of files, so one file name shouldn't work
});
});
} else { //Not a zip so handle differently }
)};
};
这似乎真的很接近我的需要,但是当文件被写入时,它所拥有的只是解压缩的选项列表:
UnZip 5.52 of 28 February 2005, by Info-ZIP. Maintained by C. Spieler. Send
bug reports using http://www.info-zip.org/zip-bug.html; see README for details.
Usage: unzip [-Z] [-opts[modifiers]] file[.zip] [list] [-x xlist] [-d exdir]
Default action is to extract files in list, except those in xlist, to exdir;
file[.zip] may be a wildcard. -Z => ZipInfo mode ("unzip -Z" for usage).
-p extract files to pipe, no messages -l list files (short format)
-f freshen existing files, create none -t test compressed archive data
-u update files, create if necessary -z display archive comment
-x exclude files that follow (in xlist) -d extract files into exdir
modifiers: -q quiet mode (-qq => quieter)
-n never overwrite existing files -a auto-convert any text files
-o overwrite files WITHOUT prompting -aa treat ALL files as text
-j junk paths (do not make directories) -v be verbose/print version info
-C match filenames case-insensitively -L make (some) names lowercase
-X restore UID/GID info -V retain VMS version numbers
-K keep setuid/setgid/tacky permissions -M pipe through "more" pager
Examples (see unzip.txt for more info):
unzip data1 -x joe => extract all files except joe from zipfile data1.zip
unzip -p foo | more => send contents of foo.zip via pipe into program more
unzip -fo foo ReadMe => quietly replace existing ReadMe if archive file newer
我不确定输入是否错误,因为这看起来像是解压缩的错误。或者,如果我只是写了错误的内容。我原以为它会像通常从控制台一样执行 - 只需将所有文件添加到目录中即可。虽然我希望能够从缓冲区中读取所有内容,但 - 选项似乎并没有这样做,所以我会满足于刚刚添加到目录中的文件。非常感谢任何有任何建议的人!
编辑 2
我能够得到这个工作,可能不是最好的方式,但它至少可以使用这条线:
var newFile = spawn('unzip', [ '-P','ThisIsATestPassword', '-d','./lib/tmp/foo','./lib/mocks/tmp/this.zip' ])
这会将所有文件解压缩到目录中,然后我就可以从那里读取它们。我的错误是第二个参数必须是一个数组。
最佳答案
我能够得到这个工作,可能不是最好的方式,但它至少可以使用这条线:
var newFile = spawn('unzip', [ '-P','ThisIsATestPassword', '-d','./lib/tmp/foo','./lib/mocks/tmp/this.zip' ])
这会将所有文件解压缩到目录中,然后我就可以从那里读取它们。我的错误是第二个参数必须是一个数组。
关于node.js - 在 Node.js 中解压缩受密码保护的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23926927/