我正在编写一个nodejs脚本,它应该执行以下操作:
由于 zip 文件相当大,我想重命名(或移动)文件而不解压缩和重新压缩文件。
那可能吗?
是的,可以使用像adm-zip这样的库
var AdmZip = require('adm-zip');
//create a zip object to hold the new zip files
var newZip = new AdmZip();
// reading archives
var zip = new AdmZip('somePath/download.zip');
var zipEntries = zip.getEntries(); // an array of ZipEntry records
zipEntries.forEach(function(zipEntry) {
var fileName = zipEntry.entryName;
var fileContent = zip.readAsText(fileName)
//Here remove the top level directory
var newFileName = fileName.substring(fileName.indexOf("/") + 1);
newZip.addFile(newFileName, fileContent, '', 0644 << 16);
});
newZip.writeZip('somePath/upload.zip'); //write the new zip
Run Code Online (Sandbox Code Playgroud)
算法
创建一个 newZip 对象来临时将文件保存在内存中 读取下载的 zip 中的所有条目。对于每个条目
希望有帮助