decompress-zip
decompress-zip copied to clipboard
Strip option causing error
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.
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.
Can this be merged ?
+1 for merge, fixed my error
Please merge and release new version in npm.
+1 for merge, fixed my error as well
merge this please
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.