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

Possible to stream a files contents directly to a string? #55

Open
newmanw opened this issue Jun 6, 2014 · 2 comments
Open

Possible to stream a files contents directly to a string? #55

newmanw opened this issue Jun 6, 2014 · 2 comments

Comments

@newmanw
Copy link

newmanw commented Jun 6, 2014

One of the files in my zip is a JSON file. I would like to stream in the zip and when I get to this file pull the contents directly into a string rather than write to a file then read from that file.

Is that sort of thing possible?

@linus-amg
Copy link

i would like to know that aswell

@joemaller
Copy link

This code snippet will extract JSON data from a file inside a streamed zip archive without writing any temp files. It's asynchronous and JSON.parse can directly access the buffer, so no string conversion is necessary. Zipped files which aren't 'package.json' are ignored.

var request = require('request');
var through = require('through2');
var unzip = require('unzip');

request('https://github.com/EvanOxfeld/node-unzip/archive/master.zip')
  .pipe(unzip.Parse())
  .on('entry', function(entry) {
    if (entry.path.indexOf('package.json') > -1) {
      entry.pipe(through.obj(function(contents) {
        // contents is a buffer containing the JSON data
        var jsonData = JSON.parse(contents);
        console.log(jsonData);
      }))
    } else {
      entry.autodrain();
    }
  });

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

3 participants