Skip to content

Sys.Zip

Sys.Zip creates and reads ZIP archives in memory and compresses data with gzip in server-side code. Archives are adm-zip objects (npm package adm-zip, version ^0.5.10) limited to the methods that work on memory, so app code cannot read or write server files through them.

Availability: Server

newZip(): ZipArchive

Creates a new, empty ZIP archive.

Returns: ZipArchive · Availability: Server

const zip = Sys.Zip.newZip()
zip.addFile('readme.txt', Buffer.from('Exported by Lowgile', 'utf-8'))
zip.addFile('data/orders.json', Buffer.from(JSON.stringify(orders), 'utf-8'))
const zipBuffer = zip.toBuffer()
readFromBuffer(buffer: Buffer | ArrayBuffer): ZipArchive

Opens a ZIP archive from its bytes, for example an uploaded file, to list, read or change its entries.

ParameterTypeDescription
bufferBuffer | ArrayBufferThe ZIP file’s content. A file name is not accepted.

Returns: ZipArchive · Availability: Server

const zip = Sys.Zip.readFromBuffer(uploadedFile)
for (const entry of zip.getEntries()) {
if (!entry.isDirectory) Sys.Log.info(entry.entryName, zip.readAsText(entry))
}
gzip(buffer: Buffer | ArrayBuffer, options?: ZlibOptions): Promise<Buffer<ArrayBufferLike>>

Compresses data with gzip, using Node.js zlib.

ParameterTypeDescription
bufferBuffer | ArrayBufferData to compress.
optionsZlibOptionsNode.js zlib options, such as level.

Returns: Promise<Buffer> · Availability: Server

gunzip(buffer: Buffer | ArrayBuffer, options?: ZlibOptions): Promise<Buffer<ArrayBufferLike>>

Decompresses gzip data, using Node.js zlib.

ParameterTypeDescription
bufferBuffer | ArrayBuffergzip-compressed data.
optionsZlibOptionsNode.js zlib options.

Returns: Promise<Buffer> · Availability: Server

type ZipArchive = Omit<AdmZip, ForbiddenMethods>

An adm-zip archive without the methods that access the server’s file system. The type cannot be referenced as Sys.Zip.ZipArchive; it is the return type of newZip() and readFromBuffer(). See the adm-zip documentation for the methods’ parameters.

MethodsPurpose
getEntries, getEntry, getEntryCount, forEachList and find entries.
readFile, readFileAsync, readAsText, readAsTextAsyncRead the content of an entry.
addFile, updateFile, deleteFileAdd, replace and remove entries.
addZipComment, getZipComment, addZipEntryComment, getZipEntryCommentRead and write comments.
testCheck the archive for errors.
toBuffer, toBufferPromiseGet the archive’s bytes.

The methods addLocalFile, addLocalFolder, addLocalFolderAsync, addLocalFolderPromise, extractEntryTo, extractAllTo, extractAllToAsync, writeZip and writeZipPromise are blocked: they are not in the type, and at runtime they are undefined. Use Sys.Fs or Sys.Files to store the result of toBuffer().

Availability: Server