On this page

    M

    zlib.zipFiles

    History
    zlib.zipFiles(files, options?): stream.Readable
    Stability: 1.0Early development

    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.

    Attributes
    files:Iterable
    of [sourcePath, entryName] string pairs. Any iterable works — an array, a Map, the result of Object.entries(), a generator.
    options:string | Object
    followSymlinks?:boolean
    Resolve a symbolic link and archive the file it points to, rather than storing the link itself. Default: true.
    comment:string
    An archive comment; a string options is shorthand for { comment: options }.
    of Buffer chunks making up the serialized archive.

    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.

    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'),
    );