Class: Zip

$.util. Zip

Class for manipulation of zip archives. It provides functionality for compressing, uncompressing, removal of entries and zip encryption.

new Zip(source, index, settings)

Parameters:
Name Type Argument Description
source ArrayBuffer | $.db.ResultSet | $.web.Body <optional>
Specifies the source for the compressed content. If no source is specified, an empty Zip object is created.
index number <optional>
If the first argument is of type ResultSet, the number specifies the index of a Blob column and is mandatory.
settings object <optional>
Used to specify the following options:
  • password - key for enabling encryption. The password is mandatory when creating a zip object from an existing encrypted archive.
  • maxUncompressedSizeInBytes - a global restriction applies to the amount of data that can be uncompressed. You can use the property maxUncompressedSizeInBytes to override the global setting and reduce even further the amount of uncompressed data allowed.
Examples
 Simple Example

var zip = new $.util.Zip({"password": "myPassword"});
zip["entry.txt"] = "Content of the entry";

$.response.status = $.net.http.OK;
$.response.contentType = "application/zip";
$.response.headers.set("Content-Disposition", "attachment; filename = Encrypted.zip");
$.response.setBody(zip.asArrayBuffer());
 Add, Update, Delete Entries

// When modeling folder hierarchies the zip object behaves like an associative array. The entry names are the keys or in other words
// full paths to the files.

//Add Zip Entry. Note that zip["entry1"] is absolutely equivalent to zip.entry1.
var zip = new $.util.Zip();
zip["entry1"] = "old entry";

//Update Entry in Zip.
zip["entry1"] = "new entry";

//Getting Entry Content. If Entry does not exist, this returns undefined.
 var content = zip["entry1"];

//Delete Entry in Zip. If entry does not exist nothing happens.
delete zip["entry1"];
 Load Zip From Array Buffer

var archive = $.request.body;
var zip = new $.util.Zip(archive.asArrayBuffer());

...

var zip = new $.util.Zip(archive.asArrayBuffer(), {"maxUncompressedSizeInBytes": 20000, "password": "myPassword"});

...
 Iterating Over Zip Entries

//Create Zip Object
var zip = new $.util.Zip();
zip["Entry1.txt"] = "Content of Entry1.txt";
zip["Entry2.txt"] = "Content of Entry2.txt";
zip["ThirdEntry.txt"] = "Content of the third entry";

var responseString = "";

for(var entry in zip) {
    if(entry.indexOf("Entry") === 0) {
        responseString = responseString + entry + " - " + String.fromCharCode.apply(null, new Uint8Array(zip[entry])) + "\n";
    }
}

$.response.status = $.net.http.OK;
$.response.contentType = "text/plain";
$.response.setBody(responseString);
 WebRequest/WebResponse Body Integration

//Create Zip Object From WebRequest Body
var zip = new $.util.Zip($.request.body);
zip["entry"] = "This is another entry added to the zip";

//Return Zip Object as WebResponse Body
$.response.status = $.net.http.OK;
$.response.contentType = "application/zip";
$.response.headers.set("Content-Disposition", "attachment; filename = fromBody.zip");
$.response.setBody(zip);
 ResultSet Integration

onesql("create column table EXAMPLETABLE (id integer primary key, data blob)");
//Create Zip Object
var zip = new $.util.Zip();
zip["entry"] = "This content will be inserted into the table";

//Commit Zip Archive
var statement = conn.prepareStatement("insert into EXAMPLETABLE values(1, ?)");
statement.setBlob(1, zip);
statement.execute();
statement.close();
conn.commit();

//Load Zip Out Of DB Blob
statement = conn.prepareStatement("select data from EXAMPLETABLE where id=1");
var rs = statement.executeQuery();
if (rs) {
    while (rs.next()) {
        //Load Zip From ResultSet
        var loadedZip = new $.util.Zip(rs, 1);
        $.response.status = $.net.http.OK;
        $.response.contentType = "application/zip";
        $.response.headers.set("Content-Disposition", "attachment; filename = fromResultSet.zip");
        $.response.setBody(loadedZip);
    }
} else {
    ...
}
...

Members

_metadata_ :object

Holds an object that contains meta information about the current zip object.
Type:
  • object
Example
var zip = new $.util.Zip();
zip["Entry1.txt"] = "Hello World!";

$.response.setBody(JSON.stringify(zip._metadata_));
// produces {"compressedSizeInBytes":14,"uncompressedSizeInBytes":12}

_password_ :string

Key used for encryption. Empty string or nothing means no encryption of the data.
Type:
  • string

Methods

asArrayBuffer() → {ArrayBuffer}

Returns the zip archive as ArrayBuffer
Returns:
Buffer containing the whole zipped content.
Type
ArrayBuffer