zlib.zipFiles(files[, options])


稳定性: 1.0 - 早期开发

ZIP 压缩档案 API 是实验性的。第一次使用它的任何部分(包括这个函数)都会发出实验性警告;仅仅导入 node:zlib 不会。

🌐 The ZIP archive API is experimental. Using any part of it (this function among them) emits an experimental warning the first time; merely importing node:zlib does not.

  • files <Iterable> [sourcePath, entryName] 字符串对。任何可迭代对象都可以——数组、Map、Object.entries() 的结果、生成器。
  • options <string> | <Object>
    • followSymlinks <boolean> 解析符号链接并归档它所指向的文件,而不是存储链接本身。默认值: true。
    • comment <string> 一个归档评论;字符串 options 是 { comment: options } 的简写。
    • baseOffset <number> 见 zlib.createZipArchive()。
  • 返回:<stream.Readable> 由 <Buffer> 个块组成的序列化归档。

从磁盘上的文件构建一个档案。对于每个 [sourcePath, entryName] 对,它会读取 sourcePath 并添加一个名为 entryName 的条目,同时捕获文件的 Unix 模式和修改时间。目录会变成一个目录条目;普通文件的内容会以流的方式写入(作为 zlib.ZipEntry.createStream() 条目),不会在内存中缓冲。目录内容不会递归遍历——列出你想包含的每个路径。

🌐 Builds an archive from files on disk. For each [sourcePath, entryName] pair it reads sourcePath and adds an entry named entryName, capturing the file's Unix mode and modification time. A directory becomes a directory entry; a regular file's contents are streamed in (as a zlib.ZipEntry.createStream() entry) without being buffered in memory. Directory contents are not walked recursively — list each path you want included.

当 followSymlinks 是 true(默认值)时,符号链接会被解析并作为其目标文件进行归档;当它是 false 时,链接本身会被存储为一个符号链接条目,其内容是目标路径(见 zlib.ZipEntry.createSymlink())。

🌐 When followSymlinks is true (the default) a symbolic link is resolved and archived as its target file; when it is false the link itself is stored as a symbolic-link entry whose content is the target path (see zlib.ZipEntry.createSymlink()).

import { zipFiles } from 'node:zlib';
import { createWriteStream } from 'node:fs';
import { pipeline } from 'node:stream/promises';

await pipeline(
  zipFiles([
    ['/data/report.pdf', 'report.pdf'],
    ['/data/notes.txt', 'docs/notes.txt'],
  ]),
  createWriteStream('archive.zip'),
);