Opera | Firefox | Safari | Chrome | Internet Explorer |
---|---|---|---|---|
7.5+ | 3.0+ | Yes | No | No |
Filename is "default.zip" | Filename is random alphanumeric with ".part" extension | Filename is "Unknown" (no extension) | Appears to only support data URLs for some content | Only supports data URLs for some content. (May be able to use MHTML?) |
Tests still need to be run on historical browsers to check which versions are supported.
The constructor
zip = new JSZip();
Add a file to the zip file. Supports chaining.
Options:
base64
, boolean. Set to true
if the data
is base64 encoded. For example image data from a <canvas>
element.
Plain text does not need this option.zip.add("Hello.txt", "Hello World\n"); zip.add("smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true}); zip.add("animals.txt", "dog,platypus\n").add("people.txt", "james,sebastian\n");
Result: Hello.txt, smile.gif, animals.txt, people.txt
Add a directory to the zip file. Supports chaining.
zip.folder("images"); zip.folder("css").add("style.css", "body {background: #FF0000}"); // or specify an absolute path (using forward slashes) zip.add("css/font.css", "body {font-family: sans-serif}")
Result: images/, css/, css/style.css, css/font.css
Compare a string or regular expression against all of the filenames and returns an informational object for each that matches.
zip.add("Readme", "Hello World!\n"); zip.add("Readme.French", "Bonjour tout le monde!\n"); zip.add("Readme.Pirate", "Ahoy m'hearty!\n"); zip.find("Readme"); // only finds "Readme" zip.find(/^Readme/); // Regular expression finds all three
Result: Array of matched file objects in the form:
{name: "Readme", data: "Hello World!", dir: false}
Delete a file. (Todo: make recursive on folders)
zip.add("Hello.txt", "Hello World\n"); zip.add("temp.txt", "nothing").remove("temp.txt");
Result: Hello.txt
zip.add("Hello.txt", "Hello World\n"); zip.folder("css").add("style.css", "body {background: #FF0000}"); zip.remove("Hello.txt").remove("css");
Result: Empty zip.
Generates the complete zip file. By default encoded as base64, pass true
to get the raw byte string
content = zip.generate(); location.href="data:application/zip;base64,"+content;
content = zip.generate(true); for (var c = 0; c < content.length; c++) { console.log(content.charCodeAt(c)); // do other things }
GPLv3 and/or MIT
Date()
to hex)add()
options object