Dowling1dow
Dowling1dow

Reputation: 57

Editing a file within a zipped file using JSZip

Using JSZip, is there a way to edit a file within a zipped file?

I've tried looking for solutions and going through the API but I can't seem to find a solution.

Any help with this would be great! Thanks in advance!

Upvotes: 3

Views: 3144

Answers (2)

Henry Luo
Henry Luo

Reputation: 404

You can refer to the official documentation.

And here's a more complete Node.js example:

var fs = require("fs");
var JSZip = require("jszip");

async function zipDemo() {
    // read the existing zip file
    var zipData = fs.readFileSync("input.zip");
    var zip = await JSZip.loadAsync(zipData);
    // add a new JSON file to the zip
    zip.file("sample.json", JSON.stringify({demo:123}));
    // write out the updated zip
    zip.generateNodeStream({type:'nodebuffer', streamFiles:true})
    .pipe(fs.createWriteStream('output.zip'))
    .on('finish', function () {
        console.log("output`enter code here`.zip written.");
    });
}

zipDemo();

Upvotes: 0

Ivan Drinchev
Ivan Drinchev

Reputation: 19581

You can edit a file inside your zip with .file method.

zip.file("existing_filename", "new file content");

This method is used for adding and updating file content.

Just make sure the file already exist.

You can read more about it in the documentation.

Upvotes: 7

Related Questions