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

[FeatureRequest] Buffer or UInt8Array for input #99

Open
jedwards1211 opened this issue Mar 7, 2023 · 4 comments
Open

[FeatureRequest] Buffer or UInt8Array for input #99

jedwards1211 opened this issue Mar 7, 2023 · 4 comments

Comments

@jedwards1211
Copy link

Someone was saying in #97 that consuming a stream isn't really feasible because metadata is at the end of the file. But it would be nice to be able to at least read a Buffer, because I'd like to use this to read small zip files sent via HTTP form upload without having to save them to disk.

@gowin20
Copy link

gowin20 commented Sep 13, 2023

@antelle any plans to add Buffer support? I am creating a large zip of images using sharp and need to access them directly without saving to disk.

@simonpai
Copy link

There is a way to work around. The idea is quite simple:

  1. Use StreamZip.setFs() to make it use a custom fs internally.
  2. Use memfs to write a Buffer as a file.

Here's the sample code:

import StreamZip from 'node-stream-zip';
import { createFsFromVolume, Volume } from 'memfs';
import { Union } from 'unionfs';

// setup
const volume = new Volume();
const memFs = createFsFromVolume(volume).promises;
const ufs = new Union();
ufs.use(fs).use(volume);
let memFileIndex = 0;
StreamZip.setFs(ufs);

async function readZipFromBuffer(buffer, callback) {
  const file = `/file_${memFileIndex++}.zip`;
  let zip;
  try {
    await memFs.writeFile(file, buffer);
    zip = new StreamZip.async({ file });
    return await callback(zip);
  } finally {
    if (zip) {
      await zip.close();
    }
    await memFs.unlink(file);
  }
}

// usage
const data = await readZipFromBuffer(buffer, async (zip) => {
  // ... do whatever with zip
  // return ...
});

Some references:

@jedwards1211
Copy link
Author

jedwards1211 commented Jul 22, 2024

That's a pretty drastic workaround...I'd prefer to be able to pass a buffer directly to node-stream-zip. Clever idea though

@simonpai
Copy link

That's a pretty drastic workaround...I'd prefer to be able to pass a buffer directly to node-stream-zip. Clever idea though

Yeah, I hope for the same thing but this project seems to be inactive for a few years now. Alternatively we could've forked the project and make it accept a Buffer, yet I believe an implementation with the same memfs approach would still be the shortest path given how much node-stream-zip is coupled with fs.

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