jvilk / BrowserFS

BrowserFS is an in-browser filesystem that emulates the Node JS filesystem API and supports storing and retrieving files from various backends.
Other
3.07k stars 218 forks source link

ZipFS Question #83

Closed SpiritedDusty closed 10 years ago

SpiritedDusty commented 10 years ago

In the wiki it says that ZipFS accepts a buffer for zip_data. How do I go about creating a buffer from lets say an xhr request? Theres probably an obvious answer that I'm not seeing but, I can't figure out how to make a buffer.

jvilk commented 10 years ago

Thanks for the question. There are two ways:

  1. Create an XmlHttpRequest file system and use readFile to get the item as a Buffer.
  2. Use the browser's XHR API to get the file as an ArrayBuffer, and then call new Buffer(data) (a DataView works too).
jvilk commented 10 years ago

Note that by default, BrowserFS will only expose Buffer as a global if you run BrowserFS.install(window);.

If you don't want Buffer to be a global, then you can do the following:

// This seems insane, but in node require('buffer').Buffer === Buffer
var Buffer = BrowserFS.BFSRequire('buffer').Buffer;
// where 'data' is the ArrayBuffer containing the zip file's contents
var zipfile = new Buffer(data);
SpiritedDusty commented 10 years ago

Oh I see. I'm using BrowserFS with emsceipten so I didn't have Buffer as a global. That was the thing that was throwing me off. Thanks for the help!

jvilk commented 10 years ago

No problem. Let me know if you have any further questions!