📜 ⬆️ ⬇️

JSZip Create .zip files

A very good and easy way to give a client several files in a zip archive. Without loading the already loaded server.
var zip = new JSZip(); zip.add("Hello.txt", "Hello World\n"); img = zip.folder("images"); img.add("smile.gif", imgData, {base64: true}); content = zip.generate(); location.href="data:application/zip;base64,"+content; 

So let's see what is happening here.
A copy of our zip archive, JSZip class, is created.
Then we can add any data to it, say Hello.txt, you can also add the images folder.
Next, put in it smile.gif, wrap it all up, and give it to you as a zip file.

Supported Browsers


OperaFirefoxSafariChrome
7.5+ Filename "default.zip"3.0+ File name set of alphabet characters with the extension ".part"Filename "Unknown" (I have downloaded) without extensionFile name "download.zip"

I think about IE, I say nothing :)

Documentation



new JSZip ([compressionMethod])

Constructor.
')
compressionMethod, string. The compression method used in the .zip file.
Available methods.
"STORE" without compression, by default.
"DEFLATE" standard zip compression, jszip-deflate.js file is needed
 var zip = new JSZip(); //    new JSZip("STORE"); var zip = new JSZip("DEFLATE"); 


add (name, data [, options])

Add the file to our zip archive. Supports chaining

Options
base64 , boolean. Set to true if data is encoded in base64. For example, a picture from the canvas.
binary , boolean. Default is true if the data is in base64, otherwise false
date , date. Use to set the date the file was last modified. Otherwise the current time will be used.

 zip.add("Hello.txt", "Hello World\n"); zip.add("smile.gif", "R0lGODdhBQAFAIACAAAAAP/eACwAAAAABQAFAAACCIwPkWerClIBADs=", {base64: true}); zip.add("magic.txt", "U2VjcmV0IGNvZGU=", {base64: true, binary: false}); zip.add("Xmas.txt", "Ho ho ho !", {date : new Date("December 25, 2007 00:00:01")}); zip.add("animals.txt", "dog,platypus\n").add("people.txt", "james,sebastian\n"); 

Result: Hello.txt, smile.gif, magic.txt, Xmas.txt, animals.txt, people.txt

folder (name)

Add directory to zip archive. 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

find (needle)

Compare the string or regular expression with all the file names and return an object with information for each match.
 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: An array with all matches of the form: {name: “Readme”, data: “Hello World!”, Dir: false}

remove (name)

Delete file or folder
<source lang = "javascript>
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

generate (asBytes = false)

Generates a zip archive. By default, encoded in base64, pass true to get the nude 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 } 


Website: jszip.stuartk.co.uk
Github: github.com/Stuk/jszip



Material prepared in the editor "Hands & Keyboard"

Source: https://habr.com/ru/post/133129/


All Articles