jszip icon indicating copy to clipboard operation
jszip copied to clipboard

Read zip file as stream

Open DavideMacchia opened this issue 3 years ago • 2 comments

Is there a way to read a zip file one file by one as a stream? I noticed that JSzip.loadAsync take in input a NodeJS.ReadableStream but it's not clear to me how to use it.

DavideMacchia avatar May 31 '22 09:05 DavideMacchia

using node 14, I'd hope the following would work, but it throws an error

const fs = require('fs');
const JSZip = require('jszip');
// const zip = new JSZip();

const { promisify } = require('util');
const pipeline = promisify(require('stream').pipeline);

(async () => {
  const fp = 'results.zip';
  const rs = fs.createReadStream(fp);
  const ws = fs.createWriteStream('temp');
  // rs.pipe(JSZip.loadAsync).pipe(ws);
  
  await pipeline(
    rs,
    async function* (readStream) {
      return JSZip.loadAsync(readStream)
    },
    ws
  );
  
  let a;
})();

throws

(node:33289) UnhandledPromiseRejectionWarning: Error: JSZip can't accept a stream when loading a zip file.

I guess there could be multiple files so it probably won't be able to pipe into file write stream nicely.

jtara1 avatar Aug 11 '22 19:08 jtara1