zlib

The zlib module is a simple wrapper around the zlib functions.

gzip-compress and gzip-decompress manipulate gzip format compression files.

zlib-compress and zlib-decompress manipulate zlib format compression files.

deflate and inflate are closer to the native zlib functions allowing manipulation of gzip, zlib and raw format compression streams.

The following show how we can prepare and consume gzip format files for use by external commands.

gzip compression

gzip-compression.idio
import zlib

tmp-file := (libc/make-tmp-file)

;; Use a non-ASCII code point
str := "Ħello World\n"

;; write the gzip compressed string out to tmp-file
ish := open-input-string str
ofh := open-output-file tmp-file
puts (gzip-compress ish) ofh
close-handle ofh

;; zcat with external command

;; you may need gzcat on SunOS/Mac OS/OpenBSD
zcat tmp-file

rm tmp-file
$ idio gzip-compression
Ħello World

gzip decompression

gzip-decompression.idio
import zlib

tmp-file := (libc/make-tmp-file)

with-output-to tmp-file (function #n {
  puts "Ħello World\n"
})

;; you might need -f on OpenBSD (as the file is short)
gzip tmp-file

gz-file := append-string tmp-file ".gz"

;; gzip decompress the contents of gz-file
ifh := open-input-file gz-file
puts (gzip-decompress ifh)
close-handle ifh

rm gz-file
$ idio gzip-decompression
Ħello World

Last built at 2024-11-21T07:11:47Z+0000 from 77077af (dev) for Idio 0.3