Update JSON API
[src/app-framework-demo.git] / afm-client / bower_components / jszip / documentation / howto / read_zip.md
1 ---
2 title: "How to read a file"
3 layout: default
4 section: example
5 ---
6
7 This page explains how to read an existing zip file or add a existing file into
8 the zip file.
9
10
11 ### In the browser
12
13 #### AJAX request
14
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) :
19
20 ```js
21 JSZipUtils.getBinaryContent('path/to/content.zip', function(err, data) {
22   if(err) {
23     throw err; // or handle err
24   }
25
26   var zip = new JSZip(data);
27 });
28 ```
29
30 <br>
31
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.
41
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.
45
46 #### Local files
47
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).
51
52 ### In nodejs
53
54 JSZip can read Buffers so you can do the following :
55
56 #### Local file
57
58 ```js
59 "use strict";
60
61 var fs = require("fs");
62 var JSZip = require("jszip");
63
64 // read a zip file
65 fs.readFile("test.zip", function(err, data) {
66   if (err) throw err;
67   var zip = new JSZip(data);
68 });
69
70 // read a file and add it to a zip
71 fs.readFile("picture.png", function(err, data) {
72   if (err) throw err;
73   var zip = new JSZip();
74   zip.file("picture.png", data);
75 });
76 ```
77
78 #### Remote file
79
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"`).
88
89 ##### With http :
90
91 ```js
92 "use strict";
93
94 var http = require("http");
95 var url = require("url");
96 var JSZip = require("jszip");
97
98 var req = http.get(url.parse("http://localhost/.../file.zip"), function (res) {
99   if (res.statusCode !== 200) {
100     console.log(res.statusCode);
101     // handle error
102     return;
103   }
104   var data = [], dataLen = 0;
105
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.
108
109   res.on("data", function (chunk) {
110     data.push(chunk);
111     dataLen += chunk.length;
112   });
113
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;
119     }
120
121     // here we go !
122     var zip = new JSZip(buf);
123     console.log(zip.file("content.txt").asText());
124   });
125 });
126
127 req.on("error", function(err){
128   // handle error
129 });
130 ```
131
132 ##### With request :
133
134 ```js
135 "use strict";
136
137 var request = require('request');
138 var JSZip = require("jszip");
139
140 request({
141   method : "GET",
142   url : "http://localhost/.../file.zip",
143   encoding: null // <- this one is important !
144 }, function (error, response, body) {
145   if(error ||  response.statusCode !== 200) {
146     // handle error
147     return;
148   }
149   var zip = new JSZip(body);
150   console.log(zip.file("content.txt").asText());
151 });
152 ```