node.js - 如何使用 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');
});
});
解决方案
推荐阅读
- javascript - React 组件没有根据异步调用的响应进行更新
- python - 在多个地方分割字符串Python
- javascript - 如何知道我在 Azure 认知服务语音合成 (TTS) 中使用了多少个字符?
- c# - .net core 3.1 分离模型之间的关系
- node.js - 剧作家拖放
- java - Spring Data JPA 规范 FETCH JOIN 问题
- react-native - 如何使用 Jest 测试 Modal (react-native) 是否可见?
- angular - 订阅和模板离子的问题
- mysql - Maria DB - 为 SQL 事件中的每一行执行存储过程
- python - How do I open an FTP directory so that I can use it the same way I could a normal directory?