Implemented URL query parsing for initial token /opa/?token=abcde
[src/app-framework-demo.git] / afb-client / bower_components / jszip / documentation / api_jszip / generate.md
1 ---
2 title: "generate(options)"
3 layout: default
4 section: api
5 ---
6
7 __Description__ : Generates the complete zip file.
8
9 __Arguments__
10
11 name                | type    | default | description
12 --------------------|---------|---------|------------
13 options             | object  |         | the options to generate the zip file :
14 options.base64      | boolean | false   | **deprecated**, use `type` instead. If `type` is not used, set to `false` to get the result as a raw byte string, `true` to encode it as base64.
15 options.compression | string  | `STORE` (no compression) | the default file compression method to use. Available methods are `STORE` and `DEFLATE`. You can also provide your own compression method.
16 options.compressionOptions | object | `null` | the options to use when compressing the file, see below.
17 options.type        | string  | `base64` | The type of zip to return, see below for the other types.
18 options.comment     | string  |          | The comment to use for the zip file.
19 options.mimeType    | string  | `application/zip` | mime-type for the generated file. Useful when you need to generate a file with a different extension, ie: ".ods".
20 options.platform    | string  | `DOS`    | The platform to use when generating the zip file.
21
22 Possible values for `type` :
23
24 * `base64` (default) : the result will be a string, the binary in a base64 form.
25 * `string` : the result will be a string in "binary" form, using 1 byte per char (2 bytes).
26 * `uint8array` : the result will be a Uint8Array containing the zip. This requires a compatible browser.
27 * `arraybuffer` : the result will be a ArrayBuffer containing the zip. This requires a compatible browser.
28 * `blob` : the result will be a Blob containing the zip. This requires a compatible browser.
29 * `nodebuffer` : the result will be a nodejs Buffer containing the zip. This requires nodejs.
30
31 Note : when using type = "uint8array", "arraybuffer" or "blob", be sure to
32 check if the browser supports it (you can use [`JSZip.support`]({{site.baseurl}}/documentation/api_jszip/support.html)).
33
34 The `compressionOptions` parameter depends on the compression type. With
35 `STORE` (no compression), this parameter is ignored. With `DEFLATE`, you can
36 give the compression level with `compressionOptions : {level:6}` (or any level
37 between 1 (best speed) and 9 (best compression)).
38
39 Note : if the entry is *already* compressed (coming from a compressed zip file),
40 calling `generate()` with a different compression level won't update the entry.
41 The reason is simple : JSZip doesn't know how compressed the content was and
42 how to match the compression level with the implementation we use.
43
44 Note for the `comment` option : the zip format has no flag or field to give the
45 encoding of this field and JSZip will use UTF-8. With non ASCII characters you
46 might get encoding issues if the file archiver doesn't use UTF-8 to decode the
47 comment.
48
49 If not set, JSZip will use the field `comment` on its `options`.
50
51 Possible values for `platform` : `DOS` and `UNIX`. It also accepts nodejs
52 `process.platform` values.
53 When using `DOS`, the attribute `dosPermissions` of each file is used.
54 When using `UNIX`, the attribute `unixPermissions` of each file is used.
55
56 If you set the platform value on nodejs, be sure to use `process.platform`.
57 `fs.stats` returns a non executable mode for folders on windows, if you
58 force the platform to `UNIX` the generated zip file will have a strange
59 behavior on UNIX platforms.
60
61 __Returns__ : The generated zip file.
62
63 __Throws__ : An exception if the asked `type` is not available in the browser,
64 see [JSZip.support]({{site.baseurl}}/documentation/api_jszip/support.html).
65
66 <!-- __Complexity__ : TODO : worst case, with/out compression, etc -->
67
68 __Example__
69
70 ```js
71 var content = zip.generate({type:"blob"});
72 // see FileSaver.js
73 saveAs(content, "hello.zip");
74 ```
75
76 ```js
77 var content = zip.generate({type:"base64"});
78 location.href="data:application/zip;base64,"+content;
79 ```
80
81 ```js
82 var content = zip.generate({type:"nodebuffer"});
83 require("fs").writeFile("hello.zip", content, function(err){/*...*/});
84 ```
85
86 ```js
87 // on nodejs
88 zip.file(pathname, content, {
89     date: stat.mtime,
90     unixPermissions: stat.mode
91 });
92
93 // ...
94
95 zip.generate({
96     type: 'nodebuffer',
97     platform: process.platform
98 });
99 ```
100
101 ```js
102 //This example will Generate a Open Document Spreasheet, with the correct mime type
103 var zip = new JSZip();
104 zip.file("mimetype", "application/vnd.oasis.opendocument.spreadsheet");
105 var conf2 = zip.folder("Configurations2");
106 conf2.folder("acceleator");
107 conf2.folder("images");
108 conf2.folder("popupmenu");
109 conf2.folder("statusbar");
110 conf2.folder("floater");
111 conf2.folder("menubar");
112 conf2.folder("progressbar");
113 conf2.folder("toolbar");
114
115 var manifest = "<..."; //xml containing manifest.xml 
116 var styles = "<..."; //xml containing styles.xml
117 var settings = "<..."; //xml containing settings.xml
118 var meta = "<..."; //xml containing meta.xml
119 var content = "<..."; //xml containing content.xml
120
121 var metaInf = zip.folder("META-INF");
122 metaInf.file("manifest.xml", manifest);
123 zip.file("styles.xml", styles);
124 zip.file("settings.xml", settings); 
125 zip.file("meta.xml", meta);
126 zip.file("content.xml", content);
127
128 //Generate the file
129 var odsFile = zip.generate({type: "blob", mimeType: "application/ods", compression: "DEFLATE"});
130
131 var url = window.URL.createObjectURL(odsFile);
132 var link = document.getElementById("link"); //I suppose you'll have a link with this id :)
133 link.download = "testjs.ods";
134 link.href = url;
135
136
137 ```
138
139