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)

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.
 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 =");
 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";
 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 =");
 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);

//Load Zip Out Of DB Blob
statement = conn.prepareStatement("select data from EXAMPLETABLE where id=1");
var rs = statement.executeQuery();
if (rs) {
    while ( {
        //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 =");
} else {


_metadata_ :object

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

// produces {"compressedSizeInBytes":14,"uncompressedSizeInBytes":12}

_password_ :string

Setting a value to this property changes the password used for encryption of the zip object. Assigning an empty string disables encryption. Accessing this property will return undefined.
  • string
// create zip object
var zip = new $.util.Zip({"password" : "old_password"});
zip["Entry1.txt"] = "Hello World!";

// change the password
zip["_password_"] = "new_password";


asArrayBuffer() → {ArrayBuffer}

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