Ala'a Al Hallaq
Ala'a Al Hallaq

Reputation: 505

Extract Single File From Piped remote Stream to unzip.Parse() using NodeJs without reading the whole stream

I am using NodeJs "torrent-stream" to open a stream to a torrent file then select the target Zip file and pip the target file stream to UnZip stream.

let torrentStream = require("torrent-stream");
let unzip = require("unzip-stream");

let engine = torrentStream(torrentMagentURL);
engine.on("ready", () => {
    //select the target zip file
    let file = engine.files[0]; 
    //open a stream to the remote zip file from torrent connection
    let stream = file.createReadStream();
    //pip the remote file to a Zip reader
    let zip = stream.pipe(unzip.Parse());
    //listen to every entry parsed from the zip file
    zip.on("entry", entry => {
            // the problem is here :: 
            // for the every entry in the zip file 
            // this call back wont be called unless 
            // all the content of the previous entry 
            // in the zip file are downloaded

            // what I really need is to SEEK to the next entry 
            // NOT download the previous entry entirely before 
            // seek to the next one. 
            // because mostly the needed file to be downloaded 
            // from the zip file wont be the first entry.

            // noting that "torrent-stream" support seeking 
            // in its remote stream.
    })
})

Upvotes: 1

Views: 1127

Answers (1)

Chris Burnham
Chris Burnham

Reputation: 109

From the unzip-stream docs, it looks like you can call autodrain() to ignore any entries you aren't using and advance to the next entry. https://www.npmjs.com/package/unzip-stream

zip.on("entry", entry => {
  if (entry.path !== 'path/filename.ext') {
      entry.autodrain();
  } else {
    // Process the entry
  }
})

Upvotes: 1

Related Questions