Appearance
k.utils.compression
Gzip decompression and ZIP packaging
Overview
k.utils.compression handles Gzip and ZIP archives (memory or site file path).
decompressGzip()
| Overload | Description |
|---|---|
decompressGzip(path) | Read from site file path and unpack into string |
decompressGzipBinary(bytes) | Decompress from byte array |
| Parameter | Type | Required | Description |
|---|---|---|---|
path | string | Yes | Site file path |
bytes | number[] | Yes | Gzip byte array |
Returns: string, the UTF-8 decompressed text.
ts
const text = k.utils.compression.decompressGzip("imports/catalog.json.gz")
const products = JSON.parse(text)
return {
count: products.length,
firstSku: products[0]?.sku
}When the Gzip content comes from an uploaded file or external API, pass bytes directly:
ts
const file = k.request.files[0]
const text = k.utils.compression.decompressGzipBinary(file.bytes)
return JSON.parse(text)zip()
| Overload | Description |
|---|---|
zip(items) | items is { name, binary }[], returning ZIP bytes |
zip(folder, zipPath) | Package the site directory and write it to zipPath |
| Parameter | Type | Required | Description |
|---|---|---|---|
items | { name: string, binary: number[] }[] | Yes | Files to write into the ZIP |
folder | string | Yes | Site folder to package |
zipPath | string | Yes | ZIP file save path |
Returns:
| Overload | Return |
|---|---|
zip(items) | number[], ZIP bytes |
zip(folder, zipPath) | void, writes directly to the site file |
ts
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:
ts
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()
| Overload | Description |
|---|---|
unzip(binary) | Unzip to ZipItem[] (including name, binary) |
unzip(zipPath, folder) | Unzip to site directory |
| Parameter | Type | Required | Description |
|---|---|---|---|
binary | number[] | Yes | ZIP bytes |
zipPath | string | Yes | Site ZIP file path |
folder | string | Yes | Target folder |
Returns:
| Overload | Return |
|---|---|
unzip(binary) | ZipItem[], including name, fullName, and binary |
unzip(zipPath, folder) | void, writes directly to the site folder |
ts
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:
ts
k.utils.compression.unzip("imports/catalog.zip", "imports/unpacked")
return k.file.folderFiles("imports/unpacked")