Download a zip file from the Internet

In this simple tutorial we'll see how to download derby.jar from apache.org. The full code can be downloaded here

The steps we'll follows are:

  1. Downloading the zip file db-derby-10.14.2.0-lib.zip with the libs from https://archive.apache.org:443/dist/db/derby/db-derby-10.14.2.0/
  2. Saving the downloaded file in the current directory
  3. Unzipping the file
  4. Extracting the file derby.jar and copying in teh current directory
  5. Removing unuseful directories

Downloading the zip file from the Internet

In order to download the file we need to create an outputPort as it follows:

...

interface ApacheDerbyDownloadInterface {
    RequestResponse:
        downloadDerby
}

...

outputPort ApacheDerby1_10_14 {
    location: "socket://archive.apache.org:443/dist/db/derby/db-derby-10.14.2.0/"
    protocol: https {
        .osc.downloadDerby.alias = "db-derby-10.14.2.0-lib.zip"
        .osc.downloadDerby.method = "get"
    }
    interfaces: ApacheDerbyDownloadInterface
}

...

Note that:

  1. The location is set without the name of the file, and the https token is replaced with medium socket
  2. The protocol https is defined in the parameter protocol
  3. The interface ApacheDerbyDownloadInterface has been attached to the outputPort where the operation downloadDerby is defined
  4. Protocol https is defines with two extra parameters .osc.downloadDerby.alias and .osc.downloadDerby.method. The former one defines an alias to use for making a request for operation downloadDerby, the latter one defines the http method to use when invoking the endpoint using operation downloadDerby

In the behaviour, it is sufficient to program the related solicit-response:

...
downloadDerby@ApacheDerby1_10_14()( derby )
...

Variable derby contains the downloaded file.

Saving the downloaded file

This step is quite simple, just use the File standard library. Pay attentio to use the format binary for keeping safe the file encoding.

writeFile@File( { filename = DOWNLOADED_ZIP, content << derby } )()

where DOWNLOADED_ZIP is a constant with the name of the file.

Unzipping the file

Now it is possibile to unzip the file by using the standard library zip-utils

from zip-utils import ZipUtils
...
embed ZipUtils as ZipUtils
...
unzip@ZipUtils( { filename = DOWNLOADED_ZIP, targetPath = TARGET_PATH_UNZIP} )()

where TARGET_PATH_UNZIP is a constant with the target path where saving the unzipped files.

Extraction of the derby.jar file

This step is quite simple because it is sufficient to exploit the standard library File

derbyDbJarFilename = TARGET_PATH_UNZIP + "/db-derby-10.14.2.0-lib/lib/derby.jar"
readFile@File( { filename = derbyDbJarFilename, format = "binary" } )( derbyJar )
writeFile@File( { filename = "derby.jar", format = "binary", content << derbyJar })()

Unuseful directories removal

Also this step exploits the standard library for files

delete@File( DOWNLOADED_ZIP )()
deleteDir@File( TARGET_PATH_UNZIP )()