Перейти до вмісту

Довідник API node:zlib

Deflate, Gzip, Brotli, Zstandard і CRC32

На цій сторінці
Open in ChatGPTOpen in Claude

node:zlib підтримує API з функціями зворотного виклику, синхронні та потокові API.

СімействоПідтримка
DeflateФункції стиснення/розпакування з обгорткою та без неї, варіанти Sync, класи й допоміжні засоби create*
GzipGzip, gunzip, unzip, варіанти Sync, класи й допоміжні засоби create*
BrotliФункції стиснення/розпакування, класи й допоміжні засоби create*
ZstandardФункції стиснення/розпакування, класи й допоміжні засоби create*
Утилітиcrc32, constants і codes
const zlib = require("node:zlib");
const input = Buffer.from("hello, zlib!");

const gzip = zlib.gzipSync(input);
zlib.gunzipSync(gzip).toString(); // "hello, zlib!"

const zstd = zlib.zstdCompressSync(input);
zlib.zstdDecompressSync(zstd).toString(); // "hello, zlib!"

Форми з функціями зворотного виклику отримують (error, result); синхронні форми повертають Buffer або кидають виняток.

node:zlib/iter

Специфічний для Elide модуль ітераторів надає фабрики перетворень для gzip, deflate, raw deflate, Brotli і Zstandard. Фабрика повертає об’єкт із .transform(asyncIterable); вивід надходить пакетами фрагментів байтів.

import { gzip } from "node:zlib/iter";

async function* input() {
  yield new TextEncoder().encode("hello");
}

for await (const batch of gzip().transform(input())) {
  for (const chunk of batch) {
    // compressed bytes
  }
}

Із Web Streams використовуйте Compression Streams.

Див. також

Reference

Status: Partial · Not measured · since 1.1.0

Exports

ExportKindStatusNotes
deflateSync()functionFullCompress data using the deflate algorithm (RFC 1951 wrapped in zlib format RFC 1950)
inflateSync()functionFullDecompress data that was compressed with deflate (zlib format)
deflateRawSync()functionFullCompress data using the deflate algorithm in raw mode (no zlib header, RFC 1951)
inflateRawSync()functionFullDecompress raw deflate data (no zlib header)
gzipSync()functionFullCompress data using gzip (RFC 1952)
gunzipSync()functionFullDecompress gzip data (RFC 1952)
brotliCompressSync()functionFullCompress data using the Brotli algorithm (RFC 7932)
brotliDecompressSync()functionFullDecompress Brotli-compressed data (RFC 7932)
unzipSync()functionFullDecompress data with automatic format detection (gzip or deflate)
constantspropertyFullThe constants object exposes zlib, Brotli, and Zstd numeric constants
top-level constantsconstantPartialZ_NO_COMPRESSION, Z_BEST_SPEED, Z_BEST_COMPRESSION, Z_DEFAULT_COMPRESSION, Z_FILTERED, Z_HUFFMAN_ONLY and 73 more

Not implemented

107 top-level constants such as DEFLATE.

Чи була ця сторінка корисною?