首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在nodejs中重命名zip存档中的文件

在nodejs中重命名zip存档中的文件
EN

Stack Overflow用户
提问于 2016-06-09 16:42:27
回答 2查看 4K关注 0票数 3

我正在编写一个nodejs脚本,它应该执行以下操作:

  1. 下载压缩文件
  2. 删除zip文件的顶层目录(将所有文件向上移动一个文件夹)
  3. 上传新的zip文件

因为zip文件相当大,所以我想重命名(或移动)这些文件,而不需要解压缩和重新压缩文件。

这有可能吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-28 07:57:03

是的,可以使用像adm-zip这样的库

代码语言:javascript
复制
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 

算法

创建一个newZip对象以暂时保存内存中的文件,读取下载的zip中的所有条目。每一项

  1. 读fileName。这包括路径。
  2. 使用fileName读取文件内容
  3. 删除顶层目录名以获取newFileName
  4. 将步骤2中的fileContent添加到newZip中,给它步骤3中的newFileName
  5. 最后,将newZip写到磁盘上,为它提供一个新的zipName

希望这有帮助

票数 4
EN

Stack Overflow用户

发布于 2021-05-20 09:08:11

您可以使用具有异步承诺风格的伟大jszip库。

代码语言:javascript
复制
import jszip from 'jszip';
import fs from 'fs';

/**
 * Move/rename entire directory tree within a zip.
 * @param {*} zipFilePath The original zip file
 * @param {*} modifiedZipFilePath The path where palace the modified zip 
 * @param {*} originalDir The original directory to change
 * @param {*} destinationDir The new directory to move to.
 */
async function moveDirectory(zipFilePath, modifiedZipFilePath, originalDir, destinationDir) {

    // Read zip file bits buffer
    const zipFileBuffer = await fs.promises.readFile(zipFilePath);
    // Load jszip instance
    const zipFile = await jszip.loadAsync(zipFileBuffer);
    // Get the original directory entry
    const originalDirContent = zipFile.folder(originalDir);
    // Walk on all directory tree
    originalDirContent.forEach((path, entry) => {
        // If it's a directory entry ignore it.
        if (entry.dir) {
            return;
        }
        // Extract the file path within the directory tree 
        const internalDir = path.split(originalDir)[0];
        // Build the new file directory in the new tree 
        const newFileDir = `${destinationDir}/${internalDir}`;
        // Put the file in the new tree, with the same properties
        zipFile.file(newFileDir, entry.nodeStream(), { 
            createFolders: true,
            unixPermissions: entry.unixPermissions,
            comment: entry.comment,
            date: entry.date,
        });
    });
    // After all files copied to the new tree, remove the original directory tree.
    zipFile.remove(originalDir);
    // Generate the new zip buffer
    const modifiedZipBuffer = await zipFile.generateAsync({ type: 'nodebuffer' });
    // Save the buffer as a new zip file
    await fs.promises.writeFile(modifiedZipFilePath, modifiedZipBuffer);
}

moveDirectory('archive.zip', 'modified.zip', 'some-dir/from-dir', 'some-other-dir/to-dir');

这只是在所有原始目录树条目上行走,并将它们放在新的目录树中。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37731780

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档