有没有办法在 JSZip 中重命名文件?

问题描述 投票:0回答:1

我在 JSZip 中有一个文件(或文件夹)。我需要重命名/移动它。

const zip = new JSZip();
zip.file("file.txt", "File Content");
zip.rename("file.txt", "newname.txt"); // zip.rename doesn't exist

zip.rename
不存在,但是有类似的功能吗?我能找到的唯一方法是这个答案,但感觉这个任务太简单了,不需要那么多代码,而且可能有更好的方法来做到这一点。如果重要的话,代码是在浏览器中运行,而不是在 node.js 中运行。

javascript zip jszip
1个回答
0
投票

JSZip.prototype.renameAsync = async function rename(oldName, newName) { const old = this.file(oldName) || this.folder(oldName); const type = old.file ? "folder" : "file"; if (type == "file") { const content = await old.async("uint8array"); this.remove(old.name); this.file(newName, content); } else if (type == "folder") { const newFolder = this.folder(newName); old.forEach(async function(name, file) { const content = await file.async("uint8array"); old.remove(name); newFolder.file(name, content); }); this.remove(oldName); } }

还是很大,但我认为更好了。

© www.soinside.com 2019 - 2024. All rights reserved.