62zip Module

62.1Overview

The zip module provides measures to read/write ZIP files.

62.2zip.reader Class

The zip.reader class provides methods to read contents and to get information in a ZIP file through stream instance. An instance of stream class created by the methods includes a property named stat, a zip.stat instance, which provides information such as filename and created time stamp that are contained in the ZIP file.

Below is an example to list filenames in a ZIP file:

import(zip)
zip.reader('foo.zip') {|r|
    println(r.entries():*stat:*filename)
}

Below is an example to print a content of a text file that is stored in a ZIP file:

import(zip)
zip.reader('foo.zip') {|r|
    print(r.entry('README.txt').readlines())
}

62.2.1Constructor

zip.reader(stream:stream:r) {block?}

Creates zip.reader instance from the specified stream.

If block is specified, it would be evaluated with a block parameter |reader:zip.reader|, where reader is the created instance. In this case, the block's result would become the function's returned value.

62.2.2Method

zip.reader#entry(name:string) {block?}

Seeks entry in the zip file that matches the specified name and returns a stream instance associated with the entry.

If block is specified, it would be evaluated with a block parameter |s:stream|, where s is the created instance. In this case, the block's result would become the function's returned value.

zip.reader#entries() {block?}

Creates an iterator instance that returns stream instances associated with each entry in the ZIP file.

In default, this returns an iterator as its result value. Specifying the following attributes would customize the returned value:

  • :iter .. An iterator. This is the default behavior.
  • :xiter .. An iterator that eliminates nil from its elements.
  • :list .. A list.
  • :xlist .. A list that eliminates nil from its elements.
  • :set .. A list that eliminates duplicated values from its elements.
  • :xset .. A list that eliminates duplicated values and nil from its elements.

See the chapter of Mapping Process in Gura Language Manual for the detail.

If a block is specified, it would be evaluated repeatingly with block parameters |value, idx:number| where value is the iterated value and idx the loop index starting from zero. In this case, the last evaluated value of the block would be the result value. If one of the attributes listed above is specified, an iterator or a list of the evaluated value would be returned.

62.3zip.writer Class

The zip.writer class provides methods to add entries to a ZIP file. When an instance of zip.writer is created, a new ZIP file would be created.

Below is an exapmple to create a ZIP archive file that contains three entries:

import(zip)
zip.writer('foo.zip') {|w|
    w.add('file1.txt')
    w.add('file2.txt')
    w.add('file3.txt')
    w.close()
}		

62.3.1Constructor

zip.writer(stream:stream:w, compression?:symbol) {block?}

Creates zip.writer instance from the stream.

Argument compression specifies the compression method and takes one of the following symbol.

  • `store
  • `deflate
  • `bzip2

If block is specified, it would be evaluated with a block parameter |writer:zip.writer|, where writer is the created instance. In this case, the block's result would become the function's returned value.

62.3.2Method

zip.writer#add(stream:stream:r, filename?:string, compression?:symbol):map:reduce

Reads data from stream and adds it to the zip file. Entry name is decided by the file name associated with the stream unless it's specified by argument filename.

Argument compression specifies the compression method and takes one of the following symbol.

  • `store
  • `deflate
  • `bzip2
zip.writer#close():void
Closes the zip file after flushing cached data.

62.4zip.stat Class

The zip.stat class provides information of entries in a ZIP file.

62.4.1Property

Property Type R/W Explanation
filename string R
comment string R
mtime datetime R
crc32 number R
compression_method number R
size number R
compressed_size number R
attributes number R

62.5Thanks

This module uses zlib and bzip2 library which are distributed in the following sites: