Node.js v24.13.0 文档


Zlib#>

源代码: lib/zlib.js

node:zlib 模块提供了使用 Gzip、Deflate/Inflate、Brotli 和 Zstd 实现的压缩功能。

【The node:zlib module provides compression functionality implemented using Gzip, Deflate/Inflate, Brotli, and Zstd.】

要访问它:

【To access it:】

import zlib from 'node:zlib';const zlib = require('node:zlib');

压缩和解压缩是围绕 Node.js 流 API 构建的。

【Compression and decompression are built around the Node.js Streams API.】

通过将源流通过 zlibTransform 流传入目标流,可以实现对流(例如文件)的压缩或解压缩:

【Compressing or decompressing a stream (such as a file) can be accomplished by piping the source stream through a zlib Transform stream into a destination stream:】

import {
  createReadStream,
  createWriteStream,
} from 'node:fs';
import process from 'node:process';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream';

const gzip = createGzip();
const source = createReadStream('input.txt');
const destination = createWriteStream('input.txt.gz');

pipeline(source, gzip, destination, (err) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
});const {
  createReadStream,
  createWriteStream,
} = require('node:fs');
const process = require('node:process');
const { createGzip } = require('node:zlib');
const { pipeline } = require('node:stream');

const gzip = createGzip();
const source = createReadStream('input.txt');
const destination = createWriteStream('input.txt.gz');

pipeline(source, gzip, destination, (err) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
});

或者,使用 promise pipeline API:

【Or, using the promise pipeline API:】

import {
  createReadStream,
  createWriteStream,
} from 'node:fs';
import { createGzip } from 'node:zlib';
import { pipeline } from 'node:stream/promises';

async function do_gzip(input, output) {
  const gzip = createGzip();
  const source = createReadStream(input);
  const destination = createWriteStream(output);
  await pipeline(source, gzip, destination);
}

await do_gzip('input.txt', 'input.txt.gz');const {
  createReadStream,
  createWriteStream,
} = require('node:fs');
const process = require('node:process');
const { createGzip } = require('node:zlib');
const { pipeline } = require('node:stream/promises');

async function do_gzip(input, output) {
  const gzip = createGzip();
  const source = createReadStream(input);
  const destination = createWriteStream(output);
  await pipeline(source, gzip, destination);
}

do_gzip('input.txt', 'input.txt.gz')
  .catch((err) => {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  });

也可以一步压缩或解压缩数据:

【It is also possible to compress or decompress data in a single step:】

import process from 'node:process';
import { Buffer } from 'node:buffer';
import { deflate, unzip } from 'node:zlib';

const input = '.................................';
deflate(input, (err, buffer) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
  console.log(buffer.toString('base64'));
});

const buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64');
unzip(buffer, (err, buffer) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
  console.log(buffer.toString());
});

// Or, Promisified

import { promisify } from 'node:util';
const do_unzip = promisify(unzip);

const unzippedBuffer = await do_unzip(buffer);
console.log(unzippedBuffer.toString());const { deflate, unzip } = require('node:zlib');

const input = '.................................';
deflate(input, (err, buffer) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
  console.log(buffer.toString('base64'));
});

const buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64');
unzip(buffer, (err, buffer) => {
  if (err) {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  }
  console.log(buffer.toString());
});

// Or, Promisified

const { promisify } = require('node:util');
const do_unzip = promisify(unzip);

do_unzip(buffer)
  .then((buf) => console.log(buf.toString()))
  .catch((err) => {
    console.error('An error occurred:', err);
    process.exitCode = 1;
  });

线程池使用和性能注意事项#>

【Threadpool usage and performance considerations】

除了那些明确为同步的 API 之外,所有 zlib API 都使用 Node.js 内部线程池。这在某些应用中可能会导致意想不到的效果和性能限制。

【All zlib APIs, except those that are explicitly synchronous, use the Node.js internal threadpool. This can lead to surprising effects and performance limitations in some applications.】

同时创建和使用大量 zlib 对象可能导致显著的内存碎片化。

【Creating and using a large number of zlib objects simultaneously can cause significant memory fragmentation.】

import zlib from 'node:zlib';
import { Buffer } from 'node:buffer';

const payload = Buffer.from('This is some data');

// WARNING: DO NOT DO THIS!
for (let i = 0; i < 30000; ++i) {
  zlib.deflate(payload, (err, buffer) => {});
}const zlib = require('node:zlib');

const payload = Buffer.from('This is some data');

// WARNING: DO NOT DO THIS!
for (let i = 0; i < 30000; ++i) {
  zlib.deflate(payload, (err, buffer) => {});
}

在前面的例子中,同时创建了 30,000 个 deflate 实例。由于某些操作系统处理内存分配和释放的方式,这可能导致严重的内存碎片问题。

【In the preceding example, 30,000 deflate instances are created concurrently. Because of how some operating systems handle memory allocation and deallocation, this may lead to significant memory fragmentation.】

强烈建议将压缩操作的结果进行缓存,以避免重复工作。

【It is strongly recommended that the results of compression operations be cached to avoid duplication of effort.】

压缩 HTTP 请求和响应#>

【Compressing HTTP requests and responses】

node:zlib 模块可用于实现对 HTTP 定义的 gzipdeflatebrzstd 内容编码机制的支持。

【The node:zlib module can be used to implement support for the gzip, deflate, br, and zstd content-encoding mechanisms defined by HTTP.】

