NodeJs解压缩流

时间:2017-12-06 08:28:15

标签: node.js stream zip

我需要解压两个级别的zip:

file1.zip
  - file1.1.zip
    - data.txt
  - file1.2.zip
  - file1.3.zip

我需要file1.zip中的file1..1ip中的data.txt,我需要忽略(entry.autodrain();)其他条目。

这是关于zip的信息:

apshenichnikov@IAS-WS-UX02:/tmp$ file /var/logs-storage/wcm/119/2017-11-01-119-TcVXBh5RPEHTm86y7sOvJc7TjyQcAc6bqZVawcniIPwyJpWlYqCBJ2XNqBj1ikVF.zip
/var/logs-storage/wcm/119/2017-11-01-119-TcVXBh5RPEHTm86y7sOvJc7TjyQcAc6bqZVawcniIPwyJpWlYqCBJ2XNqBj1ikVF.zip: Zip archive data, at least v4.5 to extract


apshenichnikov@IAS-WS-UX02:/tmp$ unzip -t /var/logs-storage/wcm/119/2017-11-01-119-TcVXBh5RPEHTm86y7sOvJc7TjyQcAc6bqZVawcniIPwyJpWlYqCBJ2XNqBj1ikVF.zip
Archive:  /var/logs-storage/wcm/119/2017-11-01-119-TcVXBh5RPEHTm86y7sOvJc7TjyQcAc6bqZVawcniIPwyJpWlYqCBJ2XNqBj1ikVF.zip
    testing: datamining_119_20171101_clickevent.zip   OK
    testing: datamining_119_20171101_impressionvisibility.zip   OK
    testing: datamining_119_20171101_conversion.zip   OK
No errors detected in compressed data of /var/logs-storage/wcm/119/2017-11-01-119-TcVXBh5RPEHTm86y7sOvJc7TjyQcAc6bqZVawcniIPwyJpWlYqCBJ2XNqBj1ikVF.zip.

这是代码:

fs.createReadStream(source)
    .pipe(unzipper.Parse())
    .on('entry', (entry) =>
    {
        if (found || stop)
        {
            entry.autodrain();  
            return;
        }

        var filePath = entry.path;
        var type = entry.type; // 'Directory' or 'File'
        //var size = entry.size; // might be undefined in some archives

        log.info(getMessage(ID, util.format("Entry: %s", filePath)));

        var outPath = path.join(target, filePath);

        if (
            filePath.match(
                new RegExp(
                    util.format(
                        "datamining_\d+_\d+_%s.*\.zip", 
                        type
                    )
                )
            )
        ) {
            var outStream = fs.createWriteStream(outPath);
            entry.pipe(outStream);
            found = true;

            procLogStream.write(util.format(
                "%s Found: %s\r\n", 
                utils.getCurrentDate(),
                filePath
            ));

            outStream.on('finish', () =>
            {
                if (!stop)
                {
                    cb();
                    //tmpdir.removeCallback();
                    console.log('out stream finish');
                }

                stop = true;
            }).on('error', (err) =>
            {
                if (!stop)
                {
                    cb(err);
                    //tmpdir.removeCallback();
                    console.log('out stream error');
                }

                stop = true;
            });
        } else {
            entry.autodrain();
        }
    })
    .on('error', (err) =>
    {
        console.log('zip stream error');
        console.log(err);
        if (!found && !stop) {
            cb(err);
            //tmpdir.removeCallback();
        }

        stop = true;
    })
    .on('finish', () =>
    {
        console.log('zip stream finish');
        if (!found && !stop) {
            cb();
            //tmpdir.removeCallback();
        }

        stop = true;
    });

这些是错误:

info: [utils/worker.js] 5a27a822cb569b2db8bc493d: Entry: datamining_119_20171101_clickevent.zip
zip stream error
Error: invalid signature: 0x83268e0
    at /home/apshenichnikov/NetBeansProjects/lps/node_modules/unzip/lib/parse.js:59:13
    at runCallback (timers.js:666:20)
    at tryOnImmediate (timers.js:639:5)
    at processImmediate [as _immediateCallback] (timers.js:611:5)
error: [utils/worker.js] 5a27a822cb569b2db8bc493d Error: invalid signature: 0x83268e0
error: [utils/worker.js] 5a27a822cb569b2db8bc493d: errored
zip stream error
{ Error: unexpected end of file
    at Zlib._handle.onerror (zlib.js:370:17) errno: -5, code: 'Z_BUF_ERROR' }

我搜索实际工作的库,我尝试了树... 我觉得NodeJs中没有选项来处理zip文件...
你能反驳我的信念吗?

1 个答案:

答案 0 :(得分:2)

您需要递归地解压缩归档文件。为此,名为“ runzip ”的节点模块已经可用。 Github链接:https://github.com/dherman/runzip

或者,您可以自己编写递归函数,添加对文件名的检查