Skip to content

File

An open file within a ZIP archive.

Returned by Archive.open and Entry.open. Entry metadata such as size and Unix mode is available on Entry rather than here.

A function that accepts an Io, such as compress.compress, can read or write the file, as its mode allows.

Inherits from: io.Io

Fields

readable @ Bool [get]

Whether the file is open for reading.

writable @ Bool [get]

Whether the file is open for writing.

Methods

close()

Closes the file.

Example

open "archive.zip" do |archive|
  let file = archive.open "data.txt"
  let content = file.read 1024
  file.close()

read … -> Bin

Reads data from the file. Read mode only. Reads to the end of the entry when size is omitted. With size, reads that many bytes unless the end is reached first.

Parameters

NameTypeDescription
size? Int Number of bytes to read; omit to read the rest of the entry.

Errors

StateError if the file was opened in write mode.

Example

open "archive.zip" do |archive|
  archive.open "document.txt" do |file|
    let data = file.read 4096
    echo "Read $(data.len) bytes"
    echo $ Str $data

write *data -> Int

Writes data, given in pieces, to the file. Write mode only.

Parameters

NameTypeDescription
*data (Bin | Str) Data to write.

Returns

The number of bytes written, which is all of them.

Errors

StateError if the file was opened in read mode.

Example

open "output.zip" "w" do |archive|
  archive.open "data.bin" do |file|
    file.write "Hello, " "World!"
    file.write b"\x00\x01\x02\x03"