bower / decompress-zip

Module that decompresses zip files
MIT License
102 stars 76 forks source link

Strip option causing error #50

Open BigFunger opened 8 years ago

BigFunger commented 8 years ago

When i specify a strip option, I get the following error:

[TypeError: Cannot read property 'path' of undefined]

The code that generates the error is below:

var DecompressZip = require('decompress-zip');

var sourcePath = './My-Archive.zip';
var targetPath = './temp';
var unzipper = new DecompressZip(sourcePath);

unzipper.on('error', function (err) {
    console.log('Caught an error');
    console.log(err);
});

unzipper.on('progress', function (fileIndex, fileCount) {
    console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount);
});

unzipper.extract({
  path: targetPath,
  filter: function (file) {
    return file.type !== 'SymbolicLink';
  },
  strip: 1
});

The My-Archive.zip file I am using can be downloaded from here: https://s3.amazonaws.com/jimtars/My-Archive.zip

The zip file was created using 7zip on Windows 8.1.

jysperm commented 8 years ago

Same error, change DecompressZip.prototype.extractFiles in lib/decompress-zip.js from:

files.forEach(function (file) {

To:

files.filter(function(file) {
  return file;
}).forEach(function (file) {

Will resolve this problem.

darkyen commented 8 years ago

Can this be merged ?

alexcroox commented 8 years ago

+1 for merge, fixed my error

hintdesk commented 7 years ago

Please merge and release new version in npm.

swoopedj commented 6 years ago

+1 for merge, fixed my error as well

boid-com commented 6 years ago

merge this please

pento commented 6 years ago

For folks running into this, the cause is that this map() doesn't return anything if file.type === 'Directory', which leaves the entry in files array as undefined.

51 isn't the correct fix for this, it'd be better to move the return outside of the if() in the map() callback.

You can work around it by adding this filter to your extract() call:

unzipper.extract( {
    path: '/some/path',
    strip: 1,
    filter: ( file ) => file.type !== 'Directory',
} );

This seems to work for me, but it might fail under some circumstances. Off the top of my head, I guess it means that empty directories won't be extracted, and there are probably other side effects.