Search code examples
javascriptjqueryjszip

Add files to an existing zip archive via JSZip


I have a zip file which is a Visual Basic project. What I'm trying to see is if I can add my web app as a directory inside this zip archive so I can easily export my web apps as native windows apps

I tried the Ajax method from the documentation and it worked fine on my hard drive when loaded in Firefox, but it's not calling when from my website!

$(document).ready(function() {
  $(".download").on("click", function() {
    JSZipUtils.getBinaryContent('YourWinApp.zip', function(err, data) {
      if(err) {
        throw err; // or handle err
      }

      var zip = new JSZip(data);
      zip.file("Hello.txt", "Hello World\n");
      var folder = zip.folder("images");
      folder.file("folder.txt", "I'm a file in a new folder");
      var content = zip.generate({type:"blob"});
      // see FileSaver.js
      saveAs(content, "example.zip");
    });

  });
});

Solution

  • I tried the Ajax method from the documentation and it worked perfectly! (I didn't link to the right file lol)

    $(document).ready(function() {
      $(".download").on("click", function() {
        JSZipUtils.getBinaryContent('YourWindowsApp.zip', function(err, data) {
          if(err) {
            throw err; // or handle err
          }
    
          var zip = new JSZip(data);
          zip.file("Hello.txt", "Hello World\n");
          var folder = zip.folder("images");
          folder.file("folder.txt", "I'm a file in a new folder");
          var content = zip.generate({type:"blob"});
          // see FileSaver.js
          saveAs(content, "example.zip");
        });
    
      });
    });