HTTP Accept-Encoding 头用于 HTTP 请求中,以识别客户端接受的压缩编码。Content-Encoding 头用于识别实际应用于消息的压缩编码。

【The HTTP Accept-Encoding header is used within an HTTP request to identify the compression encodings accepted by the client. The Content-Encoding header is used to identify the compression encodings actually applied to a message.】

下面给出的示例被大幅简化,以展示基本概念。使用 zlib 编码可能成本较高,结果应当进行缓存。有关使用 zlib 时涉及的速度/内存/压缩权衡的更多信息,请参见 内存使用调整

【The examples given below are drastically simplified to show the basic concept. Using zlib encoding can be expensive, and the results ought to be cached. See Memory usage tuning for more information on the speed/memory/compression tradeoffs involved in zlib usage.】

// Client request example
import fs from 'node:fs';
import zlib from 'node:zlib';
import http from 'node:http';
import process from 'node:process';
import { pipeline } from 'node:stream';

const request = http.get({ host: 'example.com',
                           path: '/',
                           port: 80,
                           headers: { 'Accept-Encoding': 'br,gzip,deflate' } });
request.on('response', (response) => {
  const output = fs.createWriteStream('example.com_index.html');

  const onError = (err) => {
    if (err) {
      console.error('An error occurred:', err);
      process.exitCode = 1;
    }
  };

  switch (response.headers['content-encoding']) {
    case 'br':
      pipeline(response, zlib.createBrotliDecompress(), output, onError);
      break;
    // Or, just use zlib.createUnzip() to handle both of the following cases:
    case 'gzip':
      pipeline(response, zlib.createGunzip(), output, onError);
      break;
    case 'deflate':
      pipeline(response, zlib.createInflate(), output, onError);
      break;
    default:
      pipeline(response, output, onError);
      break;
  }
});// Client request example
const zlib = require('node:zlib');
const http = require('node:http');
const fs = require('node:fs');
const { pipeline } = require('node:stream');

const request = http.get({ host: 'example.com',
                           path: '/',
                           port: 80,
                           headers: { 'Accept-Encoding': 'br,gzip,deflate,zstd' } });
request.on('response', (response) => {
  const output = fs.createWriteStream('example.com_index.html');

  const onError = (err) => {
    if (err) {
      console.error('An error occurred:', err);
      process.exitCode = 1;
    }
  };

  switch (response.headers['content-encoding']) {
    case 'br':
      pipeline(response, zlib.createBrotliDecompress(), output, onError);
      break;
    // Or, just use zlib.createUnzip() to handle both of the following cases:
    case 'gzip':
      pipeline(response, zlib.createGunzip(), output, onError);
      break;
    case 'deflate':
      pipeline(response, zlib.createInflate(), output, onError);
      break;
    case 'zstd':
      pipeline(response, zlib.createZstdDecompress(), output, onError);
      break;
    default:
      pipeline(response, output, onError);
      break;
  }
});
// server example
// Running a gzip operation on every request is quite expensive.
// It would be much more efficient to cache the compressed buffer.
import zlib from 'node:zlib';
import http from 'node:http';
import fs from 'node:fs';
import { pipeline } from 'node:stream';

http.createServer((request, response) => {
  const raw = fs.createReadStream('index.html');
  // Store both a compressed and an uncompressed version of the resource.
  response.setHeader('Vary', 'Accept-Encoding');
  const acceptEncoding = request.headers['accept-encoding'] || '';

  const onError = (err) => {
    if (err) {
      // If an error occurs, there's not much we can do because
      // the server has already sent the 200 response code and
      // some amount of data has already been sent to the client.
      // The best we can do is terminate the response immediately
      // and log the error.
      response.end();
      console.error('An error occurred:', err);
    }
  };

  // Note: This is not a conformant accept-encoding parser.
  // See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
  if (/\bdeflate\b/.test(acceptEncoding)) {
    response.writeHead(200, { 'Content-Encoding': 'deflate' });
    pipeline(raw, zlib.createDeflate(), response, onError);
  } else if (/\bgzip\b/.test(acceptEncoding)) {
    response.writeHead(200, { 'Content-Encoding': 'gzip' });
    pipeline(raw, zlib.createGzip(), response, onError);
  } else if (/\bbr\b/.test(acceptEncoding)) {
    response.writeHead(200, { 'Content-Encoding': 'br' });
    pipeline(raw, zlib.createBrotliCompress(), response, onError);
  } else {
    response.writeHead(200, {});
    pipeline(raw, response, onError);
  }
}).listen(1337);// server example
// Running a gzip operation on every request is quite expensive.
// It would be much more efficient to cache the compressed buffer.
const zlib = require('node:zlib');
const http = require('node:http');
const fs = require('node:fs');
const { pipeline } = require('node:stream');

http.createServer((request, response) => {
  const raw = fs.createReadStream('index.html');
  // Store both a compressed and an uncompressed version of the resource.
  response.setHeader('Vary', 'Accept-Encoding');
  const acceptEncoding = request.headers['accept-encoding'] || '';

  const onError = (err) => {
    if (err) {
      // If an error occurs, there's not much we can do because
      // the server has already sent the 200 response code and
      // some amount of data has already been sent to the client.
      // The best we can do is terminate the response immediately
      // and log the error.
      response.end();
      console.error('An error occurred:', err);
    }
  };

  // Note: This is not a conformant accept-encoding parser.
  // See https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.3
  if (/\bdeflate\b/.test(acceptEncoding)) {
    response.writeHead(200, { 'Content-Encoding': 'deflate' });
    pipeline(raw, zlib.createDeflate(), response, onError);
  } else if (/\bgzip\b/.test(acceptEncoding)) {
    response.writeHead(200, { 'Content-Encoding': 'gzip' });
    pipeline(raw, zlib.createGzip(), response, onError);
  } else if (/\bbr\b/.test(acceptEncoding)) {
    response.writeHead(200, { 'Content-Encoding': 'br' });
    pipeline(raw, zlib.createBrotliCompress(), response, onError);
  } else if (/\bzstd\b/.test(acceptEncoding)) {
    response.writeHead(200, { 'Content-Encoding': 'zstd' });
    pipeline(raw, zlib.createZstdCompress(), response, onError);
  } else {
    response.writeHead(200, {});
    pipeline(raw, response, onError);
  }
}).listen(1337);

默认情况下,zlib 方法在解压截断数据时会抛出错误。然而,如果已知数据不完整,或者只希望检查压缩文件的开头部分,则可以通过更改用于解压最后一块输入数据的刷新方法来抑制默认的错误处理:

【By default, the zlib methods will throw an error when decompressing truncated data. However, if it is known that the data is incomplete, or the desire is to inspect only the beginning of a compressed file, it is possible to suppress the default error handling by changing the flushing method that is used to decompress the last chunk of input data:】

// This is a truncated version of the buffer from the above examples
const buffer = Buffer.from('eJzT0yMA', 'base64');

zlib.unzip(
  buffer,
  // For Brotli, the equivalent is zlib.constants.BROTLI_OPERATION_FLUSH.
  // For Zstd, the equivalent is zlib.constants.ZSTD_e_flush.
  { finishFlush: zlib.constants.Z_SYNC_FLUSH },
  (err, buffer) => {
    if (err) {
      console.error('An error occurred:', err);
      process.exitCode = 1;
    }
    console.log(buffer.toString());
  }); 

这不会改变其他抛出错误的情况的行为,例如,当输入数据格式无效时。使用此方法,将无法判断输入是提前结束还是缺少完整性检查,因此需要手动检查解压后的结果是否有效。

【This will not change the behavior in other error-throwing situations, e.g. when the input data has an invalid format. Using this method, it will not be possible to determine whether the input ended prematurely or lacks the integrity checks, making it necessary to manually check that the decompressed result is valid.】

内存使用调整#>

【Memory usage tuning】

对于基于 zlib 的流#>

【For zlib-based streams】

来自 zlib/zconf.h,为 Node.js 使用而修改:

【From zlib/zconf.h, modified for Node.js usage:】

deflate 的内存要求是(以字节为单位):

【The memory requirements for deflate are (in bytes):】

(1 << (windowBits + 2)) + (1 << (memLevel + 9)) 

也就是说:128K 用于 windowBits = 15 + 128K 用于 memLevel = 8(默认值),再加上几千字节用于小对象。

【That is: 128K for windowBits = 15 + 128K for memLevel = 8 (default values) plus a few kilobytes for small objects.】

例如,要将默认内存需求从 256K 降低到 128K,应将选项设置为:

【For example, to reduce the default memory requirements from 256K to 128K, the options should be set to:】

const options = { windowBits: 14, memLevel: 7 }; 

然而,这通常会降低压缩性能。

【This will, however, generally degrade compression.】

inflate 的内存需求(以字节为单位)为 1 << windowBits。也就是说,对于 windowBits = 15(默认值),需要 32K,再加上一些用于小对象的少量内存。

【The memory requirements for inflate are (in bytes) 1 << windowBits. That is, 32K for windowBits = 15 (default value) plus a few kilobytes for small objects.】

这还包括一个大小为 chunkSize 的单一内部输出缓冲区,默认大小为16K。

【This is in addition to a single internal output slab buffer of size chunkSize, which defaults to 16K.】

zlib 压缩的速度最显著地受 level 设置影响。较高的压缩级别会带来更好的压缩效果,但完成的时间会更长。较低的压缩级别会导致压缩效果较差,但速度会快得多。

【The speed of zlib compression is affected most dramatically by the level setting. A higher level will result in better compression, but will take longer to complete. A lower level will result in less compression, but will be much faster.】

一般来说,较高的内存使用选项意味着 Node.js 调用 zlib 的次数会更少,因为它能够在每次 write 操作中处理更多的数据。因此,这是影响速度的另一个因素,但代价是增加了内存使用。

【In general, greater memory usage options will mean that Node.js has to make fewer calls to zlib because it will be able to process more data on each write operation. So, this is another factor that affects the speed, at the cost of memory usage.】

对于基于 Brotli 的流#>

【For Brotli-based streams】

对于基于 Brotli 的流,也存在与 zlib 选项相对应的选项,尽管这些选项的范围与 zlib 的不同:

【There are equivalents to the zlib options for Brotli-based streams, although these options have different ranges than the zlib ones:】

  • zlib 的 level 选项与 Brotli 的 BROTLI_PARAM_QUALITY 选项对应。
  • zlib 的 windowBits 选项与 Brotli 的 BROTLI_PARAM_LGWIN 选项相对应。

有关 Brotli 特定选项的更多详情,请参见 下面

【See below for more details on Brotli-specific options.】

对于基于 Zstd 的流#>

【For Zstd-based streams】

稳定性: 1 - 实验性

对于基于 Zstd 的流,也存在与 zlib 选项相对应的选项,尽管这些选项的范围与 zlib 的不同:

【There are equivalents to the zlib options for Zstd-based streams, although these options have different ranges than the zlib ones:】

  • zlib 的 level 选项与 Zstd 的 ZSTD_c_compressionLevel 选项对应。
  • zlib 的 windowBits 选项对应 Zstd 的 ZSTD_c_windowLog 选项。

有关 Zstd 特定选项的更多详细信息,请参见 下面

【See below for more details on Zstd-specific options.】

刷新#>

【Flushing】

在压缩流上调用 .flush() 会使 zlib 返回当前尽可能多的输出。这可能会以压缩质量下降为代价,但在需要尽快获取数据时非常有用。

【Calling .flush() on a compression stream will make zlib return as much output as currently possible. This may come at the cost of degraded compression quality, but can be useful when data needs to be available as soon as possible.】

在以下示例中,使用 flush() 将压缩的部分 HTTP 响应写入客户端:

【In the following example, flush() is used to write a compressed partial HTTP response to the client:】

import zlib from 'node:zlib';
import http from 'node:http';
import { pipeline } from 'node:stream';

http.createServer((request, response) => {
  // For the sake of simplicity, the Accept-Encoding checks are omitted.
  response.writeHead(200, { 'content-encoding': 'gzip' });
  const output = zlib.createGzip();
  let i;

  pipeline(output, response, (err) => {
    if (err) {
      // If an error occurs, there's not much we can do because
      // the server has already sent the 200 response code and
      // some amount of data has already been sent to the client.
      // The best we can do is terminate the response immediately
      // and log the error.
      clearInterval(i);
      response.end();
      console.error('An error occurred:', err);
    }
  });

  i = setInterval(() => {
    output.write(`The current time is ${Date()}\n`, () => {
      // The data has been passed to zlib, but the compression algorithm may
      // have decided to buffer the data for more efficient compression.
      // Calling .flush() will make the data available as soon as the client
      // is ready to receive it.
      output.flush();
    });
  }, 1000);
}).listen(1337);const zlib = require('node:zlib');
const http = require('node:http');
const { pipeline } = require('node:stream');

http.createServer((request, response) => {
  // For the sake of simplicity, the Accept-Encoding checks are omitted.
  response.writeHead(200, { 'content-encoding': 'gzip' });
  const output = zlib.createGzip();
  let i;

  pipeline(output, response, (err) => {
    if (err) {
      // If an error occurs, there's not much we can do because
      // the server has already sent the 200 response code and
      // some amount of data has already been sent to the client.
      // The best we can do is terminate the response immediately
      // and log the error.
      clearInterval(i);
      response.end();
      console.error('An error occurred:', err);
    }
  });

  i = setInterval(() => {
    output.write(`The current time is ${Date()}\n`, () => {
      // The data has been passed to zlib, but the compression algorithm may
      // have decided to buffer the data for more efficient compression.
      // Calling .flush() will make the data available as soon as the client
      // is ready to receive it.
      output.flush();
    });
  }, 1000);
}).listen(1337);

常量#>

【Constants】

zlib 常量#>

【zlib constants】

zlib.h 中定义的所有常量也都在 require('node:zlib').constants 中定义。在正常操作过程中,通常不需要使用这些常量。它们的文档是为了让它们的存在不令人惊讶。该部分几乎直接摘自 zlib 文档

【All of the constants defined in zlib.h are also defined on require('node:zlib').constants. In the normal course of operations, it will not be necessary to use these constants. They are documented so that their presence is not surprising. This section is taken almost directly from the zlib documentation.】

以前,这些常量可以直接从 require('node:zlib') 获取,例如 zlib.Z_NO_FLUSH。目前仍然可以直接从模块访问这些常量,但这种做法已被弃用。

【Previously, the constants were available directly from require('node:zlib'), for instance zlib.Z_NO_FLUSH. Accessing the constants directly from the module is currently still possible but is deprecated.】

允许的刷新值。

【Allowed flush values.】

  • zlib.constants.Z_NO_FLUSH
  • zlib.constants.Z_PARTIAL_FLUSH
  • zlib.constants.Z_SYNC_FLUSH
  • zlib.constants.Z_FULL_FLUSH
  • zlib.constants.Z_FINISH
  • zlib.constants.Z_BLOCK

压缩/解压缩函数的返回码。负值表示错误,正值表示特殊但正常的事件。

【Return codes for the compression/decompression functions. Negative values are errors, positive values are used for special but normal events.】

  • zlib.constants.Z_OK
  • zlib.constants.Z_STREAM_END
  • zlib.constants.Z_NEED_DICT
  • zlib.constants.Z_ERRNO
  • zlib.constants.Z_STREAM_ERROR
  • zlib.constants.Z_DATA_ERROR
  • zlib.constants.Z_MEM_ERROR
  • zlib.constants.Z_BUF_ERROR
  • zlib.constants.Z_VERSION_ERROR

压缩级别。

【Compression levels.】

  • zlib.constants.Z_NO_COMPRESSION
  • zlib.constants.Z_BEST_SPEED
  • zlib.constants.Z_BEST_COMPRESSION
  • zlib.constants.Z_DEFAULT_COMPRESSION

压缩策略。

【Compression strategy.】

  • zlib.constants.Z_FILTERED
  • zlib.constants.Z_HUFFMAN_ONLY
  • zlib.constants.Z_RLE
  • zlib.constants.Z_FIXED
  • zlib.constants.Z_DEFAULT_STRATEGY

Brotli 常量#>

【Brotli constants】

对于基于 Brotli 的流,有几种选项和其他常量可用:

【There are several options and other constants available for Brotli-based streams:】

刷新操作#>

【Flush operations】

以下值是基于 Brotli 的流的有效刷新操作:

【The following values are valid flush operations for Brotli-based streams:】

  • zlib.constants.BROTLI_OPERATION_PROCESS(所有操作的默认值)
  • zlib.constants.BROTLI_OPERATION_FLUSH(调用 .flush() 时的默认值)
  • zlib.constants.BROTLI_OPERATION_FINISH(最后一块的默认值)
  • zlib.constants.BROTLI_OPERATION_EMIT_METADATA
    • 在 Node.js 环境中,这个特定操作可能很难使用,因为流式层使得很难知道哪些数据最终会出现在这个帧中。此外,目前没有通过 Node.js API 消费这些数据的方法。

压缩器选项#>

【Compressor options】

Brotli 编码器可以设置多个选项,这些选项会影响压缩效率和速度。键和值都可以作为 zlib.constants 对象的属性进行访问。

【There are several options that can be set on Brotli encoders, affecting compression efficiency and speed. Both the keys and the values can be accessed as properties of the zlib.constants object.】

最重要的选项是:

【The most important options are:】

  • BROTLI_PARAM_MODE
    • BROTLI_MODE_GENERIC(默认)
    • BROTLI_MODE_TEXT,为 UTF-8 文本调整
    • BROTLI_MODE_FONT,为 WOFF 2.0 字体进行了调整
  • BROTLI_PARAM_QUALITY
    • 范围从 BROTLI_MIN_QUALITYBROTLI_MAX_QUALITY,默认值为 BROTLI_DEFAULT_QUALITY
  • BROTLI_PARAM_SIZE_HINT
    • 整数值,表示预期的输入大小;对于未知的输入大小,默认为 0

可以设置以下标志,以便对压缩算法和内存使用进行高级控制:

【The following flags can be set for advanced control over the compression algorithm and memory usage tuning:】

  • BROTLI_PARAM_LGWIN
    • 范围从 BROTLI_MIN_WINDOW_BITSBROTLI_MAX_WINDOW_BITS,默认值为 BROTLI_DEFAULT_WINDOW,如果设置了 BROTLI_PARAM_LARGE_WINDOW 标志,则最大可达 BROTLI_LARGE_MAX_WINDOW_BITS
  • BROTLI_PARAM_LGBLOCK
    • 范围从 BROTLI_MIN_INPUT_BLOCK_BITSBROTLI_MAX_INPUT_BLOCK_BITS
  • BROTLI_PARAM_DISABLE_LITERAL_CONTEXT_MODELING
    • 布尔标志,用于减少压缩比以提高解压速度。
  • BROTLI_PARAM_LARGE_WINDOW
    • 布尔标志,用于启用“Large Window Brotli”模式(与RFC 7932标准化的Brotli格式不兼容)。
  • BROTLI_PARAM_NPOSTFIX
    • 范围从 0BROTLI_MAX_NPOSTFIX
  • BROTLI_PARAM_NDIRECT
    • 范围从 015 << NPOSTFIX,步长为 1 << NPOSTFIX

解压选项#>

【Decompressor options】

这些高级选项可用于控制减压:

【These advanced options are available for controlling decompression:】

  • BROTLI_DECODER_PARAM_DISABLE_RING_BUFFER_REALLOCATION
    • 影响内部内存分配模式的布尔标志。
  • BROTLI_DECODER_PARAM_LARGE_WINDOW
    • 布尔标志,用于启用“Large Window Brotli”模式(与RFC 7932标准化的Brotli格式不兼容)。

Zstd 常量#>

【Zstd constants】

稳定性: 1 - 实验性

对于基于 Zstd 的流,有几种选项和其他常量可用:

【There are several options and other constants available for Zstd-based streams:】

刷新操作#>

【Flush operations】

以下值是基于 Zstd 的流的有效刷新操作:

【The following values are valid flush operations for Zstd-based streams:】

  • zlib.constants.ZSTD_e_continue(所有操作的默认值)
  • zlib.constants.ZSTD_e_flush(调用 .flush() 时的默认值)
  • zlib.constants.ZSTD_e_end(最后一个数据块的默认值)

压缩器选项#>

【Compressor options】

Zstd 编码器可以设置多个选项,这些选项会影响压缩效率和速度。键和值都可以作为 zlib.constants 对象的属性来访问。

【There are several options that can be set on Zstd encoders, affecting compression efficiency and speed. Both the keys and the values can be accessed as properties of the zlib.constants object.】

最重要的选项是:

【The most important options are:】

  • ZSTD_c_compressionLevel
    • 根据预定义的 cLevel 表设置压缩参数。默认级别是 ZSTD_CLEVEL_DEFAULT==3。
  • ZSTD_c_strategy
    • 选择压缩策略。
    • 可能的取值列在下面的策略选项部分。

策略选项#>

【Strategy options】

以下常量可以用作 ZSTD_c_strategy 参数的值:

【The following constants can be used as values for the ZSTD_c_strategy parameter:】

  • zlib.constants.ZSTD_fast
  • zlib.constants.ZSTD_dfast
  • zlib.constants.ZSTD_greedy
  • zlib.constants.ZSTD_lazy
  • zlib.constants.ZSTD_lazy2
  • zlib.constants.ZSTD_btlazy2
  • zlib.constants.ZSTD_btopt
  • zlib.constants.ZSTD_btultra
  • zlib.constants.ZSTD_btultra2

示例:

【Example:】

const stream = zlib.createZstdCompress({
  params: {
    [zlib.constants.ZSTD_c_strategy]: zlib.constants.ZSTD_btultra,
  },
}); 

保证源大小#>

【Pledged Source Size】

可以通过 opts.pledgedSrcSize 指定未压缩输入的预期总大小。如果输入结束时大小不匹配,压缩将以代码 ZSTD_error_srcSize_wrong 失败。

【It's possible to specify the expected total size of the uncompressed input via opts.pledgedSrcSize. If the size doesn't match at the end of the input, compression will fail with the code ZSTD_error_srcSize_wrong.】

解压选项#>

【Decompressor options】

这些高级选项可用于控制减压:

【These advanced options are available for controlling decompression:】

  • ZSTD_d_windowLogMax
    • 选择一个大小限制(以 2 的幂为单位),超过该限制时,流式 API 将拒绝分配内存缓冲区,以保护主机免受不合理的内存需求影响。

类:Options#>

【Class: Options

每个基于 zlib 的类都接受一个 options 对象。无需提供选项。

【Each zlib-based class takes an options object. No options are required.】

有些选项仅在压缩时相关,解压类会忽略这些选项。

【Some options are only relevant when compressing and are ignored by the decompression classes.】

查看更多信息,请参阅 deflateInit2inflateInit2 文档。

【See the deflateInit2 and inflateInit2 documentation for more information.】

类:BrotliOptions#>

【Class: BrotliOptions

每个基于 Brotli 的类都接受一个 options 对象。所有选项都是可选的。

【Each Brotli-based class takes an options object. All options are optional.】

例如:

【For example:】

const stream = zlib.createBrotliCompress({
  chunkSize: 32 * 1024,
  params: {
    [zlib.constants.BROTLI_PARAM_MODE]: zlib.constants.BROTLI_MODE_TEXT,
    [zlib.constants.BROTLI_PARAM_QUALITY]: 4,
    [zlib.constants.BROTLI_PARAM_SIZE_HINT]: fs.statSync(inputFile).size,
  },
}); 

类:zlib.BrotliCompress#>

【Class: zlib.BrotliCompress

使用 Brotli 算法压缩数据。

【Compress data using the Brotli algorithm.】

类:zlib.BrotliDecompress#>

【Class: zlib.BrotliDecompress

使用 Brotli 算法解压缩数据。

【Decompress data using the Brotli algorithm.】

类:zlib.Deflate#>

【Class: zlib.Deflate

使用 deflate 压缩数据。

【Compress data using deflate.】

类:zlib.DeflateRaw#>

【Class: zlib.DeflateRaw

使用 deflate 压缩数据,并且不要附加 zlib 头。

【Compress data using deflate, and do not append a zlib header.】

类:zlib.Gunzip#>

【Class: zlib.Gunzip

解压缩 gzip 流。

【Decompress a gzip stream.】

类:zlib.Gzip#>

【Class: zlib.Gzip

使用 gzip 压缩数据。

【Compress data using gzip.】

类:zlib.Inflate#>

【Class: zlib.Inflate

解压缩 deflate 流。

【Decompress a deflate stream.】

类:zlib.InflateRaw#>

【Class: zlib.InflateRaw

解压缩原始的 deflate 流。

【Decompress a raw deflate stream.】

类:zlib.Unzip#>

【Class: zlib.Unzip

通过自动检测头部来解压 Gzip 或 Deflate 压缩的流。

【Decompress either a Gzip- or Deflate-compressed stream by auto-detecting the header.】

类:zlib.ZlibBase#>

【Class: zlib.ZlibBase

不是由 node:zlib 模块导出的。之所以在这里有文档说明,是因为它是压缩器/解压器类的基类。

【Not exported by the node:zlib module. It is documented here because it is the base class of the compressor/decompressor classes.】

这个类继承自 stream.Transform,允许 node:zlib 对象在管道和类似的流操作中使用。

【This class inherits from stream.Transform, allowing node:zlib objects to be used in pipes and similar stream operations.】

zlib.bytesWritten#>

zlib.bytesWritten 属性指定写入引擎的字节数,在这些字节被处理(压缩或解压缩,根据派生类的不同而适用)之前。

【The zlib.bytesWritten property specifies the number of bytes written to the engine, before the bytes are processed (compressed or decompressed, as appropriate for the derived class).】

zlib.close([callback])#>

关闭底层句柄。

【Close the underlying handle.】

zlib.flush([kind, ]callback)#>

  • kind 默认值: 对于基于 zlib 的流,zlib.constants.Z_FULL_FLUSH;对于基于 Brotli 的流,zlib.constants.BROTLI_OPERATION_FLUSH
  • callback <Function>

刷新挂起的数据。不要轻率调用此操作,过早刷新会对压缩算法的效果产生负面影响。

【Flush pending data. Don't call this frivolously, premature flushes negatively impact the effectiveness of the compression algorithm.】

调用这个方法只会将数据从内部的 zlib 状态中刷新出来,并不会在流级别上执行任何类型的刷新。它的行为更像是正常的 .write() 调用,即它会排在其他待处理写操作的队列中,只有在从流中读取数据时才会产生输出。

【Calling this only flushes data from the internal zlib state, and does not perform flushing of any kind on the streams level. Rather, it behaves like a normal call to .write(), i.e. it will be queued up behind other pending writes and will only produce output when data is being read from the stream.】

zlib.params(level, strategy, callback)#>

