Zip class

The Zip class enables reading/writing/creating zip files. More...

The PRIMER JavaScript API provides many class constants, properties and methods. For Arup to be able to extend and enhance the API in the future any constant, property or method names beginning with a lowercase or uppercase letter are reserved.
If you need to add your own properties or methods to one of the existing classes then to avoid any potential future conflict you should ensure that the name begins with either an underscore (_) or a dollar sign ($) or the name is prefixed with your own unique identifier.
For example if company 'ABC' need to add a property called 'example' then to avoid any potential future conflict use one of:

Member functions

Zip constants

Name Description
Zip.APPEND Flag to open zip file for appending
Zip.READ Flag to open zip file for reading
Zip.WRITE Flag to open zip file for writing

Zip properties

Name Type Description
filename (read only) string Name of the zip file
mode (read only) constant Mode the zip file was opened with (Zip.READ, Zip.WRITE or Zip.APPEND)

Detailed Description

The Zip class provides functions to enable you to read, write and create zip files. The following simple example shows how to write a zip file and then read it again:

Message("Creating zip file");
var z = new Zip("C:/temp/test.zip", Zip.WRITE);
z.AddFile("C:/temp/bpost.key",   "bpost/bpost.key");
z.AddFile("C:/temp/door.key",    "door/door.key");
z.AddFile("C:/temp/barrier.key", "other.key");
z.Close();
Message("Done");

var entry = 0;
Message("Reading zip file");
var z = new Zip("C:/temp/test.zip", Zip.READ);
while (true)
{
    entry++;
    Message("Entry "+entry);
    var info = z.Information();
    for (var x in info)
        Message("    "+x+"="+info[x]);

    z.ReadFile(entry+".txt");

    if (!z.Next()) break;
}
z.Close();
Message("Done")

Constructor

new Zip(filename[string], mode[constant])

Description

Create a new Zip object for reading/writing zip files.

Arguments

  • filename (string)

    Filename of the zip file you want to read/write. If reading (Zip.READ) or appending (Zip.APPEND), the file must exist. If writing (Zip.WRITE) the file will be overwritten (if it exists).

  • mode (constant)

    The mode to open the file with. Can be Zip.READ, Zip.WRITE or Zip.APPEND.

    Returns

    Zip object

    Return type

    Zip

    Example

    To create a new Zip object to read Zip file "/data/test/file.zip"

    var p = new Zip("/data/test/file.zip");

    Details of functions

    AddFile(filename[string], zipname[string])

    Description

    Add a file to the Zip file

    Arguments

  • filename (string)

    Name of the file you want to add to the zip file

  • zipname (string)

    Name to give the file in the zip file

    Returns

    No return value

    Example

    To add file 'C:/temp/test.key' to Zip file z with zip name 'test.key':

    z.AddFile('C:/temp/test.key', 'test.key');


    Close()

    Description

    Close a Zip file

    Arguments

    No arguments

    Returns

    No return value

    Example

    To close Zip file z:

    z.Close();


    Information()

    Description

    Gets information for the current entry in the Zip file such as name, size etc

    Arguments

    No arguments

    Returns

    Object with the following properties:

    Name Type Description
    compressedSize integer Compressed size
    crc integer Cyclic redundancy check
    name string Filename
    uncompressedSize integer Uncompressed size

    Return type

    object

    Example

    To get the information:

    var info = z.Information();
    for (var x in info) Println(x + '=' + info[x]);


    Next()

    Description

    Go to the next entry in the Zip file

    Arguments

    No arguments

    Returns

    true if there is a next entry, false if there are no more entries

    Return type

    Boolean

    Example

    To go to the next entry in zip file z:

    var next = z.Next();


    ReadFile(filename[string])

    Description

    Reads the current entry to a file from the Zip file

    Arguments

  • filename (string)

    Name of the file you want to create

    Returns

    No return value

    Example

    To read the current entry in Zip file z to a file 'test.key':

    z.ReadFile('test.key');