index.html.orig 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  1. ---
  2. title: JSZip
  3. layout: default
  4. section: main
  5. ---
  6. <div class="row">
  7. <div class="col-md-5">
  8. JSZip is a javascript library for creating, reading and editing .zip files, with a
  9. lovely and simple API.
  10. </div>
  11. <div class="col-md-7">
  12. <p>
  13. <strong>Current version</strong> : v2.2.0
  14. </p>
  15. <p>
  16. <strong>License</strong> : JSZip is dual-licensed. You may use it under the
  17. MIT license <em>or</em> the GPLv3 license. See
  18. <a href="https://github.com/Stuk/jszip/blob/master/LICENSE.markdown">LICENSE.markdown</a>.
  19. </p>
  20. </div>
  21. <div class="row">
  22. <div class="col-md-5">
  23. <h3>Example</h3>
  24. <script type="text/javascript">
  25. imgData = "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=";
  26. jQuery(function($) {
  27. $("#demo").click(function () {
  28. try {
  29. eval($("#demo-code").val());
  30. $("#status")
  31. .removeClass()
  32. .addClass("text-success")
  33. .text("Done!");
  34. }
  35. catch (e) {
  36. $("#status")
  37. .removeClass()
  38. .addClass("text-danger")
  39. .text(e);
  40. }
  41. });
  42. });
  43. </script>
  44. <textarea class="form-control" id="demo-code" rows="7" spellcheck="false">
  45. var zip = new JSZip();
  46. zip.file("Hello.txt", "Hello World\n");
  47. var img = zip.folder("images");
  48. img.file("smile.gif", imgData, {base64: true});
  49. <<<<<<< HEAD
  50. var content = zip.generate();
  51. location.href="data:application/zip;base64,"+content;
  52. </textarea>
  53. <button id="demo">Run!</button>
  54. <span id="status"></span>
  55. </div>
  56. <div id="why" class="grid_4 border">
  57. <h2>Why?</h2>
  58. <ol>
  59. <li>JavaScript today is capable of generating a lot of data. The easiest way to deliver multiple files to your users is in a zip file. Instead of wasting server resources and bandwidth you can get the client to do it for you.</li>
  60. <li>Because it's cool!</li>
  61. </ol>
  62. </div>
  63. <div id="where" class="grid_3">
  64. <h2>Where?</h2>
  65. <p>
  66. <a href="http://github.com/Stuk/jszip/zipball/master" class="download grid_3 alpha omega">Download</a>
  67. <small>from <a href="http://github.com/Stuk/jszip">Github</a></small>
  68. </p>
  69. <p>
  70. See also: the <a href="test/">test suite</a>
  71. </p>
  72. </div>
  73. <hr/>
  74. <div class="grid_9 border">
  75. <h2>Tell me more!</h2>
  76. <h3>Browser support</h3>
  77. <table cellspacing="0" class="browser_support">
  78. <tr><th class="op">Opera</th> <th class="ff">Firefox</th> <th class="sf">Safari</th> <th class="cr">Chrome</th> <th class="ie">Internet Explorer</th> </tr>
  79. <tr><td>Yes</td> <td>Yes</td> <td>Yes</td> <td>Yes</td> <td>Yes</td></tr>
  80. <tr>
  81. <td>Tested with the latest version</td>
  82. <td>Tested with 3.0 / 3.6 / latest version</td>
  83. <td>Tested with the latest version</td>
  84. <td>Tested with the latest version</td>
  85. <td>Tested with IE 6 / 7 / 8 / 9 / 10</td>
  86. </tr>
  87. </table>
  88. <p>While JSZip should work everywhere, the tricky part is to give the zip file to the user.</p>
  89. <h3>Browser support for <a href="https://en.wikipedia.org/wiki/Data_URI_scheme">data URI scheme</a> with zip</h3>
  90. <table cellspacing="0" class="browser_support">
  91. <tr><th class="op">Opera</th> <th class="ff">Firefox</th> <th class="sf">Safari</th> <th class="cr">Chrome</th> <th class="ie">Internet Explorer</th> </tr>
  92. <tr><td>7.5+</td> <td>3.0+</td> <td>Yes</td> <td>Yes</td> <td>No</td></tr>
  93. <tr>
  94. <td>Filename is "default.zip"</td>
  95. <td>Filename is random alphanumeric with ".part" extension</td>
  96. <td>Filename is "Unknown" (no extension)</td>
  97. <td>Filename is "download.zip" on OSX and Linux, and just "download" on Windows (<a href="https://github.com/Stuk/jszip/issues/9">issue #9</a>)</td>
  98. <td><a href="http://msdn.microsoft.com/en-us/library/cc848897(VS.85).aspx">Only supports data URLs for some content</a>. (May be able to use <a href="http://www.phpied.com/mhtml-when-you-need-data-uris-in-ie7-and-under/">MHTML</a>?)</td>
  99. </tr>
  100. </table>
  101. <h3>Filename problems</h3>
  102. <p>The biggest issue with JSZip is that the filenames are very awkward, Firefox generates filenames such as <code>a5sZQRsx.zip.part</code> (see bugs <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=367231">367231</a> and <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=532230">532230</a>), and Safari isn't much better with just <code>Unknown</code>. Sadly there is no pure Javascript solution (and working in every browsers) to this. However...</p>
  103. <h4>Solution-ish: <a href="https://github.com/dcneiner/downloadify">Downloadify</a></h4>
  104. <p>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 <code>dataType</code> option to allow you to pass a zip for downloading. Follow the <a href="http://pixelgraphics.us/downloadify/test.html">Downloadify demo</a> with the following changes:</p>
  105. <pre class="example">
  106. zip = new JSZip();
  107. zip.file("Hello.", "hello.txt");
  108. Downloadify.create('downloadify',{
  109. ...
  110. data: function(){
  111. return zip.generate();
  112. },
  113. ...
  114. dataType: 'base64'
  115. });</pre>
  116. <h4>Other solution-ish: Blob URL / FileSaver / FileSaver.js</h4>
  117. <p>With <a href="http://caniuse.com/bloburls">some recent browsers</a> come a new way to download Blobs (a zip file for example) : blob urls. The <a href="http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download">download attribute on &lt;a&gt;</a> allows you to give the name of the file. Blob urls start to be widely supported but this attribute is currently only supported in Chrome and Firefox (>= 20). See <a href="examples/download-zip-file.html">the example</a>. Note : on IE 10/11, using a blob url as a href doesn't seem to work.</p>
  118. <pre class="example">
  119. var blob = zip.generate({type:"blob"});
  120. myLink.href = window.URL.createObjectURL(blob);
  121. myLink.download = "myFile.zip";</pre>
  122. <p>An other solution is the <a href="http://www.w3.org/TR/file-writer-api/#the-filesaver-interface">FileSaver</a> interface, created (at the time of writing) with <code>saveAs</code>. This works on Chrome and IE &gt;= 10 (not in compatibility view) but not Firefox.</p>
  123. <pre class="example">
  124. var blob = zip.generate({type:"blob"});
  125. window.saveAs(blob, "hello.zip");</pre>
  126. <p>Finally, you can use the polyfill <a href="https://github.com/eligrey/FileSaver.js">FileSaver.js</a> which will use a FileSaver if present or else a blob url.</p>
  127. <h3>Usage with Google Gears</h3>
  128. <p>
  129. <a href="http://www.picurl.org/blog/author/franz/">Franz Buchinger</a> has written a brilliant tutorial on <a href="http://www.picurl.org/blog/2009/11/22/creating-zip-archives-with-gears/">using JSZip with Google Gears</a> (<a href="http://www.picurl.org/blog/2009/11/29/gearszipper-part2-adding-support-for-real-files-and-canvas-elements/">part 2</a>). If you want to let your Gears users download several files at once I really recommend having a look at some of his <a href="http://picurl.org/gears/zipper/">examples</a>.</p>
  130. <h3>Reading a zip file from an ajax call</h3>
  131. <p>
  132. When doing an ajax call to get the binary data, the browser will try to interpret the binary as text, corrupting it. The solution is to <a href="https://developer.mozilla.org/En/XMLHttpRequest/Using_XMLHttpRequest#Receiving_binary_data_in_older_browsers">set the mimetype to 'text/plain; charset=x-user-defined'</a>. This solution works well in all browsers but IE. If you need IE support, please see what is done in the file test/index.html.
  133. </p>
  134. <p>An other solution is to use a modern browser (supporting <a href="http://caniuse.com/xhr2">xhr2</a>) : setting <code>xhr.type = 'arraybuffer';</code> will do the trick, JSZip supports ArrayBuffers. Please see <a href="examples/get-binary-files-xhr2.html">the example</a>.
  135. <h3>Reading a local zip file (File API)</h3>
  136. <p>
  137. JSZip supports (if available in the browser) the File API : reading a local zip file is simple : <code>new JSZip(readerEvent.target.result);</code>. Please see the <a href="examples/read-local-file-api.html">complete example</a> for more details.
  138. </p>
  139. <h2 style="margin-top: 2em;">Documentation</h2>
  140. <h4 id="doc_new_JSZip">new JSZip() or JSZip()</h4>
  141. <dl>
  142. <dt>Description : </dt>
  143. <dd>The default constructor.</dd>
  144. <dt>Returns : </dt>
  145. <dd>A new JSZip.</dd>
  146. </dl>
  147. <pre class="example">
  148. var zip = new JSZip();
  149. // same as
  150. var zip = JSZip();</pre>
  151. <h4 id="doc_new_JSZip_data_options">new JSZip(data [,options]) or JSZip(data [,options])</h4>
  152. <dl>
  153. <dt>Description : </dt>
  154. <dd>Create a new JSZip file and load an existing zip file. See the documentation of <a href="#doc_load_data_options"><code>load()</code></a> for more details and <a href="#zip_load_limits">this</a> for the limitations.</dd>
  155. <dt>Parameters : </dt>
  156. <dd><code>data</code> (same types as <a href="#doc_load_data_options"><code>load()</code></a>) the content of the zip file to load.</dd>
  157. <dd><code>options</code> (Object) options to pass to the <a href="#doc_load_data_options"><code>load()</code></a> method..</dd>
  158. <dt>Returns : </dt>
  159. <dd>A new JSZip.</dd>
  160. </dl>
  161. <pre class="example">
  162. new JSZip(zipDataFromXHR, {base64:false});
  163. // same as
  164. JSZip(zipDataFromXHR, {base64:false});
  165. // same as
  166. var zip = new JSZip();
  167. zip.load(zipDataFromXHR, {base64:false});</pre>
  168. <h4 id="doc_file_name">file(name)</h4>
  169. <dl>
  170. <dt>Description : </dt>
  171. <dd>Get a file with the specified name.</dd>
  172. <dt>Parameters : </dt>
  173. <dd><code>name</code> (String) the name of the file.</dd>
  174. <dt>Returns : </dt>
  175. <dd>The file if any, <code>null</code> otherwise. The file has the following structure :
  176. <ul>
  177. <li><code>name</code> the absolute path of the file</li>
  178. <li><code>options</code> the options of the file. The available options are :
  179. <ul>
  180. <li><code>base64</code>, boolean, cf <a href="#doc_file_name_data_options">file(name, data [,options])</a></li>
  181. <li><code>binary</code>, boolean, cf <a href="#doc_file_name_data_options">file(name, data [,options])</a></li>
  182. <li><code>dir</code>, boolean, true if this is a directory</li>
  183. <li><code>date</code>, date, cf <a href="#doc_file_name_data_options">file(name, data [,options])</a></li>
  184. <li><code>compression</code>, String, cf <a href="#doc_file_name_data_options">file(name, data [,options])</a></li>
  185. </ul>
  186. </li>
  187. <li><code>asText()</code>, string, the content as an utf8 string (utf8 encoded if necessary).</li>
  188. <li><code>asBinary()</code>, string, the content as binary (utf8 decoded if necessary).</li>
  189. <li><code>asArrayBuffer()</code>, ArrayBuffer, need a <a href="#jszip_support">compatible browser</a>.</li>
  190. <li><code>asUint8Array()</code>, Uint8Array, need a <a href="#jszip_support">compatible browser</a>.</li>
  191. <li><code>asNodeBuffer()</code>, nodejs Buffer, need <a href="#jszip_support">nodejs</a>.</li>
  192. </ul>
  193. </dd>
  194. </dl>
  195. <pre class="example">
  196. var zip = new JSZip();
  197. zip.file("file.txt", "content");
  198. zip.file("file.txt").name // "file.txt"
  199. zip.file("file.txt").asText() // "content"
  200. zip.file("file.txt").options.dir // false
  201. // utf8 example
  202. var zip = new JSZip(zipFromAjaxWithUTF8);
  203. zip.file("amount.txt").asText() // "&euro;15"
  204. zip.file("amount.txt").asArrayBuffer() // an ArrayBuffer containing &euro;15
  205. zip.file("amount.txt").asUint8Array() // an Uint8Array containing &euro;15
  206. </pre>
  207. <h4 id="doc_file_regex">file(regex)</h4>
  208. <dl>
  209. <dt>Description : </dt>
  210. <dd>Search a file in the current folder and subfolders with a <a href="http://www.javascriptkit.com/javatutors/redev.shtml">regular expression</a>. The regex is tested against the relative filename.</dd>
  211. <dt>Parameters : </dt>
  212. <dd><code>regex</code> (RegExp) the regex to use.</dd>
  213. <dt>Returns : </dt>
  214. <dd>An array of matching files (an empty array if none matched).</dd>
  215. </dl>
  216. <pre class="example">
  217. var zip = new JSZip();
  218. zip.file("file1.txt", "content");
  219. zip.file("file2.txt", "content");
  220. zip.file(/file/); // array of size 2
  221. // example with a relative path :
  222. var folder = zip.folder("sub");
  223. folder
  224. .file("file3.txt", "content") // relative path from folder : file3.txt
  225. .file("file4.txt", "content"); // relative path from folder : file4.txt
  226. folder.file(/file/); // array of size 2
  227. folder.file(/^file/); // array of size 2, the relative paths start with file
  228. // arrays contain objects in the form:
  229. // {name: "file2.txt", dir: false, asText : function () {...}, ...}</pre>
  230. <h4 id="doc_file_name_data_options">file(name, data [,options])</h4>
  231. <dl>
  232. <dt>Description : </dt>
  233. <dd>Add a file to the zip file.</dd>
  234. <dt>Parameters : </dt>
  235. <dd><code>name</code> (String) the name of the file.</dd>
  236. <dd><code>data</code> (String/ArrayBuffer/Uint8Array/Buffer) the content of the file.</dd>
  237. <dd><code>options</code> (Object) the options :
  238. <ul>
  239. <li><code>base64</code> (boolean) set to <code>true</code> if the data
  240. is base64 encoded. For example image data from a <code>&lt;canvas&gt;</code> element.
  241. Plain text and HTML do not need this option.</li>
  242. <li><code>binary</code> (boolean) defaults to <code>true</code> if the data
  243. is base64 encoded, <code>false</code> otherwise. If set to false then
  244. UTF-8 characters will be encoded. If the data is an ArrayBuffer or an Uint8Array, this will be set to true.</li>
  245. <li><code>date</code> (Date) use it to specify the last modification date.
  246. If not set the current date is used.</li>
  247. <li><code>compression</code> (String), default null. If set, specifies the file compression method to use. If not, the default file compression is used, cf <a href="#doc_generate_options">generate(options)</a>.</li>
  248. <li><code>optimizedBinaryString</code> (boolean), default false. Set it to true if (and only if) the input is a string and
  249. has already been prepared with a 0xFF mask.</li>
  250. </ul>
  251. </dd>
  252. <dt>Returns : </dt>
  253. <dd>A JSZip object, for chaining.</dd>
  254. </dl>
  255. <pre class="example">
  256. zip.file("Hello.txt", "Hello World\n");
  257. zip.file("smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true});
  258. zip.file("magic.txt", "U2VjcmV0IGNvZGU=", {base64: true, binary: false});
  259. zip.file("Xmas.txt", "Ho ho ho !", {date : new Date("December 25, 2007 00:00:01")});
  260. zip.file("folder/file.txt", "file in folder");
  261. zip.file("animals.txt", "dog,platypus\n").file("people.txt", "james,sebastian\n");
  262. // result : Hello.txt, smile.gif, magic.txt, Xmas.txt, animals.txt, people.txt,
  263. // folder/, folder/file.txt</pre>
  264. <h4 id="doc_folder_name">folder(name)</h4>
  265. <dl>
  266. <dt>Description : </dt>
  267. <dd>Add a directory to the zip file.</dd>
  268. <dt>Parameters : </dt>
  269. <dd><code>name</code> (String) the name of the directory.</dd>
  270. <dt>Returns : </dt>
  271. <dd>a new JSZip (for chaining), with the new folder as root.</dd>
  272. </dl>
  273. <pre class="example">
  274. zip.folder("images");
  275. zip.folder("css").file("style.css", "body {background: #FF0000}");
  276. // or specify an absolute path (using forward slashes)
  277. zip.file("css/font.css", "body {font-family: sans-serif}")
  278. // result : images/, css/, css/style.css, css/font.css</pre>
  279. <h4 id="doc_folder_regex">folder(regex)</h4>
  280. <dl>
  281. <dt>Description : </dt>
  282. <dd>Search a subdirectory.</dd>
  283. <dd>Search a subdirectory in the current directory with a <a href="http://www.javascriptkit.com/javatutors/redev.shtml">regular expression</a>. The regex is tested against the relative path.</dd>
  284. <dt>Parameters : </dt>
  285. <dd><code>regex</code> (RegExp) the regex to use.</dd>
  286. <dt>Returns : </dt>
  287. <dd>An array of matching folders (an empty array if none matched).</dd>
  288. </dl>
  289. <pre class="example">
  290. var zip = new JSZip();
  291. zip.folder("home/Pierre/videos");
  292. zip.folder("home/Pierre/photos");
  293. zip.folder("home/Jean/videos");
  294. zip.folder("home/Jean/photos");
  295. zip.folder(/videos/); // array of size 2
  296. zip.folder("home/Jean").folder(/^vid/); // array of 1</pre>
  297. <h4 id="doc_remove_name">remove(name)</h4>
  298. <p>Delete a file or folder.</p>
  299. <dl>
  300. <dt>Description : </dt>
  301. <dd>Delete a file or folder (recursively).</dd>
  302. <dt>Parameters : </dt>
  303. <dd><code>name</code> (String) the name of the file/folder to delete.</dd>
  304. <dt>Returns : </dt>
  305. <dd>The current JSZip object.</dd>
  306. </dl>
  307. <pre class="example">
  308. var zip = new JSZip();
  309. zip.file("Hello.txt", "Hello World\n");
  310. zip.file("temp.txt", "nothing").remove("temp.txt");
  311. // result : Hello.txt
  312. zip.folder("css").file("style.css", "body {background: #FF0000}");
  313. zip.remove("Hello.txt").remove("css");
  314. //result : empty zip</pre>
  315. <h4 id="doc_generate_options">generate(options)</h4>
  316. <dl>
  317. <dt>Description : </dt>
  318. <dd>Generates the complete zip file.</dd>
  319. <dt>Parameters : </dt>
  320. <dd><code>options</code> (Object) the options to generate the zip file :
  321. <ul>
  322. <li><code>base64</code> (boolean) <strong>deprecated</strong>, use "type" instead. <code>false</code> to get the result as a raw byte string.
  323. Default : <code>true</code>, encode as base64.</li>
  324. <li><code>compression</code> (String) the default file compression method to use. <code>"STORE"</code> (no compression) by default,
  325. you can use <code>"DEFLATE"</code> or write your own.</li>
  326. <li><code>type</code> (String) the type of zip to return. The possible values are :
  327. <ul>
  328. <li><code>base64</code> (default) : the result will be a string, the binary in a base64 form.</li>
  329. <li><code>string</code> : the result will be a string in "binary" form, 1 byte per char.</li>
  330. <li><code>uint8array</code> : the result will be a Uint8Array containing the zip. This requires a compatible browser.</li>
  331. <li><code>arraybuffer</code> : the result will be a ArrayBuffer containing the zip. This requires a compatible browser.</li>
  332. <li><code>blob</code> : the result will be a Blob containing the zip. This requires a compatible browser.</li>
  333. <li><code>nodebuffer</code> : the result will be a nodejs Buffer containing the zip. This requires nodejs.</li>
  334. </ul>
  335. </li>
  336. </ul>
  337. </dd>
  338. <dt>Returns : </dt>
  339. <dd>The generated zip file.</dd>
  340. </dl>
  341. HTML5 note : when using type = "uint8array", "arraybuffer" or "blob", be sure to check if the browser supports it (you can use <a href="#jszip_support"><code>JSZip.support</code></a>).
  342. This method will throw an exception otherwise.
  343. <pre class="example">
  344. content = zip.generate(); // base64
  345. location.href="data:application/zip;base64,"+content;</pre>
  346. <pre class="example">
  347. var blobLink = document.getElementById('blobLink');
  348. blobLink.download = "hello.zip";
  349. blobLink.href = window.URL.createObjectURL(
  350. zip.generate({type:"blob"})
  351. );</pre>
  352. <pre class="example">
  353. content = zip.generate({type:"string"});
  354. for (var c = 0; c &lt; content.length; c++) {
  355. console.log(content.charCodeAt(c));
  356. // do other things
  357. }</pre>
  358. <h4 id="doc_load_data_options">load(data, options)</h4>
  359. <dl>
  360. <dt>Description : </dt>
  361. <dd>Read an existing zip and merge the data in the current JSZip object. This technique has some limitations, see <a href="#zip_load_limits">below</a>.</dd>
  362. <dt>Parameters : </dt>
  363. <dd><code>data</code> (String/ArrayBuffer/Uint8Array/Buffer) the zip file</dd>
  364. <dd><code>options</code> (Object) the options to load the zip file :
  365. <ul>
  366. <li><code>base64</code> (boolean) <code>true</code> if the data is base64 encoded, <code>false</code> for binary. Default : <code>false</code>.</li>
  367. <li><code>checkCRC32</code> (boolean) <code>true</code> if the read data should be checked against its CRC32. Default : <code>false</code>.</li>
  368. <li><code>optimizedBinaryString</code> (boolean), default false. Set it to true if (and only if) the input is a string and
  369. has already been prepared with a 0xFF mask.</li>
  370. </ul>
  371. </dd>
  372. <dt>Returns : </dt>
  373. <dd>The current JSZip object.</dd>
  374. </dl>
  375. <pre class="example">
  376. var zip = new JSZip();
  377. zip.load(zipDataFromXHR);</pre>
  378. <h5>Zip features supported by this method</h5>
  379. <ul>
  380. <li>Compression (<code>DEFLATE</code> supported)</li>
  381. <li>zip with data descriptor</li>
  382. <li>ZIP64</li>
  383. <li>UTF8 in file name, UTF8 in file content</li>
  384. </ul>
  385. <h5>Zip features not (yet) supported</h5>
  386. <ul>
  387. <li>password protected zip</li>
  388. <li>multi-volume zip</li>
  389. </ul>
  390. <h4 id="doc_filter_predicate">filter(predicate)</h4>
  391. <dl>
  392. <dt>Description : </dt>
  393. <dd>Filter nested files/folders with the specified function.</dd>
  394. <dt>Parameters : </dt>
  395. <dd><code>predicate</code> (function) the predicate to use : <code>function (relativePath, file) {...}</code> It takes 2 arguments : the relative path and the file.
  396. <ul>
  397. <li><code>relativePath</code> (String) The filename and its path, reliatively to the current folder.</li>
  398. <li><code>file</code> (Object) The file being tested. Like the result of <a href="#doc_file_name"><code>file(name)</code></a>, the file has the form <code>{name:"...", options:{...}, asText:function,...}</code>.</li>
  399. <li>Return true if the file should be included, false otherwise.</li>
  400. </ul>
  401. </dd>
  402. <dt>Returns : </dt>
  403. <dd>An array of matching elements.</dd>
  404. </dl>
  405. <pre class="example">
  406. var zip = new JSZip().folder("dir");
  407. zip.file("readme.txt", "content");
  408. zip.filter(function (relativePath, file){
  409. // relativePath == "readme.txt"
  410. // file = {name:"dir/readme.txt",options:{...},asText:function}
  411. return true/false;
  412. });</pre>
  413. <h4 id="jszip_support">JSZip.support</h4>
  414. <p>
  415. If the browser supports them, JSZip can take advantage of some new features : ArrayBuffer, Blob, Uint8Array.
  416. To know if JSZip can use them, you can check the JSZip.support object. It contains the following
  417. properties :
  418. <ul>
  419. <li><code>arraybuffer</code> : true if JSZip can read and generate ArrayBuffer, false otherwise.</li>
  420. <li><code>uint8array</code> : true if JSZip can read and generate Uint8Array, false otherwise.</li>
  421. <li><code>blob</code> : true if JSZip can read and generate Blob, false otherwise.</li>
  422. <li><code>nodebuffer</code> : true if JSZip can read and generate nodejs Buffer, false otherwise.</li>
  423. </ul>
  424. </p>
  425. <h3 id="zip_load_limits">Loading zip files, limitations</h3>
  426. <h4>Not supported features</h4>
  427. <p>
  428. All the features of zip files are not supported.
  429. Classic zip files will work but encrypted zip, multi-volume, etc are not supported
  430. and the load() method will throw an <code>Error</code>.
  431. </p>
  432. <h4>ZIP64 and 32bit integers</h4>
  433. <p>
  434. ZIP64 files can be loaded, but only if the zip file is not "too big". ZIP64 uses 64bits integers
  435. but Javascript represents all numbers as
  436. <a href="http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-262.pdf">
  437. 64-bit double precision IEEE 754 floating point numbers</a> (see section 8.5).
  438. So, we have 53bits for integers and
  439. <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Operators/Bitwise_Operators">
  440. bitwise operations treat everything as 32bits</a>.
  441. So if all the 64bits integers can fit into 32 bits integers, everything will be fine. If
  442. it's not the case, you will have other problems anyway (see next limitation).
  443. </p>
  444. <h4>Performance issues</h4>
  445. <p>
  446. An other limitation comes from the browser (and the machine running the browser).
  447. A compressed zip file of 10MB is "easily" opened by firefox/chrome/opera/IE10 but
  448. will crash older IE. Also keep in mind that strings in javascript are encoded in UTF-16 :
  449. a 10MB ascii text file will take 20MB of memory.
  450. </p>
  451. <p>
  452. If you're having performance issues, please consider the following :
  453. <ul>
  454. <li>Don't use IE &lt; 10. Everything is better with WebGL support.</li>
  455. <li>
  456. If you load the file from an ajax call, ask your XHR an ArrayBuffer.
  457. Loading a string is asking for troubles.
  458. </li>
  459. <li>
  460. If you want to get the content of an ASCII file as a string, consider using
  461. <code>asBinary()</code> instead of <code>asText()</code>. The transformation
  462. "binary string" -&gt; "utf string" is a consuming process.
  463. </li>
  464. </ul>
  465. </p>
  466. <h4>The output zip will differ from the input zip</h4>
  467. <p>
  468. Reading and generating a zip file won't give you back the same file.
  469. Some data are discarded (file metadata) and other are added (subfolders).
  470. </p>
  471. <h2><a href="https://github.com/Stuk/jszip/blob/master/CHANGES.md">Changelog</a></h2>
  472. <h2 id="migrating_guide">Migrating Guide</h2>
  473. <h3>From 2.0.0 to 2.1.0</h3>
  474. <p>
  475. <ul>
  476. <li>The packaging changed : instead of loading jszip.js, jszip-load.js, jszip-inflate.js, jszip-deflate.js, just include dist/jszip.js or dist/jszip.min.js</li>
  477. </ul>
  478. </p>
  479. <h3>From 1.x to 2.x</h3>
  480. <p>
  481. <ul>
  482. <li><code>JSZipBase64</code> has been renamed to <code>JSZip.base64</code>.</li>
  483. <li>
  484. The <code>data</code> attribute doesn't exist anymore :
  485. use the getters <code>asText()</code>, <code>asBinary()</code>, etc
  486. </li>
  487. <li>
  488. The compression/decompression methods now give their input type with the
  489. <code>compressInputType</code> and <code>uncompressInputType</code> attributes.
  490. </ul>
  491. Example for the data attribute :
  492. <pre>// before
  493. zip.file("test.txt").data;
  494. zip.files["test.txt"].data;
  495. zip.file("image.png").data;
  496. zip.files["image.png"].data;
  497. // after
  498. zip.file("test.txt").asText();
  499. zip.files["test.txt"].asText();
  500. zip.file("image.png").asBinary();
  501. zip.files["image.png"].asBinary();
  502. </p>
  503. </div>
  504. <div id="who" class="grid_3">
  505. <h2>Anything else?</h2>
  506. <h3>License</h3>
  507. <p>
  508. <a href="https://github.com/Stuk/jszip/blob/master/LICENSE.markdown">MIT license or GPLv3</a>
  509. </p>
  510. <h3>Version</h3>
  511. <p>2.2.1</p>
  512. <p>See the <a href="#migrating_guide">migrating guide</a> when updating the library!</p>
  513. <h3>Todo</h3>
  514. <ul>
  515. <li>Set correct version needed to extract</li>
  516. <li>Set correct version made by</li>
  517. <li>Set internal and external file attributes in central directory</li>
  518. <li>Allow setting of attributes/other info in <code>add()</code> options object</li>
  519. <li>Look into using <a href="http://www.phpied.com/mhtml-when-you-need-data-uris-in-ie7-and-under/">MHTML</a> for IE</li>
  520. </ul>
  521. <h3>Who?</h3>
  522. <p><a href="http://stuartk.com">Stuart Knightley</a>, with contributions from:</p>
  523. <ul>
  524. <li><a href="http://www.picurl.org/blog/author/franz/">Franz Buchinger</a></li>
  525. <li><a href="http://www.aadsm.net/">António Afonso</a></li>
  526. <li><a href="http://github.com/dduponchel">David Duponchel</a></li>
  527. <li><a href="http://yiminghe.javaeye.com">yiminghe</a></li>
  528. </ul>
  529. <h3>Contact and bugs</h3>
  530. <ul>
  531. <li><a href="https://github.com/Stuk/jszip/issues">Issue tracker</a></li>
  532. <li><a href="mailto:stuart -at- stuartk.com?subject=JSZip">Email me</a></li>
  533. </ul>
  534. </div>
  535. =======
  536. var content = zip.generate({type:"blob"});
  537. // see FileSaver.js
  538. saveAs(content, "example.zip");</textarea>
  539. <button id="demo" class="btn btn-primary">Run!</button>
  540. <span id="status"></span>
  541. >>>>>>> Rewrite the documentation
  542. </div>
  543. <div class="col-md-7">
  544. <h3>Installation</h3>
  545. <p>
  546. <strong>With npm</strong> : <code>npm install jszip</code>
  547. </p>
  548. <p>
  549. <strong>With bower</strong> : <code>bower install Stuk/jszip</code>
  550. </p>
  551. <p>
  552. <strong>With component</strong> : <code>component install Stuk/jszip</code>
  553. </p>
  554. <p>
  555. <strong>Manually</strong> : <a href="http://github.com/Stuk/jszip/zipball/master">download JSZip</a>
  556. and include the file <code>dist/jszip.js</code> or <code>dist/jszip.min.js</code>
  557. </p>
  558. <br>
  559. <p>
  560. Installed ? Great ! You can now check our
  561. <a href="{{site.baseurl}}/documentation/examples.html">guides and examples !</a>
  562. </p>
  563. </div>
  564. </div>
  565. <h3>Support</h3>
  566. <table class="browser_support">
  567. <tr>
  568. <th class="support_op">Opera</th>
  569. <th class="support_ff">Firefox</th>
  570. <th class="support_sf">Safari</th>
  571. <th class="support_cr">Chrome</th>
  572. <th class="support_ie">Internet Explorer</th>
  573. <th class="support_nd">Node.js</th>
  574. </tr>
  575. <tr>
  576. <td>Yes</td>
  577. <td>Yes</td>
  578. <td>Yes</td>
  579. <td>Yes</td>
  580. <td>Yes</td>
  581. <td>Yes</td>
  582. </tr>
  583. <tr>
  584. <td>Tested with the latest version</td>
  585. <td>Tested with 3.0 / 3.6 / latest version</td>
  586. <td>Tested with the latest version</td>
  587. <td>Tested with the latest version</td>
  588. <td>Tested with IE 6 / 7 / 8 / 9 / 10</td>
  589. <td>Tested with node.js 0.8 and 0.10</td>
  590. </tr>
  591. </table>
  592. <h3>Getting help</h3>
  593. <p>
  594. Having trouble ? We'd like to help !
  595. </p>
  596. <ul>
  597. <li>
  598. Try the <a href="{{site.baseurl}}/documentation/faq.html">FAQ</a>, it has
  599. answers to common questions.
  600. </li>
  601. <li>
  602. If you're looking for informations about a specific method, try the
  603. <a href="{{site.baseurl}}/documentation/api_jszip.html">documentation</a>.
  604. </li>
  605. <li>
  606. Check the
  607. <a href="{{site.baseurl}}/documentation/examples.html">examples</a>.
  608. </li>
  609. <li>
  610. Report bugs in our
  611. <a href="https://github.com/Stuk/jszip/issues">Bug tracker</a>.
  612. </li>
  613. </ul>
  614. <h3>Test status</h3>
  615. <dl class="dl-horizontal">
  616. <dt>Travis build :</dt>
  617. <dd>
  618. <a href="http://travis-ci.org/Stuk/jszip">
  619. <img src="https://secure.travis-ci.org/Stuk/jszip.png?branch=master" alt="" />
  620. </a>
  621. </dd>
  622. <dt>Saucelabs build :</dt>
  623. <dd>
  624. <a href="https://saucelabs.com/u/jszip">
  625. <img src="https://saucelabs.com/browser-matrix/jszip.svg" alt="" />
  626. </a>
  627. </dd>
  628. <dt>Live tests :</dt>
  629. <dd>
  630. <a href="{{site.baseurl}}/test/">See for yourself !</a>
  631. </dd>
  632. </dl>