Samuel
Samuel

Reputation: 135

How to zip multiple zip files in Javascript?

Problem

I want to generate a ZIP file that contains multiples other ZIP files in Javascript. I use JSZIP but I don't manage to add ZIP files into one ZIP file.

Example

I have multiple text files :

I want to generate that ZIP file :

Thank you for your help.

Upvotes: 1

Views: 2289

Answers (2)

Touffy
Touffy

Reputation: 6561

You could do this easily with client-zip, by piping the output of multiple downloadZip calls to another's input :

import { downloadZip } from "https://cdn.jsdelivr.net/npm/client-zip/index.js"

/* assuming you have all the files in variables p1f1, p1f2, etc. */

const player1zip = downloadZip([
  { name: 'Player 1 - file 1.txt', input: p1f1 },
  { name: 'Player 1 - file 2.txt', input: p1f2 }
])
const player2zip = downloadZip([
  { name: 'Player 2 - file 1.txt', input: p2f1 },
  { name: 'Player 2 - file 2.txt', input: p2f2 }
])
const allPlayersZip = downloadZip([
  { name: 'Player 1.zip', input: player1zip },
  { name: 'Player 2.zip', input: player2zip }
])

// this part is just boilerplate for downloading the result :

const link = document.createElement("a")
link.href = URL.createObjectURL(await allPlayersZip.blob())
link.download = "test.zip"
link.click()
link.remove()
URL.revokeObjectURL(link.href)

Upvotes: 0

Michael Schwartz
Michael Schwartz

Reputation: 8415

Fiddle: https://mikethedj4.github.io/kodeWeave/editor/#ca2d1692722e8f6c321c322cd33ed246

After many hours and failed attempts I finally got it to work with JSZip!

NOTE: I'm using JSZip v2.6.0 which is currently outdated and will not work with the current version which is 3.0 as time of posting.

JavaScript:

// Set Sample URL
document.getElementById("zipurl").value = "https://mikethedj4.github.io/kodeWeave/editor/zips/font-awesome.zip";

$(".loadzipurl").on("click", function() {
  if ( (!document.getElementById("zipurl").value) ) {
    // Do nothing
    alertify.error("Unable to perform operation as value is blank!");
  } else {
    if ( (document.getElementById("zipurl").value.toLowerCase().substring(0,7) === "http://" ) || (document.getElementById("zipurl").value.toLowerCase().substring(0,8) === "https://") ) {
      JSZipUtils.getBinaryContent(document.getElementById("zipurl").value, function(error, repoFiles) {
        if(error) {
          throw error // or handle err
        }

        var webAppZipBinary = repoFiles;

        // Download as Windows App
        JSZipUtils.getBinaryContent("https://mikethedj4.github.io/kodeWeave/editor/zips/YourLinApp.zip", function(err, data) {
          if(err) {
            throw err // or handle err
          }

          alertify.message("Creating application!");
          var zip = new JSZip();
          zip.load(data);

          // Your Web Application
          zip.folder("HELLOMOMMY/").load(webAppZipBinary);

          // For 32bit Windows Application
          zip.file("package.json", '{\n  "main"  : "index.html",\n  "name"  : "test",\n  "window": {\n      "toolbar" : false,\n      "icon"    : "app/icons/128.png",\n      "width"   : 1000,\n      "height"  : 600,\n      "position": "center"\n  }\n}');
          zip.file("index.html", '<!doctype html>\n<html>\n <head>\n    <title>test</title>\n    <style>\n      iframe {\n        position: absolute;\n        top: 0;\n        left: 0;\n        width: 100%;\n        height: 100%;\n        overflow: visible;\n        border: 0;\n      }\n    </style>\n  </head>\n <body>\n    <iframe src="app/index.html"></iframe>\n  </body>\n</html>');

          // Export your application
          var content = zip.generate({type:"blob"});
          saveAs(content, "test-win.zip");
          return false;
        });
      });
    } else {
      alertify.error("Error! \"http://\" and \"https://\" urls are only supported!");
    }
  }
});

HTML:

<input type="text" id="zipurl" placeholder="http://">
<button class="loadzipurl">Export Application</button>

Upvotes: 1

Related Questions