Difference between revisions of "Archive Module"

From BaseX Documentation
Jump to navigation Jump to search
Line 33: Line 33:
 
The following function creates an archive {{Code|mp3.zip}}, which contains all MP3 files of a local directory:
 
The following function creates an archive {{Code|mp3.zip}}, which contains all MP3 files of a local directory:
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
let $path := 'audio/'
+
let $path := 'audio/'
let $entries  := file:list($path, true(), '*.mp3')
+
let $files := file:list($path, true(), '*.mp3')
let $zip := zip2:create(
+
let $zip   := zip2:create(
   $entries ! element entry { . },
+
   for $f in $files return <entry>{ $f }</entry>,
   $entries ! file:read-binary($path || .)
+
   for $f in $files return file:read-binary($path || $f)
 
)
 
)
 
return file:write-binary('mp3.zip', $zip)</pre>
 
return file:write-binary('mp3.zip', $zip)</pre>

Revision as of 21:19, 28 May 2012

This XQuery Module contains functions to handle ZIP archives. New ZIP archives can be created, existing archives can be updated, and the archive entries can be listed and extracted. This module may eventually replace the existing ZIP Module as soon as it is finalized and adopted by other XQuery implementations.

Conventions

All functions in this module are assigned to the http://basex.org/modules/zip2 namespace, which is statically bound to the zip2 prefix.
All errors are assigned to the http://basex.org/errors namespace, which is statically bound to the bxerr prefix.

Functions

zip2:create

Signatures zip2:create($entries as element(entry)*, $contents as item()*) as xs:base64Binary
Summary Creates a new ZIP archive from the specified entries and contents.
The $entries descriptors contain meta information required to create new ZIP entries. Beside the mandatory entry name, optional attributes can be specified, which contain the timestamp, compression level and encoding of textual entries. An Example:
<entry last-modified='2011-11-11T11:11:11'
       compression-level='9'
       encoding='US-ASCII'>hello.txt</entry>

The actual $contents must be xs:string or xs:base64Binary items.

Errors FOZ20001: the number of entries and contents differs.
FOZ20002: (some of) the contents are not of type xs:string or xs:base64Binary.
FOZ20003: entry descriptors contain invalid data.
FOZ20004: specified encoding is invalid or not supported.
FOZ29999: archive creation failed for some other reason.
Examples The following one-liner creates an archive archive.zip with one file file.txt:
zip2:create(<entry>file.txt</entry>, 'Hello World')

The following function creates an archive mp3.zip, which contains all MP3 files of a local directory:

let $path  := 'audio/'
let $files := file:list($path, true(), '*.mp3')
let $zip   := zip2:create(
  for $f in $files return <entry>{ $f }</entry>,
  for $f in $files return file:read-binary($path || $f)
)
return file:write-binary('mp3.zip', $zip)