类:ZipProvider
🌐 Class: ZipProvider
一个提供者,通过 VFS API 暴露 ZIP 压缩文件的条目 —— 无论是 zlib.ZipBuffer(内存中)还是 zlib.ZipFile(磁盘上)。provider.readonly 反映了压缩文件自身的 zipFile.writable 标志:ZipBuffer 总是可写的,而 ZipFile 只有在用 writable: true 打开时才可写。
🌐 A provider that exposes the entries of a ZIP archive - either a
zlib.ZipBuffer (in memory) or a zlib.ZipFile (on disk) - through
the VFS API. provider.readonly reflects the archive's own
zipFile.writable flag: a ZipBuffer is always writable, and a
ZipFile is writable only when opened with { writable: true }.
目录既可以显式识别(名称以 / 结尾的条目),也可以隐式识别(任何以 "<dir>/" 开头的条目名称),并且可以通过 readdir() 列出,包括使用 { recursive: true },无论哪种方式。因为 ZIP 成员不能就地编辑或读取——只能完全写入或完全解压——所以文件在仅用于写入时,只有在句柄关闭时,其内容才会提交(作为新的归档条目)。
每个方法都有一个同步对应方法(openSync()、statSync()、readdirSync() 等等),由同样完整的同步界面 zlib.ZipBuffer/zlib.ZipFile 提供支持。像那些方法一样,这里的同步方法会阻塞 Node.js 事件循环和后续的 JavaScript 执行,直到操作完成——包括任何压缩/解压的过程。
🌐 Every method has a synchronous counterpart (openSync(), statSync(),
readdirSync(), and so on), backed by the equally complete synchronous
surface zlib.ZipBuffer/zlib.ZipFile expose. As with those, the
synchronous methods here block the Node.js event loop and further JavaScript
execution until the operation - including any deflate/inflate pass -
completes.
const vfs = require('node:vfs');
const zlib = require('node:zlib');
const { readFileSync } = require('node:fs');
async function main() {
const zip = new zlib.ZipBuffer(readFileSync('archive.zip'));
const archiveVfs = vfs.create(new vfs.ZipProvider(zip));
console.log(await archiveVfs.promises.readdir('/'));
await archiveVfs.promises.writeFile('/new.txt', 'hello');
}
main();