Skip to content

k.utils.compression

Gzip decompression and ZIP packaging

Overview

k.utils.compression handles Gzip and ZIP archives (memory or site file path).

decompressGzip()

OverloadDescription
decompressGzip(path)Read from site file path and unpack into string
decompressGzipBinary(bytes)Decompress from byte array
ParameterTypeRequiredDescription
pathstringYesSite file path
bytesnumber[]YesGzip 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()

OverloadDescription
zip(items)items is { name, binary }[], returning ZIP bytes
zip(folder, zipPath)Package the site directory and write it to zipPath
ParameterTypeRequiredDescription
items{ name: string, binary: number[] }[]YesFiles to write into the ZIP
folderstringYesSite folder to package
zipPathstringYesZIP file save path

Returns:

OverloadReturn
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()

OverloadDescription
unzip(binary)Unzip to ZipItem[] (including name, binary)
unzip(zipPath, folder)Unzip to site directory
ParameterTypeRequiredDescription
binarynumber[]YesZIP bytes
zipPathstringYesSite ZIP file path
folderstringYesTarget folder

Returns:

OverloadReturn
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")