Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

example request #65

Closed
quasiperfect opened this issue Jun 29, 2018 · 1 comment
Closed

example request #65

quasiperfect opened this issue Jun 29, 2018 · 1 comment

Comments

@quasiperfect
Copy link

hi

can you please add a example on how to extract a specific folder ?
thanks in advance for your help

download('urltofile').pipe(unzipper.Parse()).on('entry', function (entry)
{
	var fileName = entry.path;
	var type = entry.type;
	var size = entry.size;

	if (type === 'Directory' && /src\/assets\/scss/i.test(fileName))
	{
	    //extract this folder
	}
	else
	{
	    entry.autodrain();
	}
});
@ZJONSSON
Copy link
Owner

Every entry has the full path of the filename in the entry.path. So you can pick your files with something like this:

var Writer = require('fstream').Writer;

download('urltofile').pipe(unzipper.Parse()).on('entry', function (entry) {
  // filename will be undefined if it doesn't match the regex
  var filename = /src\/assets\/scss\/(.*)/i.exec(entry.path);
  if (type != 'Directory' && filename) {
    entry.pipe(writer({path: filename[1]}));
  } else {
    entry.autodrain();
  }
});

If the incoming zip files are untrusted you also might want to protect against extracting files above the target directory by adding a check like this:

    // to avoid zip slip (writing outside of the destination), we resolve
    // the target path, and make sure it's nested in the intended
    // destination, or not extract it otherwise.
    var extractPath = path.join(opts.path, entry.path);
    if (extractPath.indexOf(opts.path) != 0) {
      entry.autodrain();
      return;
    }

Please reopen if this doesn't work or if you have any questions/comments!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants