decompress-zip icon indicating copy to clipboard operation
decompress-zip copied to clipboard

Strip option causing error

Open BigFunger opened this issue 9 years ago • 7 comments

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.

BigFunger avatar Sep 22 '15 15:09 BigFunger

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.

jysperm avatar Oct 09 '15 08:10 jysperm

Can this be merged ?

darkyen avatar Feb 28 '16 00:02 darkyen

+1 for merge, fixed my error

alexcroox avatar Jul 08 '16 14:07 alexcroox

Please merge and release new version in npm.

hintdesk avatar Nov 25 '16 15:11 hintdesk

+1 for merge, fixed my error as well

swoopedj avatar Nov 08 '17 17:11 swoopedj

merge this please

boid-com avatar Mar 13 '18 00:03 boid-com

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.

pento avatar Jun 02 '18 04:06 pento