在Node.js中解压缩受密码保护的文件

问题描述 投票:14回答:2

是否有一个可用来解压缩受密码保护的文件的库(网站让我在下载文件时在文件上放了密码)?有大量的库可以解压缩普通文件,但是我发现没有一个库可以使用密码来解压缩。

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]

然后我尝试通过将最后三行更改为]来做一些简单的事情>

   fs.writeFile('./tmp/this.zip', data, 'binary');
   var newFile = spawn('unzip','-P','ThisIsATest','./tmp/this.zip');
   console.log('Data: ' + data);

但是数据再有用不过是[object Object]。我不知道下一步如何将这个新的未压缩文件转换为工作文件或可读字符串。

我对Node超级陌生,所有其他进程触发的异步/监听器仍然有些混乱,因此,如果其中任何一个都没有道理,我感到抱歉。非常感谢您的帮助!

编辑:

我现在添加了以下代码:

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' ])

这只会将所有文件解压缩到目录中,然后我可以从那里读取它们。我的错误是第二个参数必须是数组。

是否有一个可用来解压缩受密码保护的文件的库(网站让我在下载文件时在文件上放了密码)?有很多库可以解压缩普通文件,但我...

node.js unzip zipfile
2个回答
5
投票

我能够做到这一点,可能不是最好的方法,但至少使用此行它可以工作:


0
投票

我使用unzipper找到了解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.