首页 > 解决方案 > 如何使用 adm-zip 编辑 zip 文件中的内容?

问题描述

我想更新 zip 文件中的内容(我是网络开发新手)。我正在使用nodejs、adm-zip 和cheerio。

zip 文件类似于:

file.zip

   |--a.xhtml

   |--b.xhtml

我想要做的是编辑 a.xhtml 的内容,比如 <div><div class="example">

我目前正在尝试使用 fs.readFile 然后使用 fs.writeFile

但似乎我需要提供路径但 fs 无法读取 zip。

所以我很困惑我该怎么办?

这是我当前的代码:

fs.readFile('/path/file.zip' ,'utf8' , function(err,data){
if (err) {throw err;}

let $ = cheerio.load(data,{xmlMode:true, decodeEntities: false})
$("div").addClass("example")
    fs.writeFile('/path/file.zip', $.xml(), 'utf-8', function (err) {
        if (err) {throw err;}
        console.log('complete');
      });

});

标签: node.jsxhtmlfscheerioadm-zip

解决方案


推荐阅读