Skip to content

@absoluteui/services/zip

Re-exports the ZIP writer and reader of @absoluteui/helpers/zip.

import { ... } from '@absoluteui/services/zip';

Re-exports the ZIP writer and reader, which moved to @absoluteui/helpers/zip so that components can use them. Existing imports from @absoluteui/services/zip keep working; new code should import from @absoluteui/helpers/zip. Error codes are HLP_ZIP_* (they were SVC_ZIP_*).

createZipWriter

Creates a streaming ZIP writer. Entries stream through without knowing their sizes (local headers use data descriptors), names are UTF-8, and CRC-32 is computed incrementally.

function createZipWriter(options?: ZipWriterOptions): ZipWriter;

Example:

const zip = createZipWriter();
zip.add('reports/q1.csv', 'id,name\n1,Ada');
zip.add('photo.jpg', new File([new Uint8Array([0xff, 0xd8, 0xff])], 'photo.jpg'), { compression: 'store' });
const blob = await zip.toBlob();
(await readZip(blob)).entries.map((e) => e.name); // => ['reports/q1.csv', 'photo.jpg']

readZip

Opens a ZIP archive (including ZIP64) by reading its central directory; entry data is read lazily. Guards against zip bombs (entry count, total size, compression ratio, actual output never exceeding the declared size), verifies CRC-32, rejects encrypted entries, and sanitizes names so they cannot escape the extraction root (“zip slip”).

function readZip(input: Blob | Uint8Array | ArrayBuffer, options?: ReadZipOptions): Promise<ZipArchive>;

Example:

const file = await zipFiles([{ name: 'reports/q1.csv', data: 'id,name\n1,Ada' }]); // or a File from a picker
const zip = await readZip(file);
zip.entries.map((e) => e.name); // => ['reports/q1.csv']
const first = zip.entries[0];
const text = first ? new TextDecoder().decode(await first.read()) : ''; // => 'id,name\n1,Ada'

zipFiles

Zips files into a Blob in one call.

function zipFiles(files: readonly ZipFileInput[], options?: ZipWriterOptions): Promise<Blob>;

Example:

const pngBlob = new Blob([new Uint8Array([0x89, 0x50, 0x4e, 0x47])], { type: 'image/png' });
const blob = await zipFiles([{ name: 'a.txt', data: 'hello' }, { name: 'img/b.png', data: pngBlob, compression: 'store' }]);
blob.type; // => 'application/zip'

ReadZipOptions

interface ReadZipOptions {
readonly maxEntries?: number;
readonly maxTotalUncompressed?: number;
readonly maxCompressionRatio?: number;
readonly rawNames?: boolean;
readonly signal?: AbortSignal;
}

ZipAddOptions

interface ZipAddOptions {
readonly lastModified?: Date;
readonly compression?: ZipCompression;
readonly comment?: string;
}

ZipArchive

An opened archive.

interface ZipArchive {
readonly entries: readonly ZipEntry[];
readonly comment: string;
get(name: string): ZipEntry | undefined;
}

ZipEntry

One entry of an archive opened with readZip.

interface ZipEntry {
readonly name: string;
readonly rawName: string;
readonly size: number;
readonly compressedSize: number;
readonly lastModified: Date;
readonly isDirectory: boolean;
readonly encrypted: boolean;
readonly method: number;
readonly crc32: number;
readonly comment: string;
read(options?: {
readonly signal?: AbortSignal;
}): Promise<Uint8Array<ArrayBuffer>>;
stream(): ReadableStream<Uint8Array<ArrayBuffer>>;
}

ZipFileInput

A file for zipFiles.

interface ZipFileInput extends ZipAddOptions {
readonly name: string;
readonly data: ZipEntryData;
}

ZipWriter

Streaming ZIP archive builder.

interface ZipWriter {
add(name: string, data: ZipEntryData, options?: ZipAddOptions): void;
addDirectory(name: string, options?: Pick<ZipAddOptions, 'lastModified' | 'comment'>): void;
readonly size: number;
stream(): ReadableStream<Uint8Array<ArrayBuffer>>;
toBlob(): Promise<Blob>;
}

ZipWriterOptions

interface ZipWriterOptions {
readonly compression?: ZipCompression;
readonly comment?: string;
readonly zip64?: 'auto' | 'always';
readonly maxEntries?: number;
}

ZipCompression

deflate uses native CompressionStream('deflate-raw'); store writes bytes as they are.

type ZipCompression = 'deflate' | 'store';

ZipEntryData

Entry content. Streams and Blobs are read lazily while the archive is produced.

type ZipEntryData = string | Uint8Array | ArrayBuffer | Blob | ReadableStream<Uint8Array>;