1

I would like to read a pptx file in javascript, so I would like to unzip it and read the content in memory. I don't want to store the file first on a server. I want to choose a file with a input type file and just use the file of the input-element and read it binary or something like that.

I found a lot of libraries to unzip zip-files from url, I tried to look at the code but I couldn't figure it out to use it for a blob or byte array.

I can read some stuff like the things described here: http://en.wikipedia.org/wiki/ZIP_%28file_format%29#File_headers

But I don't know how deflating works on byte- or bit-level.

YentheO
  • 307
  • 4
  • 13

1 Answers1

1

(You've said you want to use an input element, so I'm guessing this is browser-based JavaScript.)

Your first step will be to use the File API to read the file as a binary string. See my answer to this other question for an example of that. Then you'll need to find a library. A quick search discovered this one that implements both inflate and deflate. (I don't have personal experience using it, just found it in an answer to this other question.)

Naturally this will only work on quite modern browsers that support the File API. Otherwise, you have no option but to push the file to a server and do the work there, since you can't access the content of the file in the browser without the File API.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • But if I read the zip-file as a binary string the file headers will be in that string and those don't need to be deflated. – YentheO Jan 03 '12 at 14:49
  • @user709455: You'll have to work through the details of the pptx format. There's no shortcut barring your finding a client-side JavaScript library that's already been built that understands pptx files. – T.J. Crowder Jan 03 '12 at 15:02
  • but a pptx file is just a zip file... i only need to extract it and read the file that are inside – YentheO Jan 03 '12 at 15:04
  • @user709455: Then you need something that understands the zip file format, not just the `deflate` algorithm. [Here's a question and answer](http://stackoverflow.com/questions/2163857/unzipping-zip-archives-with-javascript-in-firefox-3-6) here on SO that talks about doing that. – T.J. Crowder Jan 03 '12 at 15:13
  • well, yes, that's the problem... I only find libraries that use a url to a file on a server... And as explained above I can't work out how they work with the binarystream they get from the XMLHttpRequest – YentheO Jan 03 '12 at 15:22
  • @user709455: If you look inside the ZipFile.complete.js file in the library I pointed to, you'll see that it's just reading the full text of the response and then wrapping a convenience object it's called a `BinaryUrlStream` around it. In your case, you can read the text directly rather than having to do the ajax request. Shouldn't be that big a deal to do what it does in its `readyState == 4` logic. Barring finding a library that already does what you want, there's nothing for it but to do the coding -- or farm it out, a lot of us here are consultants. ;-) – T.J. Crowder Jan 03 '12 at 15:30