Skip to content

k.utils.compression ​

Gzip decompression and ZIP packaging

Execution ContextPrerequisitesTypical Use
KScript API, background jobsPath overloads read/write site k.file pathsUnpacking .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 ​

js
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 / PropertyDescriptionReturns
decompressGzip(path)Read a site Gzip file and decompress as UTF-8 textstring
decompressGzipBinary(bytes)Decompress Gzip bytes into a UTF-8 stringstring
zip(items)Package { name, binary }[] entries into ZIP bytesnumber[]
zip(folder, zipPath)Package a site folder and write to a site ZIP filevoid
unzip(binary)Extract ZIP bytes into ZipItem[]ZipItem[]
unzip(zipPath, folder)Extract a site ZIP file into a site foldervoid

decompressGzip() / decompressGzipBinary() ​

Decompress a Gzip archive from a site file path or byte array and return its UTF-8 text.

OverloadDescription
decompressGzip(path)Read from a site file path and unpack into a string
decompressGzipBinary(bytes)Decompress from a byte array
ParameterTypeRequiredDescription
pathstringRequired for decompressGzipSite file path
bytesnumber[]Required for decompressGzipBinaryGzip byte array

Returns: string, the UTF-8 decompressed text.

js
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:

js
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.

OverloadDescription
zip(items)items is { name, binary }[], returning ZIP bytes
zip(folder, zipPath)Package a site directory and write it to zipPath
ParameterTypeRequiredDescription
itemsEntry[]Required for zip(items){ name: string, binary: number[] }[] entries
folderstringRequired for zip(folder, zipPath)Site folder to package
zipPathstringRequired 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).

js
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:

js
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.

OverloadDescription
unzip(binary)Unzip to ZipItem[] (name, fullName, binary, getString())
unzip(zipPath, folder)Unzip to site directory
ParameterTypeRequiredDescription
binarynumber[]Required for unzip(binary)ZIP bytes
zipPathstringRequired for unzip(zipPath, folder)Site ZIP file path
folderstringRequired 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).

js
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:

js
k.utils.compression.unzip("imports/catalog.zip", "imports/unpacked")
return k.file.folderFiles("imports/unpacked")

TypeScript Definitions ​

ts
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;
}