Olga Pshenichnikova
Olga Pshenichnikova

Reputation: 1581

NodeJs unzip stream

I need unzip some two level zip:

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

I need just data.txt in file1.1.zip inside file1.zip and I need ignore (entry.autodrain();) other entries.

This is info about 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.

This is the code:

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;
    });

And these are the errors:

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' }

I search much for library which will actually work, I tried tree...
I feel that there is no option in NodeJs to handle zip files...
Can you disprove my belief?

Upvotes: 3

Views: 7286

Answers (1)

Sandip Ghosh
Sandip Ghosh

Reputation: 719

You need to unzip the archive recursively. For that purpose a node module called "runzip" is already available. Github link: https://github.com/dherman/runzip

or, you can write a recursive function on our own, adding a check on filename

Upvotes: 2

Related Questions