From 3826514ffefd2c5ee548538383404df0f4afca40 Mon Sep 17 00:00:00 2001 From: vilicvane Date: Fri, 27 May 2016 18:24:53 +0800 Subject: [PATCH 1/3] Add strip option --- index.js | 5 ++++- readme.md | 1 + 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 248c721..2f963c9 100644 --- a/index.js +++ b/index.js @@ -66,7 +66,10 @@ module.exports = function (zipPath, opts, cb) { opts.onEntry(entry) } - var dest = path.join(opts.dir, entry.fileName) + var strip = opts.strip || 0 + var filenameParts = entry.fileName.split(path.sep) + var destParts = [opts.dir].concat(filenameParts.slice(strip)) + var dest = path.join.apply(path, destParts) // convert external file attr int into a fs stat mode int var mode = (entry.externalFileAttributes >> 16) & 0xFFFF diff --git a/readme.md b/readme.md index 7b3845e..5025b0a 100644 --- a/readme.md +++ b/readme.md @@ -36,6 +36,7 @@ extract(source, {dir: target}, function (err) { - `defaultDirMode` - integer - Directory Mode (permissions) will default to `493` (octal `0755` in integer) - `defaultFileMode` - integer - File Mode (permissions) will default to `420` (octal `0644` in integer) - `onEntry` - function - if present, will be called with every entry from the zip file. forwarded from the `entry` event from yauzl. +- `strip` - integer - number of leading directory components to remove from extracted files. Default modes are only used if no permissions are set in the zip file. From 1e552d1883e5152c5e62f38ab5f700fe1653c3cd Mon Sep 17 00:00:00 2001 From: vilicvane Date: Fri, 27 May 2016 18:48:05 +0800 Subject: [PATCH 2/3] Fix path.sep with '/'; use splice instead of slice --- index.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 2f963c9..d67978c 100644 --- a/index.js +++ b/index.js @@ -67,8 +67,9 @@ module.exports = function (zipPath, opts, cb) { } var strip = opts.strip || 0 - var filenameParts = entry.fileName.split(path.sep) - var destParts = [opts.dir].concat(filenameParts.slice(strip)) + var filenameParts = entry.fileName.split('/') + filenameParts.splice(0, Math.min(strip, filenameParts.length - 1)) + var destParts = [opts.dir].concat(filenameParts) var dest = path.join.apply(path, destParts) // convert external file attr int into a fs stat mode int From 98eff2ae6e47459f1053dd1d12ee9199ef098388 Mon Sep 17 00:00:00 2001 From: vilicvane Date: Tue, 20 Sep 2016 16:40:42 +0800 Subject: [PATCH 3/3] Use slice instead of splice --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index d67978c..363a454 100644 --- a/index.js +++ b/index.js @@ -68,7 +68,7 @@ module.exports = function (zipPath, opts, cb) { var strip = opts.strip || 0 var filenameParts = entry.fileName.split('/') - filenameParts.splice(0, Math.min(strip, filenameParts.length - 1)) + filenameParts = filenameParts.slice(Math.min(strip, filenameParts.length - 1)) var destParts = [opts.dir].concat(filenameParts) var dest = path.join.apply(path, destParts)