Loading

try {
  http:get("/blog/60028626564/the-new-archive-module")
} catch http:not-found {
  <p>
    404 NOT FOUND.
    Take me home 
  </p>
}
~
~
-- INSERT -- All 6, 17

The New Archive Module

Posted 1 year ago

The Archive module provides functions to create, read, and modify archives given as xs:base64Binary items. For example, it provides functions to retrieve the names or extract the values of several entries from a zip archive.

In the following, we would like to show some basic examples to demonstrate how to use this module. Assume you have a digital book given in the ePub format (an ePub file is actually a collection of mostly XML and HTML files that are zipped together). To list information about all files/entries in such an archive, we use entries() as shown in the following example (you can try it live here):

import module namespace archive = "http://www.zorba-xquery.com/modules/archive";
import module namespace http = "http://expath.org/ns/http-client";

let $moby := http:send-request(<http:request href="http://zorba.s3.amazonaws.com/MobyDick.epub" method="get" />)[2]
return archive:entries($moby)

Similarly, we use extract-text() to extract the text content of a particular file in this archive. Here, we extract the first chapter of the book as text, parse it, and return all the paragraphs contained in this chapter (try it live):

import module namespace archive = "http://www.zorba-xquery.com/modules/archive";
import module namespace http = "http://expath.org/ns/http-client";

let $moby := http:send-request(<http:request href="http://zorba.s3.amazonaws.com/MobyDick.epub" method="get" />)[2]
let $chap1 := archive:extract-text($moby, "OPS/main1.xml")
let $doc := parse-xml($chap1)
return $doc//*:p

Finally, create() allows you to create archives. In the following example, we use it to create a tar.gz archive containing a text file and the Zorba logo (try it live):

import module namespace archive = "http://www.zorba-xquery.com/modules/archive";
import module namespace http = "http://expath.org/ns/http-client";

let $logo := http:send-request(<http:request href="http://www.zorba-xquery.com/images/zorba-logo-small.png" method="get" />)[2]
return
api:save-archive(
archive:create(
(
"my-text-file.txt",
"logo.png"
),
(
"This is the content of the text file.",
$logo
),
<archive:options>
<archive:format>TAR</archive:format>
<archive:compression>GZIP</archive:compression>
</archive:options>
)
)