此函数仅适用于基于 zlib 的流,即不适用于 Brotli。

【This function is only available for zlib-based streams, i.e. not Brotli.】

动态更新压缩级别和压缩策略。 仅适用于 deflate 算法。

【Dynamically update the compression level and compression strategy. Only applicable to deflate algorithm.】

zlib.reset()#>

将压缩器/解压器重置为出厂默认设置。仅适用于 inflate 和 deflate 算法。

【Reset the compressor/decompressor to factory defaults. Only applicable to the inflate and deflate algorithms.】

类:ZstdOptions#>

【Class: ZstdOptions

稳定性: 1 - 实验性

每个基于 Zstd 的类都接受一个 options 对象。所有选项都是可选的。

【Each Zstd-based class takes an options object. All options are optional.】

  • flush <integer> 默认值: zlib.constants.ZSTD_e_continue
  • finishFlush <integer> 默认值: zlib.constants.ZSTD_e_end
  • chunkSize <integer> 默认值: 16 * 1024
  • params <Object> 包含索引 Zstd 参数 的键值对象。
  • maxOutputLength <integer> 在使用 便捷方法 时限制输出大小。默认值: buffer.kMaxLength
  • info <boolean> 如果为 true,返回一个包含 bufferengine 的对象。默认值: false
  • dictionary <Buffer> 可选字典,用于在压缩或解压缩与字典具有共同模式的数据时提高压缩效率。

例如:

【For example:】

const stream = zlib.createZstdCompress({
  chunkSize: 32 * 1024,
  params: {
    [zlib.constants.ZSTD_c_compressionLevel]: 10,
    [zlib.constants.ZSTD_c_checksumFlag]: 1,
  },
}); 

类:zlib.ZstdCompress#>

【Class: zlib.ZstdCompress

稳定性: 1 - 实验性

使用 Zstd 算法压缩数据。

【Compress data using the Zstd algorithm.】

类:zlib.ZstdDecompress#>

【Class: zlib.ZstdDecompress

稳定性: 1 - 实验性

使用 Zstd 算法解压缩数据。

【Decompress data using the Zstd algorithm.】

zlib.constants#>

提供枚举 Zlib 相关常量的对象。

【Provides an object enumerating Zlib-related constants.】

zlib.crc32(data[, value])#>

  • data <string> | <Buffer> | <TypedArray> | <DataView>data 是字符串时,它将在用于计算之前被编码为 UTF-8。
  • value <integer> 可选的起始值。它必须是一个32位无符号整数。默认值: 0
  • 返回:<integer> 一个包含校验和的 32 位无符号整数。

计算 data 的 32 位 循环冗余校验 校验和。如果指定了 value,则将其用作校验和的起始值,否则使用 0 作为起始值。

【Computes a 32-bit Cyclic Redundancy Check checksum of data. If value is specified, it is used as the starting value of the checksum, otherwise, 0 is used as the starting value.】

CRC 算法旨在计算校验和并检测数据传输中的错误。它不适用于密码学身份验证。

【The CRC algorithm is designed to compute checksums and to detect error in data transmission. It's not suitable for cryptographic authentication.】

为了与其他 API 保持一致,如果 data 是字符串,它将在用于计算之前以 UTF-8 编码。如果用户仅使用 Node.js 来计算和匹配校验和,这与默认使用 UTF-8 编码的其他 API 可以很好地配合。

【To be consistent with other APIs, if the data is a string, it will be encoded with UTF-8 before being used for computation. If users only use Node.js to compute and match the checksums, this works well with other APIs that uses the UTF-8 encoding by default.】

一些第三方 JavaScript 库会基于 str.charCodeAt() 对字符串计算校验和,以便在浏览器中运行。 如果用户希望匹配在浏览器中由此类库计算的校验和,最好在 Node.js 中使用相同的库(如果它也可以在 Node.js 中运行)。 如果用户必须使用 zlib.crc32() 来匹配由此类第三方库生成的校验和:

【Some third-party JavaScript libraries compute the checksum on a string based on str.charCodeAt() so that it can be run in browsers. If users want to match the checksum computed with this kind of library in the browser, it's better to use the same library in Node.js if it also runs in Node.js. If users have to use zlib.crc32() to match the checksum produced by such a third-party library:】

  1. 如果库接受 Uint8Array 作为输入,请在浏览器中使用 TextEncoder 将字符串编码为 UTF-8 的 Uint8Array,并在浏览器中基于 UTF-8 编码的字符串计算校验和。
  2. 如果库只接受字符串并基于 str.charCodeAt() 计算数据,那么在 Node.js 端,将字符串使用 Buffer.from(str, 'utf16le') 转换为缓冲区。
import zlib from 'node:zlib';
import { Buffer } from 'node:buffer';

let crc = zlib.crc32('hello');  // 907060870
crc = zlib.crc32('world', crc);  // 4192936109

crc = zlib.crc32(Buffer.from('hello', 'utf16le'));  // 1427272415
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc);  // 4150509955const zlib = require('node:zlib');
const { Buffer } = require('node:buffer');

let crc = zlib.crc32('hello');  // 907060870
crc = zlib.crc32('world', crc);  // 4192936109

crc = zlib.crc32(Buffer.from('hello', 'utf16le'));  // 1427272415
crc = zlib.crc32(Buffer.from('world', 'utf16le'), crc);  // 4150509955

zlib.createBrotliCompress([options])#>

创建并返回一个新的 BrotliCompress 对象。

【Creates and returns a new BrotliCompress object.】

