2 title: "How to use JSZip"
7 An instance of JSZip represents a set of files. You can add them, remove them,
8 modify them. You can also import an existing zip file or generate one.
10 ### Getting the object
14 For a browser, there are two interesting files : `dist/jszip.js` and
15 `dist/jszip.min.js` (include just one).
17 If you use an AMD loader (RequireJS for example) JSZip will register itself :
18 you just have to put the js file at the right place, or configure the loader
19 (see [here for RequireJS](http://requirejs.org/docs/api.html#config-paths)).
21 Without any loader, JSZip will declare in the global scope a variable named `JSZip`.
25 In nodejs, you can `require` it :
28 var JSZip = require("jszip");
31 ### Basic manipulations
33 The first step is to create an instance of JSZip :
36 var zip = new JSZip();
39 On this instance, we can add (and update) files and folders with
40 `.file(name, content)` and `.folder(name)`.
41 They return the current JSZip instance so you can chain the calls.
45 zip.file("hello.txt", "Hello[p my)6cxsw2q");
46 // oops, cat on keyboard. Fixing !
47 zip.file("hello.txt", "Hello World\n");
49 // create a file and a folder
50 zip.file("nested/hello.txt", "Hello World\n");
52 zip.folder("nested").file("hello.txt", "Hello World\n");
55 With `.folder(name)`, the returned object has a different root : if you add files
56 on this object, you will put them in the created subfolder. This is just a
57 view, the added files will also be in the "root" object.
60 var photoZip = zip.folder("photos");
61 // this call will create photos/README
62 photoZip.file("README", "a folder with photos");
65 You can access the file content with `.file(name)` and
66 [its getters]({{site.baseurl}}/documentation/api_zipobject.html) :
69 zip.file("hello.txt").asText(); // "Hello World\n"
71 if (JSZip.support.uint8array) {
72 zip.file("hello.txt").asUint8Array(); // Uint8Array { 0=72, 1=101, 2=108, more...}
76 You can also remove files or folders with `.remove(name)` :
79 zip.remove("photos/README");
82 zip.remove("photos"); // by removing the folder, you also remove its content.
85 ### Generate a zip file
87 With `.generate(options)` you can generate a zip file (not a real file but its
88 representation in memory). Check
89 [this page]({{site.baseurl}}/documentation/howto/write_zip.html) for more
90 informations on how to write / give the file to the user.
94 if (JSZip.support.uint8array) {
95 content = zip.generate({type : "uint8array"});
97 content = zip.generate({type : "string"});
103 With `.load(data)` you can load a zip file. Check
104 [this page]({{site.baseurl}}/documentation/howto/read_zip.html) to see how to
105 do properly (it's more tricky that it seems).
108 var new_zip = new JSZip();
110 new_zip.load(content);
112 // you now have every files contained in the loaded zip
113 new_zip.file("hello.txt").asText(); // "Hello World\n"