最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Editing a file within a zipped file using JSZip - Stack Overflow

programmeradmin3浏览0评论

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!

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!

Share Improve this question asked Nov 23, 2016 at 20:53 Dowling1dowDowling1dow 571 silver badge6 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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.

You can refer to the official documentation.

And here's a more plete 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();
发布评论

评论列表(0)

  1. 暂无评论