Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ declare namespace extract {
defaultDirMode?: number;
defaultFileMode?: number;
onEntry?: (entry: Entry, zipfile: ZipFile) => void;
filter?: (entry: Entry, zipfile: ZipFile) => boolean;
}
}

Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ class Extractor {
})

this.zipfile.on('entry', async entry => {
if (this.opts.filter) {
if (!this.opts.filter(entry, this.zipfile)) {
this.zipfile.readEntry()
return
}
}

/* istanbul ignore if */
if (this.canceled) {
debug('skipping entry', entry.fileName, { cancelled: this.canceled })
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ async function main () {
- `defaultDirMode` - integer - Directory Mode (permissions), defaults to `0o755`
- `defaultFileMode` - integer - File Mode (permissions), defaults to `0o644`
- `onEntry` - function - if present, will be called with `(entry, zipfile)`, entry is every entry from the zip file forwarded from the `entry` event from yauzl. `zipfile` is the `yauzl` instance
- `filter` - function - if present, will be called with `(entry, zipfile)`, entry is every entry from the zip file forwarded from the `entry` event from yauzl. `zipfile` is the `yauzl` instance. If the filter returns `true` for a given file it will be extracted, else it will be skipped. It is possible to change `entry.fileName` in the filter to change the location of output files.

Default modes are only used if no permissions are set in the zip file.

Expand Down
17 changes: 17 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,20 @@ test('extract broken zip', async t => {
message: 'invalid central directory file header signature: 0x2014b00'
})
})

test('filter file output', async t => {
const dirPath = await mkdtemp(t, 'filter-files')
await extract(catsZip, {
dir: dirPath,
filter: (entry, zipFile) => {
if (entry.fileName !== 'a-cat.png') {
return false
}
entry.fileName = 'bored-cat.png'
entry.fileNameLength = entry.fileName.length
return true
}
})
const entries = await fs.readdir(dirPath)
t.deepEqual(entries, ['bored-cat.png'])
})