Skip to content

zip

Reads and writes ZIP archives.

The API is VFS-transparent: archive paths use the active VFS context.

Types

TypeDescription
Archive An open ZIP archive.
Entry A single archive entry's metadata, with a method to open it for reading.
File An open file within a ZIP archive.
OpenMode Access mode accepted by open.

OpenMode

Access mode accepted by open.

TypeDescription
"r" Read mode -- opens an existing archive for reading
"w" Write mode -- creates a new archive, truncating any existing one

Functions

open source … -> Archive overload 1 of 3

Opens a ZIP archive.

The archive stays open until Archive.close is called.

The archive is read from or written to source. A path is opened. Any other source, such as an open fs.File, is used without being closed. Reading needs a readable, seekable source and writing a writable, seekable one, and writing starts at the source's beginning, since archive offsets are absolute.

Parameters

NameTypeDescription
source (Str | fs.Path | io.Io) Path to the ZIP archive, or a seekable stream holding it.
mode? OpenMode Access mode. Defaults to "r".

Errors

Exception Condition
TypeError source is not a path or an Io
ValueError source cannot be read or written as mode needs; it is left open
DataError The archive, or an entry's name or comment, is malformed

Example

fs.open "bundle.zip" do |file|
  open $file do |archive|
    for entry = archive.entries
      echo $entry.name

open[R] source func -> R overload 2 of 3

Opens a ZIP archive for reading and calls func with it. The archive is closed when func returns.

open "archive.zip" do |archive|
  for entry = archive.entries
    echo "Entry: $(entry.name)"

Parameters

NameTypeDescription
source (Str | fs.Path | io.Io) Path to the ZIP archive, or a seekable stream holding it.
func ((Archive) -> R) Called with the Archive.

open[R] source mode func -> R overload 3 of 3

Opens a ZIP archive with a mode and calls func with it. The archive is closed when func returns.

open "output.zip" "w" do |archive|
  archive.open "file.txt" do |file|
    file.write "Hello, World!"

Parameters

NameTypeDescription
source (Str | fs.Path | io.Io) Path to the ZIP archive, or a seekable stream holding it.
mode OpenMode Access mode.
func ((Archive) -> R) Called with the Archive.