k.utils.compression
Gzip decompression and ZIP packaging
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
| KScript API, background jobs | Path overloads read/write site k.file paths | Unpacking .gz imports, building downloadable ZIPs, and extracting ZIP uploads |
k.utils.compression handles Gzip and ZIP archives in memory (byte arrays) or via site file paths.
Quick Start
k.api.post("pack", () => {
const zipBytes = k.utils.compression.zip([
{ name: "readme.txt", binary: k.utils.stringToBytes("Export generated by Kooboo", "utf-8") }
])
const entries = k.utils.compression.unzip(zipBytes)
return { count: entries.length, firstText: entries[0].getString() }
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
decompressGzip(path) | Read a site Gzip file and decompress as UTF-8 text | string |
decompressGzipBinary(bytes) | Decompress Gzip bytes into a UTF-8 string | string |
zip(items) | Package { name, binary }[] entries into ZIP bytes | number[] |
zip(folder, zipPath) | Package a site folder and write to a site ZIP file | void |
unzip(binary) | Extract ZIP bytes into ZipItem[] | ZipItem[] |
unzip(zipPath, folder) | Extract a site ZIP file into a site folder | void |
decompressGzip() / decompressGzipBinary()
Decompress a Gzip archive from a site file path or byte array and return its UTF-8 text.
| Overload | Description |
|---|---|
decompressGzip(path) | Read from a site file path and unpack into a string |
decompressGzipBinary(bytes) | Decompress from a byte array |
| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Required for decompressGzip | Site file path |
bytes | number[] | Required for decompressGzipBinary | Gzip byte array |
Returns: string, the UTF-8 decompressed text.
const text = k.utils.compression.decompressGzip("imports/catalog.json.gz")
const products = JSON.parse(text)
return {
count: products.length,
firstSku: products[0] ? products[0].sku : null
}When the Gzip content comes from an uploaded file or external API, pass bytes directly:
const file = k.request.files[0]
const text = k.utils.compression.decompressGzipBinary(file.bytes)
return JSON.parse(text)zip()
Package in-memory entries into ZIP bytes, or compress an entire site folder to a site ZIP path.
| Overload | Description |
|---|---|
zip(items) | items is { name, binary }[], returning ZIP bytes |
zip(folder, zipPath) | Package a site directory and write it to zipPath |
| Parameter | Type | Required | Description |
|---|---|---|---|
items | Entry[] | Required for zip(items) | { name: string, binary: number[] }[] entries |
folder | string | Required for zip(folder, zipPath) | Site folder to package |
zipPath | string | Required for zip(folder, zipPath) | ZIP file save path |
Returns: zip(items) returns number[] (ZIP bytes); zip(folder, zipPath) returns void (writes directly to the site file).
const zipBytes = k.utils.compression.zip([
{
name: "readme.txt",
binary: k.utils.stringToBytes("Export generated by Kooboo", "utf-8")
},
{
name: "catalog.json",
binary: k.utils.stringToBytes(JSON.stringify(products), "utf-8")
}
])
k.file.writeBinary("exports/catalog.zip", zipBytes)Use the path overload to zip a whole site folder:
k.utils.compression.zip("exports/catalog", "exports/catalog.zip")
const file = k.file.get("exports/catalog.zip")
return { path: file.fullName, size: file.size }unzip()
Extract ZIP bytes into ZipItem[], or extract a site ZIP file directly into a site folder.
| Overload | Description |
|---|---|
unzip(binary) | Unzip to ZipItem[] (name, fullName, binary, getString()) |
unzip(zipPath, folder) | Unzip to site directory |
| Parameter | Type | Required | Description |
|---|---|---|---|
binary | number[] | Required for unzip(binary) | ZIP bytes |
zipPath | string | Required for unzip(zipPath, folder) | Site ZIP file path |
folder | string | Required for unzip(zipPath, folder) | Target folder |
Returns: unzip(binary) returns ZipItem[] (including name, fullName, binary, and getString()); unzip(zipPath, folder) returns void (writes directly to the site folder).
const zipBytes = k.file.readBinary("imports/catalog.zip")
const items = k.utils.compression.unzip(zipBytes)
for (const item of items) {
k.file.writeBinary("imports/unpacked/" + item.fullName, item.binary)
}
return items.map((item) => item.fullName)Unzip a site file into a folder:
k.utils.compression.unzip("imports/catalog.zip", "imports/unpacked")
return k.file.folderFiles("imports/unpacked")TypeScript Definitions
interface Compression {
decompressGzip(path: string): string;
decompressGzipBinary(bytes: number[]): string;
zip(items: Entry[]): number[];
zip(folder: string, zipPath: string): void;
unzip(Binary: number[]): ZipItem[];
unzip(zipPath: string, folder: string): void;
}
interface Entry {
name: string;
binary: number[];
}
interface ZipItem {
name: string;
fullName: string;
binary: number[];
getString(): string;
}