Websites Navigation: Airbit | Shop | m-shell.net
Languages: EN | DE

Module zip: ZIP Archives

This module provides read access to ZIP (PKZIP) archive files. Archive members are extracted via ordinary stream objects, to be passed to functions in module io.

For instance, the following function extracts all members in a ZIP archive matching a given pattern into the current directory (the default pattern extracts all members). Note that the code does not create any required directories, nor correctly distinguishes between directories and files.

function unzip(name, pattern=null)
  z=zip.open(name);
  for f in zip.scan(z, pattern) do
    print "Extracting",f["name"];
    i=zip.extract(z, f["name"]);
    o=io.create(f["name"]);
    b=io.read(i, 256);
    while b#null do
      io.write(o, b); b=io.read(i, 256)
    end;
    io.close(i); io.close(o)
  end;
  zip.close(z)
end

zip.close

• function close(zipfile) → null

Closes the ZIP archive zipfile previously opened with zip.open.

zip.extract

• function extract(zipfile, name) → Native Object

Opens a stream to extract the member name from the ZIP archive zipfile, and returns it. zipfile must have been previously opened with zip.open.

name is semi-case sensitive: if a member with the same name observing case exists, it is returned, otherwise a member with the same name ignoring case is returned.

Throws ErrNotFound if there is no such member.

The returned stream can be accessed with most functions from module io:

  • io.read, io.readln, and io.readm read data,
  • io.size gets the total number of bytes,
  • io.avail gets the number of bytes remaining,
  • io.close closes the stream,
  • io.ces gets and sets the character encoding scheme. As with files, the default is io.raw.

zip.open

• function open(file) → Native Object
  Permissions: Read(file)

Opens the ZIP archive with file name file, and returns an object to access it. The object should be closed with zip.close if it is no longer needed.

Throws ErrNotFound if there is no such archive, and ErrCorrupt if the archive is not valid.

zip.scan

• function scan(zipfile, name=null) → Array

Scans the ZIP archive zipfile for all members matching name. name is not case sensitive and can contain the wildcards * and ?. If name=null, all members are returned.

Returns an array with one element for each member found, each element being an array with the following keys:

KeyMeaningType
nameMember nameString
sizeUncompressed sizeNumber
csizeCompressed sizeNumber
crcCRC-32 checksumNumber

z=zip.open('ZipTest.zip')
for f in zip.scan(z, '*AudioTest.*') do
  print f
end
→ [tests\AudioTest.mid,1983,510,2487703623]
[tests\AudioTest.mm,2416,952,1954653783]


© 2004-2011 airbit AG, CH-8008 Zürich
Document AB-M-LIB-888
mShell Home  > Documentation  > Manuals