diff options
Diffstat (limited to 'afm-client/bower_components/jszip/documentation/howto')
-rw-r--r-- | afm-client/bower_components/jszip/documentation/howto/read_zip.md | 152 | ||||
-rw-r--r-- | afm-client/bower_components/jszip/documentation/howto/write_zip.md | 106 |
2 files changed, 258 insertions, 0 deletions
diff --git a/afm-client/bower_components/jszip/documentation/howto/read_zip.md b/afm-client/bower_components/jszip/documentation/howto/read_zip.md new file mode 100644 index 0000000..48a5240 --- /dev/null +++ b/afm-client/bower_components/jszip/documentation/howto/read_zip.md @@ -0,0 +1,152 @@ +--- +title: "How to read a file" +layout: default +section: example +--- + +This page explains how to read an existing zip file or add a existing file into +the zip file. + + +### In the browser + +#### AJAX request + +Getting binary data with an ajax request is hard (mainly because of IE <= 9). +The easy way is to use [JSZipUtils.getBinaryContent](https://github.com/stuk/jszip-utils). +With JSZipUtils.getBinaryContent, you can do the following (see the +documentation for more examples) : + +```js +JSZipUtils.getBinaryContent('path/to/content.zip', function(err, data) { + if(err) { + throw err; // or handle err + } + + var zip = new JSZip(data); +}); +``` + +<br> + +If you need to adapt an existing solution to what getBinaryContent does, here +are the details. When doing a XHR request (level 1, without setting the +`responseType`) the browser will try to interpret the response as a string and +decode it from its charset. To avoid this on Firefox/Chrome/Opera, you need to +set mime type : `xhr.overrideMimeType("text/plain; charset=x-user-defined");`. +On IE <= 9, this is harder. The overrideMimeType trick doesn't work so we need +to use [vbscript](http://stackoverflow.com/questions/1095102/how-do-i-load-binary-image-data-using-javascript-and-xmlhttprequest) +and non standard attributes. +On IE > 9, overrideMimeType doesn't work but xhr2 does. + +With [xhr 2](http://caniuse.com/xhr2), you can just set the responseType +attribute : `xhr.responseType = "arraybuffer";`. With this, the browser will +return an ArrayBuffer. + +#### Local files + +If the browser supports the [FileReader API](http://caniuse.com/filereader), +you can use it to read a zip file. JSZip can read ArrayBuffer, so you can use +`FileReader.readAsArrayBuffer(Blob)`, see this [example]({{site.baseurl}}/documentation/examples/read-local-file-api.html). + +### In nodejs + +JSZip can read Buffers so you can do the following : + +#### Local file + +```js +"use strict"; + +var fs = require("fs"); +var JSZip = require("jszip"); + +// read a zip file +fs.readFile("test.zip", function(err, data) { + if (err) throw err; + var zip = new JSZip(data); +}); + +// read a file and add it to a zip +fs.readFile("picture.png", function(err, data) { + if (err) throw err; + var zip = new JSZip(); + zip.file("picture.png", data); +}); +``` + +#### Remote file + +There are a lot of nodejs libraries doing http requests, from the built-in +[http](http://nodejs.org/docs/latest/api/http.html) to the +[npm packages](https://www.npmjs.org/browse/keyword/http). Here are two +examples, one with the default http API, the other with +[request](https://github.com/mikeal/request) (but you're free to use your +favorite library !). If possible, download the file as a Buffer (you will get +better performances). If it's not possible, you can fallback to a binary string +(the option is likely to be `encoding : "binary"`). + +##### With http : + +```js +"use strict"; + +var http = require("http"); +var url = require("url"); +var JSZip = require("jszip"); + +var req = http.get(url.parse("http://localhost/.../file.zip"), function (res) { + if (res.statusCode !== 200) { + console.log(res.statusCode); + // handle error + return; + } + var data = [], dataLen = 0; + + // don't set the encoding, it will break everything ! + // or, if you must, set it to null. In that case the chunk will be a string. + + res.on("data", function (chunk) { + data.push(chunk); + dataLen += chunk.length; + }); + + res.on("end", function () { + var buf = new Buffer(dataLen); + for (var i=0,len=data.length,pos=0; i<len; i++) { + data[i].copy(buf, pos); + pos += data[i].length; + } + + // here we go ! + var zip = new JSZip(buf); + console.log(zip.file("content.txt").asText()); + }); +}); + +req.on("error", function(err){ + // handle error +}); +``` + +##### With request : + +```js +"use strict"; + +var request = require('request'); +var JSZip = require("jszip"); + +request({ + method : "GET", + url : "http://localhost/.../file.zip", + encoding: null // <- this one is important ! +}, function (error, response, body) { + if(error || response.statusCode !== 200) { + // handle error + return; + } + var zip = new JSZip(body); + console.log(zip.file("content.txt").asText()); +}); +``` diff --git a/afm-client/bower_components/jszip/documentation/howto/write_zip.md b/afm-client/bower_components/jszip/documentation/howto/write_zip.md new file mode 100644 index 0000000..e524491 --- /dev/null +++ b/afm-client/bower_components/jszip/documentation/howto/write_zip.md @@ -0,0 +1,106 @@ +--- +title: "How to write a file / give it to the user" +layout: default +section: example +--- + +### In the browser + +With only javascript, this part won't work in old browsers, including IE < 10. +For those browsers, you can use a flash polyfill, see below. + +You can also see this +[example]({{site.baseurl}}/documentation/examples/download-zip-file.html). + +#### Blob URL / FileSaver + +With recent browsers, the easiest way is to use `saveAs` or a polyfill, see +[FileSaver.js](https://github.com/eligrey/FileSaver.js) : + +```js +var blob = zip.generate({type:"blob"}); +saveAs(blob, "hello.zip"); +``` + +Under the hood, the polyfill uses the native `saveAs` from the +[FileSaver](http://www.w3.org/TR/file-writer-api/#the-filesaver-interface) API +(on Chrome and IE10+) or use a [Blob URL](http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download) +(on Firefox). + + +#### Data URI + +For older browsers that support [data URI](http://caniuse.com/datauri), you can also +do the following : + +```js +location.href="data:application/zip;base64," + zip.generate({type:"base64"}); +``` + +The biggest issue here is that the filenames are very awkward, Firefox +generates filenames such as `a5sZQRsx.zip.part` (see bugs +[367231](https://bugzilla.mozilla.org/show_bug.cgi?id=367231) and +[532230](https://bugzilla.mozilla.org/show_bug.cgi?id=532230), and Safari +isn't much better with just `Unknown`. + +Browser support and resulting filename : + +Opera | Firefox | Safari | Chrome | Internet Explorer +-------|---------|--------|--------|------------------ +"default.zip" | random alphanumeric with ".part" extension | "Unknown" (no extension) | "download.zip" on OSX and Linux, just "download" on Windows | No + +#### Downloadify + +[Downloadify](https://github.com/dcneiner/downloadify) uses a small Flash SWF +to download files to a user's computer with a filename that you can choose. +Doug Neiner has added the `dataType` option to allow you to pass a zip for +downloading. Follow the [Downloadify demo](http://pixelgraphics.us/downloadify/test.html) +with the following changes: + +```js +zip = new JSZip(); +zip.file("Hello.", "hello.txt"); +Downloadify.create('downloadify',{ +... + data: function(){ + return zip.generate({type:"base64"}); + }, +... + dataType: 'base64' +}); +``` + +<!-- +TODO : send data as GET / POST ? +--> + +#### Deprecated google gears + +[Franz Buchinger](http://www.picurl.org/blog/author/franz/) has written a +brilliant tutorial on [using JSZip with Google Gears](http://www.picurl.org/blog/2009/11/22/creating-zip-archives-with-gears) +([part 2](http://www.picurl.org/blog/2009/11/29/gearszipper-part2-adding-support-for-real-files-and-canvas-elements/)). +If you want to let your Gears users download several files at once I really +recommend having a look at some of his [examples](http://picurl.org/gears/zipper/). + + + +### In nodejs + +JSZip can generate Buffers so you can do the following : + +```js +var fs = require("fs"); +var JSZip = require("jszip"); + +var zip = new JSZip(); +// zip.file("file", content); +// ... and other manipulations + +var buffer = zip.generate({type:"nodebuffer"}); + +fs.writeFile("test.zip", buffer, function(err) { + if (err) throw err; +}); +``` + + |