Difference between revisions of "Archive Module"

From BaseX Documentation
Jump to navigation Jump to search
Line 15: Line 15:
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Creates a new ZIP archive from the specified entries and contents.<br/>The {{Code|$entries}} elements 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:
+
|Creates a new ZIP archive from the specified entries and contents.<br/>The {{Code|$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:
 
<pre class="brush:xml">
 
<pre class="brush:xml">
 
<entry last-modified='2011-11-11T11:11:11'
 
<entry last-modified='2011-11-11T11:11:11'
Line 24: Line 24:
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|{{Error|FOZ20001|#Errors}} the number of entries and contents differs.<br />{{Error|FOZ20002|#Errors}} (some of) the contents are not of type {{Code|xs:string}} or {{Code|xs:base64Binary}}.<br />{{Error|FOZ20003|#Errors}} entries elements contain invalid data.<br/>{{Error|FOZ20004|#Errors}} the specified encoding is invalid or not supported.<br/>{{Error|FOZ29999|#Errors}} archive processing failed for some other reason.
+
|{{Error|FOZ20001|#Errors}} the number of entries and contents differs.<br />{{Error|FOZ20002|#Errors}} (some of) the contents are not of type {{Code|xs:string}} or {{Code|xs:base64Binary}}.<br />{{Error|FOZ20003|#Errors}} entry descriptors contain invalid data.<br/>{{Error|FOZ20004|#Errors}} specified encoding is invalid or not supported.<br/>{{Error|FOZ29999|#Errors}} archive creation failed for some other reason.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
|The following function creates an archive {{Code|archive.zip}} with one file {{Code|file.txt}}:
+
|The following one-liner creates an archive {{Code|archive.zip}} with one file {{Code|file.txt}}:
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
 
zip2:create(<entry>file.txt</entry>, 'Hello World')
 
zip2:create(<entry>file.txt</entry>, 'Hello World')
 
</pre>
 
</pre>
 +
|The following function create a {{Code|mp3.zip}} archive containing all mp3 files of a directory:
 +
<pre class="brush:xquery">
 +
let $path := 'audio/'
 +
let $entries  := file:list($path, true(), '*.mp3')
 +
let $zip := zip2:create(
 +
  for $e in $entries
 +
  return element entry { $e },
 +
  for $e in $entries
 +
  return file:read-binary($path || $e)
 +
)
 +
return file:write-binary('mp3.zip', $zip)</pre>
 
|}
 
|}
  
 
[[Category:XQuery]]
 
[[Category:XQuery]]

Revision as of 21:17, 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 create a mp3.zip archive containing all mp3 files of a directory:
let $path := 'audio/'
let $entries  := file:list($path, true(), '*.mp3')
let $zip := zip2:create(
  for $e in $entries
  return element entry { $e },
  for $e in $entries
  return file:read-binary($path || $e)
)
return file:write-binary('mp3.zip', $zip)