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.

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 size -> Bin

Reads data from the file. Read mode only.

Parameters

NameTypeDescription
size Int Maximum number of bytes to read.

Errors

Raises a runtime error if called on a file 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

Writes data to the file. Write mode only.

Parameters

NameTypeDescription
data (Bin | Str) Data to write.

Errors

Raises a runtime error if called on a file 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"