Compress

The Compress module provides functions for data compression and decompression using Gzip, Zlib, and raw Deflate algorithms, plus checksum functions.

File Capabilities

In-memory compression helpers do not require authority. File helpers such as Gzip.compress-file, Zlib.compress-file, and Deflate.compress-file require FileReadAuth for the input and FileWriteAuth for the output.

Gzip

Functions for Gzip compression, commonly used for HTTP compression and .gz files.

Gzip.compress
String -> Int -> Result String String
Compresses data using gzip format. The second argument is the compression level (1-9, where 9 is maximum compression).
match Gzip.compress "Hello, World!" 6
  | Ok compressed -> println "Compressed ${String.length compressed} bytes"
  | Err e -> println "Error: ${e}"
Gzip.decompress
String -> Result String String
Decompresses gzip-compressed data.
match Gzip.decompress compressed-data
  | Ok original -> println original
  | Err e -> println "Error: ${e}"
Gzip.compress-file
FileReadAuth -> FileWriteAuth -> String -> String -> CompressionLevel -> Result () String
Compresses a file to gzip format. Takes read authority, write authority, source path, destination path, and compression level.
match Gzip.compress-file read-auth write-auth "data.txt" "data.txt.gz" 6
  | Ok _ -> println "File compressed"
  | Err e -> println "Error: ${e}"
Gzip.decompress-file
FileReadAuth -> FileWriteAuth -> String -> String -> Result () String
Decompresses a gzip file. Takes read authority, write authority, source path, and destination path.
match Gzip.decompress-file read-auth write-auth "data.txt.gz" "data.txt"
  | Ok _ -> println "File decompressed"
  | Err e -> println "Error: ${e}"
Gzip.is-gzip?
String -> Bool
Returns true if the data appears to be gzip-compressed (checks magic bytes).

Zlib

Functions for Zlib compression, commonly used in PNG images and many network protocols.

Zlib.compress
String -> Int -> Result String String
Compresses data using zlib format. The second argument is the compression level (1-9).
match Zlib.compress "Hello, World!" 6
  | Ok compressed -> println "Compressed to ${String.length compressed} bytes"
  | Err e -> println "Error: ${e}"
Zlib.decompress
String -> Result String String
Decompresses zlib-compressed data.
Zlib.compress-file
FileReadAuth -> FileWriteAuth -> String -> String -> CompressionLevel -> Result () String
Compresses a file to zlib format. Takes read authority, write authority, source path, destination path, and compression level.
match Zlib.compress-file read-auth write-auth "data.bin" "data.bin.z" 6
  | Ok _ -> println "File compressed"
  | Err e -> println "Error: ${e}"
Zlib.decompress-file
FileReadAuth -> FileWriteAuth -> String -> String -> Result () String
Decompresses a zlib file. Takes read authority, write authority, source path, and destination path.
match Zlib.decompress-file read-auth write-auth "data.bin.z" "data.bin"
  | Ok _ -> println "File decompressed"
  | Err e -> println "Error: ${e}"
Zlib.is-zlib?
String -> Bool
Returns true if the data appears to be zlib-compressed.

Deflate

Functions for raw Deflate compression without headers.

Deflate.compress
String -> Int -> Result String String
Compresses data using raw deflate (no gzip/zlib headers). The second argument is the compression level (1-9).
Deflate.decompress
String -> Result String String
Decompresses raw deflate data.
Deflate.compress-file
FileReadAuth -> FileWriteAuth -> String -> String -> CompressionLevel -> Result () String
Compresses a file with raw deflate. Takes read authority, write authority, source path, destination path, and compression level.
match Deflate.compress-file read-auth write-auth "data.bin" "data.bin.deflate" 6
  | Ok _ -> println "File compressed"
  | Err e -> println "Error: ${e}"
Deflate.decompress-file
FileReadAuth -> FileWriteAuth -> String -> String -> Result () String
Decompresses a raw deflate file. Takes read authority, write authority, source path, and destination path.
match Deflate.decompress-file read-auth write-auth "data.bin.deflate" "data.bin"
  | Ok _ -> println "File decompressed"
  | Err e -> println "Error: ${e}"

Checksum

Functions for computing checksums for data integrity verification.

Checksum.crc32
String -> Int
Computes the CRC32 checksum of the data. Returns a 32-bit integer.
checksum = Checksum.crc32 "Hello, World!"
println "CRC32: ${Int.to-hex checksum}"
Checksum.adler32
String -> Int
Computes the Adler-32 checksum of the data. Faster than CRC32 but slightly less reliable for error detection.
checksum = Checksum.adler32 "Hello, World!"
println "Adler32: ${checksum}"