2 title: "How to read a file"
7 This page explains how to read an existing zip file or add a existing file into
15 Getting binary data with an ajax request is hard (mainly because of IE <= 9).
16 The easy way is to use [JSZipUtils.getBinaryContent](https://github.com/stuk/jszip-utils).
17 With JSZipUtils.getBinaryContent, you can do the following (see the
18 documentation for more examples) :
21 JSZipUtils.getBinaryContent('path/to/content.zip', function(err, data) {
23 throw err; // or handle err
26 var zip = new JSZip(data);
32 If you need to adapt an existing solution to what getBinaryContent does, here
33 are the details. When doing a XHR request (level 1, without setting the
34 `responseType`) the browser will try to interpret the response as a string and
35 decode it from its charset. To avoid this on Firefox/Chrome/Opera, you need to
36 set mime type : `xhr.overrideMimeType("text/plain; charset=x-user-defined");`.
37 On IE <= 9, this is harder. The overrideMimeType trick doesn't work so we need
38 to use [vbscript](http://stackoverflow.com/questions/1095102/how-do-i-load-binary-image-data-using-javascript-and-xmlhttprequest)
39 and non standard attributes.
40 On IE > 9, overrideMimeType doesn't work but xhr2 does.
42 With [xhr 2](http://caniuse.com/xhr2), you can just set the responseType
43 attribute : `xhr.responseType = "arraybuffer";`. With this, the browser will
44 return an ArrayBuffer.
48 If the browser supports the [FileReader API](http://caniuse.com/filereader),
49 you can use it to read a zip file. JSZip can read ArrayBuffer, so you can use
50 `FileReader.readAsArrayBuffer(Blob)`, see this [example]({{site.baseurl}}/documentation/examples/read-local-file-api.html).
54 JSZip can read Buffers so you can do the following :
61 var fs = require("fs");
62 var JSZip = require("jszip");
65 fs.readFile("test.zip", function(err, data) {
67 var zip = new JSZip(data);
70 // read a file and add it to a zip
71 fs.readFile("picture.png", function(err, data) {
73 var zip = new JSZip();
74 zip.file("picture.png", data);
80 There are a lot of nodejs libraries doing http requests, from the built-in
81 [http](http://nodejs.org/docs/latest/api/http.html) to the
82 [npm packages](https://www.npmjs.org/browse/keyword/http). Here are two
83 examples, one with the default http API, the other with
84 [request](https://github.com/mikeal/request) (but you're free to use your
85 favorite library !). If possible, download the file as a Buffer (you will get
86 better performances). If it's not possible, you can fallback to a binary string
87 (the option is likely to be `encoding : "binary"`).
94 var http = require("http");
95 var url = require("url");
96 var JSZip = require("jszip");
98 var req = http.get(url.parse("http://localhost/.../file.zip"), function (res) {
99 if (res.statusCode !== 200) {
100 console.log(res.statusCode);
104 var data = [], dataLen = 0;
106 // don't set the encoding, it will break everything !
107 // or, if you must, set it to null. In that case the chunk will be a string.
109 res.on("data", function (chunk) {
111 dataLen += chunk.length;
114 res.on("end", function () {
115 var buf = new Buffer(dataLen);
116 for (var i=0,len=data.length,pos=0; i<len; i++) {
117 data[i].copy(buf, pos);
118 pos += data[i].length;
122 var zip = new JSZip(buf);
123 console.log(zip.file("content.txt").asText());
127 req.on("error", function(err){
137 var request = require('request');
138 var JSZip = require("jszip");
142 url : "http://localhost/.../file.zip",
143 encoding: null // <- this one is important !
144 }, function (error, response, body) {
145 if(error || response.statusCode !== 200) {
149 var zip = new JSZip(body);
150 console.log(zip.file("content.txt").asText());