Skip to content

class ZInternalFile


methods


ZInternalFile.read


function ZInternalFile.read(...: ("*a"|"*l"|integer)) ->  string?

Read a file according to the given formats, which specify what to read.

For each format, the function returns a string with the characters read, or nil if it cannot read data with the specified format. When called without formats, it uses a default format that reads the entire next line (see below).

The available formats are:

  • *a: reads the whole file, starting at the current position. On end of file, it returns the empty string.
  • *l: reads the next line (skipping the end of line), returns nil on end of file. This is the default format.
  • number: reads a string with up to that number of characters, returning nil on end of file.

Example:

local z_file = zip.open('../test.zip')
assert(z_file)
local z_internal_file, err = z_file:open('Hello-world.txt')
assert(z_internal_file)
local content = z_internal_file:read('*a')
assert(content == 'Hello, World!\n', content)

Reference:

ZInternalFile.seek


function ZInternalFile.seek(
  whence: ("set"|"cur"|"end")?,
  offset: integer?
)
 -> offset integer?
 -> err string?
 -> errno integer?

Sets and gets the file position.

Unlike the standard I/O read, the format "*n" is not supported.

Sets and gets the file position, measured from the beginning of the file, to the position given by offset plus a base specified by the string whence, as follows:

  • set: base is position 0 (beginning of the file);
  • cur: base is current position;
  • end: base is end of file;

In case of success, function seek returns the final file position, measured in bytes from the beginning of the file. If this function fails, it returns nil, plus an error string. The default value for whence is "cur", and for offset is 0. Therefore, the call zfile:seek() returns the current file position, without changing it; the call zfile:seek("set") sets the position to the beginning of the file (and returns 0); and the call zfile:seek("end") sets the position to the end of the file, and returns its size.

Example:

local z_file = zip.open('../test.zip')
assert(z_file)
local z_internal_file, err = z_file:open('Hello-world.txt')
assert(z_internal_file)

local size = z_internal_file:seek('end')
assert(size == 14, size)

local offset = z_internal_file:seek('set', 7)
assert(offset == 7)

local cur = z_internal_file:seek('cur')
assert(cur == 7)

Reference:

ZInternalFile.close


function ZInternalFile.close()
 -> success true?
 -> err string?
 -> errno integer?

Close a file opened by zfile:open().

Example:

local z_file = zip.open('../test.zip')
assert(z_file)
local z_internal_file, err = z_file:open('Hello-world.txt')
assert(z_internal_file)
local success = z_internal_file:close()
assert(success == true)

Reference:

@see ZFile.open

ZInternalFile.lines


function ZInternalFile.lines() ->  fun() -> string

Return an iterator function that returns a new line from the file each time it is called.

Therefore, the construction

for line in file:lines() do ... end

will iterate over all lines of the file.

Example:

local z_file = zip.open('../test.zip')
assert(z_file)
local z_internal_file, err = z_file:open('Hello-world.txt')
assert(z_internal_file)
for line in z_internal_file:lines() do
  assert(line == 'Hello, World!')
end

Reference: