Skip to main content
Skip table of contents

unzip

This procedure unzips an existing zip archive to a destination folder.

Usage

SQL
 CALL "UTILS.unzip"(
    "file_datasource" => 'string_file_datasource',
    "file_name" => 'string_file_name',
    "output_folder" => 'string_output_folder'
);;

Parameters

ParameterDescription
string_file_datasourceData source name where the archive file is located; mandatory
string_file_nameZip archive to be unzipped; mandatory
string_output_folderName of the output folder. If the output folder does not exist, it will be created. If the output folder is set to empty, the archive will be unzipped in the 'string_file_datasource' file directory

Definition

SQL
CREATE VIRTUAL PROCEDURE unzip(IN file_datasource string, IN file_name string , IN output_folder string)
AS
BEGIN
    DECLARE string file_source_props = SELECT properties FROM "SYSADMIN.Connections" WHERE "name" = file_datasource;
    IF( file_source_props IS NULL )
    BEGIN
        ERROR 'The datasource  "' || file_datasource || '"  does not exist!';
    END
      
        SELECT * FROM (OBJECTTABLE(language 'javascript'
                    'importPackage(java.io);
                    var matches = file_source_props.match(/([^=,]*)=("(?:\\.|[^"\\]+)*"|[^,"]*)/g);
                    var result = {};
                    for( i=0;i<matches.length;i++) {
                        var key = matches[i].match(/([^=,]*)=("[^"]*"|[^,"]*)/)[1];
                        var value = matches[i].match(/([^=,]*)=("[^"]*"|[^,"]*)/)[2];
                        result[key] = value;
                    }
                    var source_dir = result["ParentDirectory"];
                    var filename = source_dir + java.lang.System.getProperty("file.separator") + file_name;
                    var zip = new java.util.zip.ZipInputStream(new FileInputStream(filename));
                    var ze = null;
                     
                    var outputFolder = new File(source_dir + java.lang.System.getProperty("file.separator") + output_folder);
                    if(!outputFolder.exists()){
                        outputFolder.mkdirs();
                    }
                    var outputFolderName = source_dir + java.lang.System.getProperty("file.separator") + output_folder;
                    while ( (ze = zip.getNextEntry()) != null ) {
                       var entryFileName = ze.getName();
                       var newSubFile = new File(outputFolderName + java.lang.System.getProperty("file.separator")+ entryFileName);
                     if(ze.isDirectory()) {
                        var newSubDir = new File(newSubFile.getAbsolutePath());
                        if(!newSubDir.exists()) {
                         newSubDir.mkdirs();
                        }
                      } else {
                       var fos = new java.io.FileOutputStream(newSubFile);      
                       var buffer = new java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, 65536);     
                       var len;
                       while ((len = zip.read(buffer)) > 0) {
                                fos.write(buffer, 0, len);
                        }
                       fos.close();
                      }
                    }
                     zip.closeEntry();
                     zip.close();
                    '
                    PASSING file_source_props AS file_source_props, file_name AS file_name, output_folder AS output_folder
                    COLUMNS "result" blob 'dv_row'
                )AS x
            ) ;
END;

Examples

1. Example with indicated output folder

SQL
CALL "UTILS.unzip"(
    "file_datasource" => 'file',
    "file_name" => 'jboss-as-7.1.1.Final.zip',
    "output_folder" => '1'
);;

2. Example without indicated output folder

SQL
CALL "UTILS.unzip"(
    "file_datasource" => 'file',
    "file_name" => 'jboss-as-7.1.1.Final.zip',
    "output_folder" => ''
);;
JavaScript errors detected

Please note, these errors can depend on your browser setup.

If this problem persists, please contact our support.