jugglervr
jugglervr

Reputation: 11

JSZip won't load a zip from a base64 string?

So I'm trying to write a small JSON object to the URL as a user changes items on a page, and also allow the URL to be read to let the user pick up where they left off.

I'm able to generate the zip fine with JSZip, but I'm unable to figure out how to later open the zip from that string. Here's the code I was working with. zip.file has elements, but I don't know how to read the base64 string back to zip2 to be able to open it.

var figures = [{
    "qty": 1,
    "name": "",
    "level": 1,
    "defense": 1,
    "melee": 3,
    "ranged": 1,
    "abilities": [
      "c02","c12","c22","c32","t12"
    ]
  },{
    "qty": 1,
    "name": "",
    "level": 1,
    "defense": 2,
    "melee": 1,
    "ranged": 1,
    "abilities": [
      "c02","c12","c22","c32","t45"
    ]
  },{
    "qty": 1,
    "name": "",
    "level": 4,
    "defense": 1,
    "melee": 1,
    "ranged": 5,
    "abilities": [
      "c01","c14","c23","c35"
    ]
  }]
var zip = new JSZip()
zip.file = figures
var urlString = zip.generate({type:"base64"})
location.href="#"+ urlString
console.log(urlString)
console.log(zip)
var zip2 = new JSZip()
zip2.load(urlString,{"base64": true})
console.log(zip2)

Upvotes: 0

Views: 5613

Answers (1)

David Duponchel
David Duponchel

Reputation: 4069

JSZip doesn't use all attributes on its instance, you need to use the file() method:

var zip = new JSZip();
zip.file("figures.json", JSON.stringify(figures));
var urlString = zip.generate({type:"base64"});

var zip2 = new JSZip(urlString,{"base64": true});
console.log(zip2.file("figures.json").asText());

Upvotes: 1

Related Questions