Cod*_*aaf 14 unzip zipfile node.js
是否有一个库可以用来解压缩受密码保护的文件(网站下载时我会在文件上输入密码)?有大量的库可以解压缩普通文件,但我找不到任何可以使用密码的文件.
在这里,我找到了一些有用的开始.但我宁愿不使用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...
Run Code Online (Sandbox Code Playgroud)
然后我有点失去了该怎么做.我试着写newFile一个文件,但那只是说[object].
然后我尝试通过将最后3行更改为更简单的操作
fs.writeFile('./tmp/this.zip', data, 'binary');
var newFile = spawn('unzip','-P','ThisIsATest','./tmp/this.zip');
console.log('Data: ' + data);
Run Code Online (Sandbox Code Playgroud)
但数据再也没有用[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 }
)};
};
Run Code Online (Sandbox Code Playgroud)
这看起来非常接近我需要的东西,但是当文件被写入时,所有它都是解压缩的选项列表:
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
Run Code Online (Sandbox Code Playgroud)
我不确定输入是否错误,因为这看起来像是解压缩的错误.或者,如果我只是写错了内容.我原以为它会像通常从控制台那样执行 - 只需添加所有文件,因为它们被提取到目录中.虽然我希望能够从缓冲区中读取所有内容,但是 - 选项似乎没有这样做,因此我会解决刚刚添加到目录中的文件.非常感谢任何有任何建议的人!
我能够让这个工作,但不是最好的方式,但它至少使用这一行:
var newFile = spawn('unzip', [ '-P','ThisIsATestPassword', '-d','./lib/tmp/foo','./lib/mocks/tmp/this.zip' ])
Run Code Online (Sandbox Code Playgroud)
这只是将所有文件解压缩到目录中,然后我就可以从那里读取它们.我的错误是第二个参数必须是一个数组.
我能够使此工作正常运行,虽然可能不是最好的方法,但至少使用此行它可以工作:
var newFile = spawn('unzip', [ '-P','ThisIsATestPassword', '-d','./lib/tmp/foo','./lib/mocks/tmp/this.zip' ])
Run Code Online (Sandbox Code Playgroud)
这只会将所有文件解压缩到目录中,然后我就可以从那里读取它们了。我的错误是第二个参数必须是数组。
我使用unzipper找到了解决方案。
粘贴此博客中的代码
const unzipper = require('unzipper');
(async () => {
try {
const directory = await unzipper.Open.file('path/to/your.zip');
const extracted = await directory.files[0].buffer('PASSWORD');
console.log(extracted.toString()); // This will print the file content
} catch(e) {
console.log(e);
}
})();
Run Code Online (Sandbox Code Playgroud)
正如@codyschaaf在他的回答中提到的,我们可以使用spawn或其他一些,child_process但它们并不总是与操作系统无关。因此,如果我在生产中使用它,我将始终寻求与操作系统无关的解决方案(如果存在)。
希望这可以帮助某人。
| 归档时间: |
|
| 查看次数: |
3949 次 |
| 最近记录: |