Just keep calling zip.file().
Look at the example from their documentation page (comments mine):
var zip = new JSZip();
// Add a text file with the contents "Hello World\n"
zip.file("Hello.txt", "Hello World\n");
// Add a another text file with the contents "Goodbye, cruel world\n"
zip.file("Goodbye.txt", "Goodbye, cruel world\n");
// Add a folder named "images"
var img = zip.folder("images");
// Add a file named "smile.gif" to that folder, from some Base64 data
img.file("smile.gif", imgData, {base64: true});
zip.generateAsync({type:"base64"}).then(function (content) {
location.href="data:application/zip;base64," + content;
});
The important thing is to understand the code you've written - learn what each line does. If you do this, you'd realize that you just need to call zip.file() again to add another file.
Stuk
stuk.github.io › jszip
JSZip
JSZip is a javascript library for creating, reading and editing .zip files, with a lovely and simple API.
Changelog
Add sponsorship files. If you appreciate the time spent maintaining JSZip then I would really appreciate your sponsorship.
FAQ
Create .zip files using JavaScript. Provides a simple API to place any content generated by JavaScript into a .zip file for your users.
Upgrade guide
JSZip.compressions has been removed.
Stuk
stuk.github.io › jszip › documentation › examples.html
How to use JSZip
Create .zip files using JavaScript. Provides a simple API to place any content generated by JavaScript into a .zip file for your users.
Videos
03:21
Extract Zip Files With JavaScript JSZip | JavaScript Unzip Zip ...
02:52
Zip Files JavaScript | Create Zip From Uploaded Files With JavaScript ...
07:41
Javascript JSZip Example to Compress Multiple Files Using HTML5 ...
13:24
Easily Zip Files in React - YouTube
04:01
How to Create ZIP Files in Javascript Using JSZIP Library and ...
03:14
JSZip Jump Start - YouTube
GitHub
github.com › Stuk › jszip
GitHub - Stuk/jszip: Create, read and edit .zip files with Javascript · GitHub
A library for creating, reading and editing .zip files with JavaScript, with a lovely and simple API. See https://stuk.github.io/jszip for all the documentation.
Starred by 10.3K users
Forked by 1.3K users
Languages JavaScript 97.3% | HTML 2.7%
npm
npmjs.com › package › jszip
jszip - npm
August 2, 2022 - Create, read and edit .zip files with JavaScript http://stuartk.com/jszip. Latest version: 3.10.1, last published: 4 years ago. Start using jszip in your project by running `npm i jszip`. There are 6157 other projects in the npm registry using jszip.
» npm install jszip
Published Aug 02, 2022
Version 3.10.1
Author Stuart Knightley
Repository https://github.com/Stuk/jszip
Homepage https://github.com/Stuk/jszip#readme
Stuk
stuk.github.io › jszip › documentation › api_jszip › file_data.html
file(name, data [,options])
var zip = new JSZip(); // here, we have a correct (unicode) string zip.file("hello.txt", "unicode ♥", {binary: false}); // here, we have a binary string: it can contain binary content, one byte // per character.
Top answer 1 of 4
23
Just keep calling zip.file().
Look at the example from their documentation page (comments mine):
var zip = new JSZip();
// Add a text file with the contents "Hello World\n"
zip.file("Hello.txt", "Hello World\n");
// Add a another text file with the contents "Goodbye, cruel world\n"
zip.file("Goodbye.txt", "Goodbye, cruel world\n");
// Add a folder named "images"
var img = zip.folder("images");
// Add a file named "smile.gif" to that folder, from some Base64 data
img.file("smile.gif", imgData, {base64: true});
zip.generateAsync({type:"base64"}).then(function (content) {
location.href="data:application/zip;base64," + content;
});
The important thing is to understand the code you've written - learn what each line does. If you do this, you'd realize that you just need to call zip.file() again to add another file.
2 of 4
3
Adding to @Jonathon Reinhart answer,
You could also set both file name and path at the same time
// create a file and a folder
zip.file("nested/hello.txt", "Hello World\n");
// same as
zip.folder("nested").file("hello.txt", "Hello World\n");
Stuk
stuk.github.io › jszip › documentation › howto › read_zip.html
How to read a file
Here are two examples, one with the default http API, the other with request (but you’re free to use your favorite library !). If possible, download the file as a Buffer (you will get better performances). If it’s not possible, you can fallback to a binary string (the option is likely to be encoding : "binary"). "use strict"; var http = require("http"); var url = require("url"); var JSZip = require("jszip"); var req = http.get(url.parse("http://localhost/.../file.zip"), function (res) { if (res.statusCode !== 200) { console.log(res.statusCode); // handle error return; } var data = [], dataLen = 0; // don't set the encoding, it will break everything !
Stuk
stuk.github.io › jszip › documentation › examples › download-zip-file.html
Download the generated zip file
"use strict"; var zip = new JSZip(); zip.file("Hello.txt", "Hello world\n"); jQuery("#data_uri").on("click", function () { zip.generateAsync({type:"base64"}).then(function (base64) { window.location = "data:application/zip;base64," + base64; }, function (err) { jQuery("#data_uri").text(err); }); }); <p>Does not work in IE, has restrictions on the length.</p> <button id="data_uri" class="btn btn-primary">click to download</button>
Stuk
stuk.github.io › jszip › documentation › howto › write_zip.html
How to write a file / give it to the user
var fs = require("fs"); var JSZip = require("jszip"); var zip = new JSZip(); // zip.file("file", content); // ...
Stuk
stuk.github.io › jszip › documentation › api_jszip › file_name.html
file(name)
Returns : An instance of ZipObject representing the file if any, null otherwise. ... Throws : Nothing. var zip = new JSZip(); zip.file("file.txt", "content"); zip.file("file.txt").name // "file.txt" zip.file("file.txt").async("string") // a promise of "content" zip.file("file.txt").dir // false // utf8 example var zip = new JSZip(); zip.file("amount.txt", "€15"); zip.file("amount.txt").async("string") // a promise of "€15" zip.file("amount.txt").async("arraybuffer") // a promise of an ArrayBuffer containing €15 encoded as utf8 zip.file("amount.txt").async("uint8array") // a promise of an Uint8Array containing €15 encoded as utf8 // with folders zip.folder("sub").file("file.txt", "content"); zip.file("sub/file.txt"); // the file // or zip.folder("sub").file("file.txt") // the file
Stuk
stuk.github.io › jszip › documentation › api_jszip.html
JSZip API
Create .zip files using JavaScript. Provides a simple API to place any content generated by JavaScript into a .zip file for your users.
Heltschl
heltschl.org › _js › jszip
JSZip: JavaScript zip class
If set, specifies the file compression method to use. If not, the default file compression is used, cf generate(options). optimizedBinaryString (boolean), default false. Set it to true if (and only if) the input is a string and has already been prepared with a 0xFF mask. ... A JSZip object, for chaining.
Tabnine
tabnine.com › home page › code › javascript › jszip
jszip JavaScript and Node.js code examples | Tabnine
name: fileTemp, }) await zip.generateAsync({ //设置压缩格式,开始打包 type: "nodebuffer", //nodejs用 compression: "DEFLATE", //压缩算法 ... var zip = new JSZip(); if (options.xyzZipData) { zip.loadAsync(options.xyzZipData).then(function() { callback(null, geopackage, tileDao, zip); }); } else { fs.readFile(options.xyzZip, function(err, data) { zip.loadAsync(data).then(function() { callback(null, geopackage, tileDao, zip); });
Stuk
stuk.github.io › jszip › documentation › api_jszip › folder_name.html
folder(name)
Returns : A new JSZip (for chaining), with the new folder as root. ... zip.folder("images"); zip.folder("css").file("style.css", "body {background: #FF0000}"); // or specify an absolute path (using forward slashes) zip.file("css/font.css", "body {font-family: sans-serif}") // result : images/, css/, css/style.css, css/font.css
Stuk
stuk.github.io › jszip › documentation › api_jszip › load_async.html
loadAsync(data [, options])
require("fs").readFile("hello.zip", function (err, data) { if (err) throw err; var zip = new JSZip(); zip.loadAsync(data); } ... // here, "bin" is zip file containing: // file1.txt // folder1/file2.txt var zip = new JSZip(); zip.folder("subfolder").loadAsync(bin) .then(function (zip) { // "zip" is still in the "subfolder" folder console.log(zip.files); // subfolder/file1.txt // subfolder/folder1/file2.txt });
Stuk
stuk.github.io › jszip › documentation › api_jszip › generate_async.html
generateAsync(options[, onUpdate])
Note : if the entry is already compressed (coming from a compressed zip file), calling generateAsync() with a different compression level won’t update the entry. The reason is simple : JSZip doesn’t know how compressed the content was and how to match the compression level with the implementation we use.
Medium
medium.com › @joshmarinacci › a-little-fun-with-zip-files-4058812abf92
A little fun with zip files. I recently needed to generate a zip… | by Josh Marinacci | Medium
July 27, 2018 - I recently needed to generate a zip archive from several files that are on remote web-servers. More importantly, I needed to do this from within a webpage, and without blocking the UI thread. This looks like a job for promises! I found a great little library called JSZip to do the job.
Snyk
snyk.io › advisor › jszip › functions › jszip
How to use the jszip function in jszip | Snyk
function createZipFile(siteId, site, user, images, imagesData) { /* Creating the zip file */ const zip = new JSZip(); const www = zip.folder('www'); /* Generate index.html file */ www.file('index.html', indexHtml); /* Generate config.xml file */ const configParams = { appId: generateAppId(site.url), appName: site.name, siteURL: site.url, userEmail: user.email, userName: user.name, siteId, }; const xmlFile = generateConfigXML(configParams); www.file('config.xml', xmlFile);
Cjoshmartin
cjoshmartin.com › blog › creating-zip-files-with-javascript
Generating ZIP Files with Javascript - Blog - Josh Martin's Website
June 4, 2024 - 1async function GenerateZipDownload() { 2 const imageDownload = "https://unsplash.com/photos/two-people-in-scuba-gear-swimming-in-the-ocean-SuGTwrtPCg4"; 3 const file = await fetch(imageDownload).then(r => r.blob()); 4 5 const zip = new JSZip(); 6 zip.file(`filename.jpg`, file); // adds the image file to the zip file 7}