zlib.createBrotliDecompress([options])#>

创建并返回一个新的 BrotliDecompress 对象。

【Creates and returns a new BrotliDecompress object.】

zlib.createDeflate([options])#>

创建并返回一个新的 Deflate 对象。

【Creates and returns a new Deflate object.】

zlib.createDeflateRaw([options])#>

创建并返回一个新的 DeflateRaw 对象。

【Creates and returns a new DeflateRaw object.】

将 zlib 从 1.2.8 升级到 1.2.11 后,当 windowBits 为原始 deflate 流设置为 8 时,其行为发生了变化。zlib 会在最初设置为 8 时自动将 windowBits 设置为 9。较新的 zlib 版本会抛出异常,因此 Node.js 恢复了将 8 的值升级为 9 的原始行为,因为将 windowBits = 9 传递给 zlib 实际上会生成一个有效只使用 8 位窗口的压缩流。

【An upgrade of zlib from 1.2.8 to 1.2.11 changed behavior when windowBits is set to 8 for raw deflate streams. zlib would automatically set windowBits to 9 if was initially set to 8. Newer versions of zlib will throw an exception, so Node.js restored the original behavior of upgrading a value of 8 to 9, since passing windowBits = 9 to zlib actually results in a compressed stream that effectively uses an 8-bit window only.】

zlib.createGunzip([options])#>

创建并返回一个新的 Gunzip 对象。

【Creates and returns a new Gunzip object.】

zlib.createGzip([options])#>

创建并返回一个新的 Gzip 对象。请参阅 例子

【Creates and returns a new Gzip object. See example.】

zlib.createInflate([options])#>

创建并返回一个新的 Inflate 对象。

【Creates and returns a new Inflate object.】

zlib.createInflateRaw([options])#>

创建并返回一个新的 InflateRaw 对象。

【Creates and returns a new InflateRaw object.】

zlib.createUnzip([options])#>

创建并返回一个新的 Unzip 对象。

【Creates and returns a new Unzip object.】

zlib.createZstdCompress([options])#>

稳定性: 1 - 实验性

  • options zstd options

创建并返回一个新的 ZstdCompress 对象。

【Creates and returns a new ZstdCompress object.】

zlib.createZstdDecompress([options])#>

稳定性: 1 - 实验性

  • options zstd options

创建并返回一个新的 ZstdDecompress 对象。

【Creates and returns a new ZstdDecompress object.】

便捷方法#>

【Convenience methods】

所有这些都将 <Buffer><TypedArray><DataView><ArrayBuffer> 或字符串作为第一个参数,可选的第二个参数用于向 zlib 类提供选项,并将使用 callback(error, result) 调用提供的回调函数。

【All of these take a <Buffer>, <TypedArray>, <DataView>, <ArrayBuffer>, or string as the first argument, an optional second argument to supply options to the zlib classes and will call the supplied callback with callback(error, result).】

每个方法都有一个 *Sync 对应方法,它们接受相同的参数,但不需要回调。

【Every method has a *Sync counterpart, which accept the same arguments, but without a callback.】

zlib.brotliCompress(buffer[, options], callback)#>

zlib.brotliCompressSync(buffer[, options])#>

使用 BrotliCompress 压缩一块数据。

【Compress a chunk of data with BrotliCompress.】

zlib.brotliDecompress(buffer[, options], callback)#>

zlib.brotliDecompressSync(buffer[, options])#>

使用 BrotliDecompress 解压一块数据。

【Decompress a chunk of data with BrotliDecompress.】

zlib.deflate(buffer[, options], callback)#>

zlib.deflateSync(buffer[, options])#>

使用 Deflate 压缩一块数据。

【Compress a chunk of data with Deflate.】

zlib.deflateRaw(buffer[, options], callback)#>

zlib.deflateRawSync(buffer[, options])#>

使用 DeflateRaw 压缩一块数据。

【Compress a chunk of data with DeflateRaw.】

zlib.gunzip(buffer[, options], callback)#>

zlib.gunzipSync(buffer[, options])#>

使用 Gunzip 解压一块数据。

【Decompress a chunk of data with Gunzip.】

zlib.gzip(buffer[, options], callback)#>

zlib.gzipSync(buffer[, options])#>

使用 Gzip 压缩一块数据。

【Compress a chunk of data with Gzip.】

zlib.inflate(buffer[, options], callback)#>

zlib.inflateSync(buffer[, options])#>

使用 Inflate 解压一块数据。

【Decompress a chunk of data with Inflate.】

zlib.inflateRaw(buffer[, options], callback)#>

zlib.inflateRawSync(buffer[, options])#>

使用 InflateRaw 解压一块数据。

【Decompress a chunk of data with InflateRaw.】

zlib.unzip(buffer[, options], callback)#>

zlib.unzipSync(buffer[, options])#>

使用 Unzip 解压一块数据。

【Decompress a chunk of data with Unzip.】

zlib.zstdCompress(buffer[, options], callback)#>

稳定性: 1 - 实验性

zlib.zstdCompressSync(buffer[, options])#>

稳定性: 1 - 实验性

使用 ZstdCompress 压缩一块数据。

【Compress a chunk of data with ZstdCompress.】

zlib.zstdDecompress(buffer[, options], callback)#>

zlib.zstdDecompressSync(buffer[, options])#>

稳定性: 1 - 实验性

使用 ZstdDecompress 解压一块数据。

【Decompress a chunk of data with ZstdDecompress.】

Node.js 中文网 - 粤ICP备13048890号