This [[Module Library|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 will may soon replace the existing [[ZIP Module]] ([http://spex.basex.org/index.php?title=ZIP_Module more information]).
=Conventions=
All functions in this module are assigned to the {{Code|http://basex.org/modules/zip2archive}} namespace, which is statically bound to the {{Code|zip2archive}} prefix.<br/>
All errors are assigned to the {{Code|http://basex.org/errors}} namespace, which is statically bound to the {{Code|bxerr}} prefix.
=Functions=
==zip2archive:create==
{| width='100%'
|-
| width='90' | '''Signatures'''
|{{Func|zip2archive:create|$entries as element(entry)*, $contents as item()*|xs:base64Binary}}<br />
|-
| '''Summary'''
|The following one-liner creates an archive {{Code|archive.zip}} with one file {{Code|file.txt}}:
<pre class="brush:xquery">
zip2archive:create(<entry>file.txt</entry>, 'Hello World')
</pre>
The following function creates an archive {{Code|mp3.zip}}, which contains all MP3 files of a local directory:
let $path := 'audio/'
let $files := file:list($path, true(), '*.mp3')
let $zip := zip2archive:create(
for $f in $files return <entry>{ $f }</entry>,
for $f in $files return file:read-binary($path || $f)
|}
==zip2archive:entries==
{| width='100%'
|-
| width='90' | '''Signatures'''
|{{Func|zip2archive:entries|$zip as xs:base64Binary|element(entry)*}}<br />
|-
| '''Summary'''
|Sums up the file sizes of all entries of a JAR file:
<pre class="brush:xquery">
sum(zip2archive:entries(file:read-binary('zip.zip'))/@size)
</pre>
|}
==zip2archive:extract-text==
{| width='100%'
|-
| width='90' | '''Signatures'''
|{{Func|zip2archive:extract-text|$zip as xs:base64Binary|xs:string*}}<br/>{{Func|zip2archive:extract-text|$zip as xs:base64Binary, $entry-names as xs:string*|xs:string*}}<br/>{{Func|zip2archive:extract-text|$zip as xs:base64Binary, $entry-names as xs:string*, $encoding as xs:string|xs:string*}}<br/>
|-
| '''Summary'''
<pre class="brush:xquery">
let $archive := file:read-binary("documents.zip")
for $entry in zip2archive:entries($archive)[ends-with(., '.txt')]return zip2archive:extract-text($archive, $entry)
</pre>
|}
==zip2archive:extract-binary==
{| width='100%'
|-
| width='90' | '''Signatures'''
|{{Func|zip2archive:extract-binary|$zip as xs:base64Binary|xs:string*}}<br/>{{Func|zip2archive:extract-binary|$zip as xs:base64Binary, $entry-names as xs:string*|xs:base64Binary*}}
|-
| '''Summary'''
<pre class="brush:xquery">
let $archive := file:read-binary('archive.zip')
let $entries := zip2archive:entries($archive)let $contents := zip2archive:extract-binary($archive)
for $entry at $p in $entries
return file:write-binary($entry, $contents[$p])
|}
==zip2archive:update==
{| width='100%'
|-
| width='90' | '''Signatures'''
|{{Func|zip2archive:update|$zip as xs:base64Binary, $entries as element(entry)*, $contents as item()*|xs:base64Binary}}
|-
| '''Summary'''
|Adds new entries and replaces existing entries in a zip archive.<br/>The format of {{Code|$entries}} and {{Code|$contents}} is the same as for [[#zip2archive:create|zip2archive:create]].
|-
| '''Errors'''
let $archive := file:read-binary($input)
let $entry :=
copy $c := fn:parse-xml(zip2archive:extract-text($archive, $doc))
modify replace value of node $c//*[text() = "HELLO WORLD!"] with "HELLO UNIVERSE!"
return fn:serialize($c)
let $updated := zip2archive:update($archive, <entry>{ $doc }</entry>, $entry)
return file:write-binary($output, $updated)
</pre>
|}
==zip2archive:delete==
{| width='100%'
|-
| width='90' | '''Signatures'''
|{{Func|zip2archive:delete|$zip as xs:base64Binary, $entry-names as xs:string*|xs:base64Binary}}
|-
| '''Summary'''
<pre class="brush:xquery">
let $zip := file:read-binary('old.zip')
let $entries := zip2archive:entries($zip)[matches(., '\.x?html?$', 'i')]return file:write-binary('new.zip', zip2archive:delete($zip, $entries))
</pre>
|}