javascript - 如何在 Javascript 中压缩多个 zip 文件?

标签 javascript zip jszip

问题

我想生成一个 ZIP 文件,其中包含 Javascript 中的多个其他 ZIP 文件。我使用 JSZIP,但无法将 ZIP 文件添加到一个 ZIP 文件中。

示例

我有多个文本文件:

  • 玩家 1 - 文件 1.txt
  • 玩家 1 - 文件 2.txt
  • 玩家 2 - 文件 1.txt
  • 玩家 2 - 文件 2.txt

我想生成该 ZIP 文件:

  • 示例.zip
    • 玩家 1.zip
      • 玩家 1 - 文件 1.txt
      • 玩家 1 - 文件 2.txt
    • 玩家 2.zip
      • 玩家 2 - 文件 1.txt
      • 玩家 2 - 文件 2.txt

感谢您的帮助。

最佳答案

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

经过几个小时和失败的尝试,我终于让它与 JSZip 一起工作。 !

注意:我使用的是 JSZip v2.6.0,该版本目前已过时,无法与发布时的当前版本 3.0 配合使用。

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>

关于javascript - 如何在 Javascript 中压缩多个 zip 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32696101/

相关文章:

java - 如何将 Android Studio 项目导出为 zip 文件?

javascript - 将包含时间的字符串转换为 24 小时时间我可以用 - jQuery 做数学

javascript - 如何根据经纬度计算距离

javascript - 正确从 javascript 关闭 iframe 并且不隐藏 iframe

.net-core - 通过休息端点将压缩文件动态传输到客户端

node.js - 使用 Meteor 从 Github Assets API 下载文件时出错

javascript - 如何使用 javascript 将参数从 JSP 传递到 Servlet

javascript - 如何处理来自 Ajax POST 请求的多个文件响应?

javascript - 在浏览器中通过 Javascript 在 iOS 上保存 Zip 文件

javascript - FileSaver saveAs 在 IE11 中不起作用