Changes

Jump to navigation Jump to search
4,826 bytes added ,  18:38, 1 December 2023
m
Text replacement - "syntaxhighlight" to "pre"
This [[Module Library|XQuery Module]] contains functions to handle ZIP archives(including ePub, Open Office, JAR, and many other formats). New ZIP and GZIP archives can be created, existing archives can be updated, and the archive entries can be listed and extracted. This module may soon replace The {{Function||archive:extract-binary}} function includes an example for writing the existing [[ZIP Module]] ([http://spex.basex.org/index.php?title=ZIP_Module more information])contents of an archive to disk.
=Conventions=
All functions and errors in this module are assigned to the {{Code|<code><nowiki>http://basex.org/modules/archive}} </nowiki></code> namespace, which is statically bound to the {{Code|archive}} prefix.<br/>All errors are assigned to the {{Code|http://basex.org/errors}} namespace, which is statically bound to the {{Code|bxerr}} prefix.
=FunctionsContent Handling==archive:create=={| width='100%'|-| width='90' | '''Signatures'''|{{Func|archive:create|$entries as element(archive:entry)*, $contents as item()*|xs:base64Binary}}<br />{{Func|archive:create|$entries as element(archive:entry)*, $contents as item()*, $options as item()|xs:base64Binary}}<br />|-| '''Summary'''|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, which is contained in the text node, further optional attributes can be specified:* {{Code|last-modified}}: timestamp, specified as xs:dateTime (default: current time)* {{Code|compression-level}}: 0-9, 0 = uncompressed (default: 8)* {{Code|encoding}}: for textual entries (default: UTF-8)An example:<pre class="brush:xml"><archive:entry last-modified='2011-11-11T11:11:11' compression-level='9' encoding='US-ASCII'>hello.txt</entry></pre>The actual {{Code|$contents}} must be {{Code|xs:string}} or {{Code|xs:base64Binary}} items.<br/>The {{Code|$options}} parameter contains archiving options, which can either be specified* as children of an {{Code|<archive:options/>}} element:<pre class="brush:xml"><archive:options> <archive:format value="zip"/> <archive:algorithm value="deflate"/></archive:options></pre>* as map, which contains all key/value pairs:<pre class="brush:xml">map { "format" := "zip", "algorithm" := "deflate" }</pre>Currently, only the default options are supported, which are shown in the above examples. |-| '''Errors'''|{{Error|ARCH0001|#Errors}} the number of entries and contents differs.<br />{{Error|ARCH0003|#Errors}} entry descriptors contain invalid entry names, timestamps or compression levels.<br/>{{Error|ARCH0004|#Errors}} the specified encoding is invalid or not supported, or the string conversion failed.<br/>{{Error|ARCH0005|#Errors}} the specified option or its value is invalid or not supported.<br />{{Error|ARCH9999|#Errors}} archive creation failed for some other reason.<br />{{Error|FORG0006|XQuery Errors#Function Errors}} (some of) the contents are not of type {{Code|xs:string}} or {{Code|xs:base64Binary}}.|-| '''Examples'''|The following one-liner creates an archive {{Code|archive.zip}} with one file {{Code|file.txt}}:<pre class="brush:xquery">archive:create(<archive:entry>file.txt</archive:entry>, 'Hello World')</pre>The following function creates an archive {{Code|mp3.zip}}, which contains all MP3 files of a local directory:<pre class="brush:xquery">let $path := 'audio/'let $files := file:list($path, true(), '*.mp3')let $zip := archive:create( for $f in $files return <archive:entry>{ $f }</archive:entry>, for $f in $files return file:read-binary($path || $f))return file:write-binary('mp3.zip', $zip)</pre>|}
==archive:entries==
{| width='100%'
|-valign="top"| width='90120' | '''SignaturesSignature'''|{{Func|<pre>archive:entries|( $zip archive as xs:base64Binary|) as element(archive:entry)*}}<br /pre>|-valign="top"
| '''Summary'''
|Returns the entry descriptors of the given zip specified {{Code|$archive}}. A descriptor contains the following attributes, provided that they are available in the archive format:
* {{Code|size}}: original file size
* {{Code|last-modified}}: timestamp, formatted as xs:dateTime
* {{Code|compressed-size}}: compressed file size
An example:
<pre classlang="brush:xml">
<archive:entry size="1840" last-modified="2009-03-20T03:30:32" compressed-size="672">
doc/index.html
</archive:entry>
</pre>
|-valign="top"
| '''Errors'''
|{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.|-valign="top"
|'''Examples'''
|Sums up the file sizes of all entries of a JAR file:
<pre classlang="brush:'xquery"'>
sum(archive:entries(file:read-binary('zip.zip'))/@size)
</pre>
{| width='100%'
|-valign="top"| width='90120' | '''SignaturesSignature'''|{{Func|<pre>archive:options|( $zip archive as xs:base64Binary|element) as map(archive:options*)*}}<br /pre>|-valign="top"
| '''Summary'''
|Returns the options of the given zip specified {{Code|$archive, as }} in the format specified by the [[#archive:create{{Function||archive:create]] function}}.|-valign="top"
| '''Errors'''
|{{Error|ARCH0002format|#Errors}} The packing archive format is not supported.<br/>{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.|- valign="top"| '''Examples'''|A standard ZIP archive will return the following options:<pre lang='xquery'>map { "format": "zip", "algorithm": "deflate"}</pre>
|}
{| width='100%'
|-valign="top"| width='90120' | '''SignaturesSignature'''|{{Func|archive:extract-text|$zip as xs:base64Binary|xs:string*}}<br/pre>{{Func|archive:extract-text|( $zip archive as xs:base64Binary, $entry-names entries as xs:stringitem()*|xs :string*}}<br/>{{Func|archive:extract-text|$zip as xs:base64Binary= (), $entry-names encoding as xs:string*, $encoding := ()) as xs:string|xs:string*}}<br/pre>|-valign="top"
| '''Summary'''
|Extracts entries of the specified {{Code|$archive entries }} and returns them as texts.<br/>The returned entries can be limited to via {{Code|$entry-namesentries}}. The format of the argument is the same as for {{Function||archive:create}}(attributes will be ignored).<br/>The optional parameter encoding of the input files can be specified via {{Code|$encoding}} defines the encoding of the file.|-valign="top"
| '''Errors'''
|{{Error|ARCH0004encode|#Errors}} the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if {{Option|CHECKSTRINGS}} is turned off.<br />{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.|-valign="top"
| '''Examples'''
|The following expression extracts all {{Code|.txt}} files from an archive:
<pre classlang="brush:'xquery"'>
let $archive := file:read-binary("documents.zip")
for $entry in archive:entries($archive)[ends-with(., '.txt')]
{| width='100%'
|-valign="top"| width='90120' | '''SignaturesSignature'''|{{Func|archive:extract-binary|$zip as xs:base64Binary|xs:string*}}<br/pre>{{Func|archive:extract-binary|( $zip archive as xs:base64Binary, $entry-names entries as xsitem()* :string*|= ()) as xs:base64Binary*}}</pre>|-valign="top"
| '''Summary'''
|Extracts entries of the specified {{Code|$archive entries }} and returns them as binaries.<br/>The returned entries can be limited to via {{Code|$entry-namesentries}}. The format of the argument is the same as for {{Function||archive:create}}(attributes will be ignored).|-valign="top"
| '''Errors'''
|{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.|-valign="top"
| '''Examples'''
|This example unzips all files of an archive to the current directory:
<pre classlang="brush:'xquery"'>
let $archive := file:read-binary('archive.zip')
let $entries := archive:entries($archive)
let $contents := archive:extract-binary($archive)
return for -each-pair($entries, $contents, function($entry at , $content) { file:create-dir(replace($p in entry, "[^/]+$entries", "")),return file:write-binary($entry, $content)})</pre>|} =Updates= ==archive:create== {| width='100%'|- valign="top"| width='120' | '''Signature''' |<pre>archive:create( $entries as item(), $contents as item()*, $options as map(*)? := map { }) as xs:base64Binary</pre>|- valign="top"| '''Summary'''|Creates a new archive from the specified entries and contents[.<br/>The {{Code|$entries}} argument contains meta information required to create new entries. All items may either be of type {{Code|xs:string}}, representing the entry name, or {{Code|element(archive:entry)}}, containing the name as text node and additional, optional attributes:* {{Code|last-modified}}: timestamp, specified as xs:dateTime (default: current time)* {{Code|compression-level}}: 0-9, 0 = uncompressed (default: 8)* {{Code|encoding}}: for textual entries (default: UTF-8)An example:<pre lang="xml"><archive:entry last-modified='2011-11-11T11:11:11' compression-level='8' encoding='US-ASCII'>hello.txt</archive:entry></pre>The actual {{Code|$contents}} must be {{Code|xs:string}} or {{Code|xs:base64Binary}} items.<br/>The {{Code|$p]options}} parameter contains archiving options:* {{Code|format}}: allowed values are {{Code|zip}} and {{Code|gzip}}. {{Code|zip}} is the default.* {{Code|algorithm}}: allowed values are {{Code|deflate}} and {{Code|stored}} (for the {{Code|zip}} format). {{Code|deflate}} is the default.|- valign="top"| '''Errors'''|{{Error|number|#Errors}} the number of entries and contents differs.<br/>{{Error|format|#Errors}} the specified option or its value is invalid or not supported.<br/>{{Error|descriptor|#Errors}} entry descriptors contain invalid entry names, timestamps or compression levels.<br/>{{Error|encode|#Errors}} the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if {{Option|CHECKSTRINGS}} is turned off.<br/>{{Error|single|#Errors}} the chosen archive format only allows single entries.<br/>{{Error|error|#Errors}} archive creation failed.|- valign="top"| '''Examples'''|The following one-liner creates an archive {{Code|archive.zip}} with one file {{Code|file.txt}}:<pre lang='xquery'>archive:create(<archive:entry>file.txt</archive:entry>, 'Hello World')
</pre>
The following function creates an archive {{Code|mp3.zip}}, which contains all MP3 files of a local directory:
<pre lang='xquery'>
let $path := 'audio/'
let $files := file:list($path, true(), '*.mp3')
let $zip := archive:create($files,
for $file in $files
return file:read-binary($path || $file)
)
return file:write-binary('mp3.zip', $zip)</pre>
|}
{| width='100%'
|-valign="top"| width='90120' | '''SignaturesSignature'''|{{Func|<pre>archive:update|( $zip archive as xs:base64Binary, $entries as elementitem(entry)*, $contents as item()*|) as xs:base64Binary}}</pre>|-valign="top"
| '''Summary'''
|Adds Creates an updated version of the specified {{Code|$archive}} with new or replaced entries and replaces existing entries in a zip archive.<br/>The format of {{Code|$entries}} and {{Code|$contents}} is the same as for [[#archive:create{{Function||archive:create]]}}.|-valign="top"
| '''Errors'''
|{{Error|ARCH0001number|#Errors}} the number of entries and contents differs.<br />{{Error|ARCH0003descriptor|#Errors}} entry descriptors contain invalid entry names, timestamps, compression levels or encodings.<br/>{{Error|ARCH0004encode|#Errors}} the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if {{Option|CHECKSTRINGS}} is turned off.<br />{{Error|ARCH9999modify|#Errors}} the entries of the given archive creation failed for some other reasoncannot be modified.<br />{{Error|FORG0006error|XQuery Errors#Function Errors}} (some of) the contents are not of type {{Code|xs:string}} or {{Code|xs:base64Binary}}archive creation failed.|-valign="top"
| '''Examples'''
|This example replaces texts in a Word document:
<pre classlang="brush:'xquery"'>
declare variable $input := "HelloWorld.docx";
declare variable $output := "HelloUniverse.docx";
modify replace value of node $c//*[text() = "HELLO WORLD!"] with "HELLO UNIVERSE!"
return fn:serialize($c)
let $updated := archive:update($archive, <entry>{ $doc }</entry>, $entry)
return file:write-binary($output, $updated)
</pre>
{| width='100%'
|-valign="top"| width='90120' | '''SignaturesSignature'''|{{Func|<pre>archive:delete|( $zip archive as xs:base64Binary, $entry-names entries as xs:stringitem()*|) as xs:base64Binary}}</pre>|-valign="top"
| '''Summary'''
|Deletes entries from a zip an {{Code|$archive}}.<br/>The format of {{Code|$entry-namesentries}} is the list of entries to be deletedsame as for {{Function||archive:create}}.|-valign="top"
| '''Errors'''
|{{Error|ARCH9999modify|#Errors}} the entries of the given archive cannot be modified.<br/>{{Error|error|#Errors}} archive creation failed for some other reason.|-valign="top"
| '''Examples'''
|This example deletes all HTML files in an archive and creates a new file:
<pre classlang="brush:'xquery"'>
let $zip := file:read-binary('old.zip')
let $entries := archive:entries($zip)[matches(., '\.x?html?$', 'i')]
return file:write-binary('new.zip', archive:delete($zip, $entries))
</pre>
|}
 
=Convenience=
 
==archive:create-from==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>archive:create-from(
$path as xs:string,
$options as map(*)? := map { },
$entries as item()* := ()
) as xs:base64Binary</pre>
|- valign="top"
| '''Summary'''
|This convenience function creates an archive from all files in the specified directory {{Code|$path}}.<br/>The {{Code|$options}} parameter contains archiving options, and the files to be archived can be limited via {{Code|$entries}}. The format of the two last arguments is identical to {{Function||archive:create}}, with two additional options:
* {{Code|recursive}}: parse all files recursively (default: {{Code|true}}; ignored if entries are specified via the last argument).
* {{Code|root-dir}}: use name of supplied directory as archive root directory (default: {{Code|false}}).
|- valign="top"
| '''Errors'''
|{{Error|file:no-dir|File Module#Errors}} the specified path does not point to a directory.<br/>{{Error|file:is-dir|File Module#Errors}} one of the specified entries points to a directory.<br/>{{Error|file:not-found|File Module#Errors}} a specified entry does not exist.<br/>{{Error|error|#Errors}} archive creation failed.
|- valign="top"
| '''Examples'''
|This example writes the files of a user’s home directory to <code>archive.zip</code>:
<pre lang='xquery'>
let $zip := archive:create-from('/home/user/')
return file:write-binary('archive.zip', $zip)
</pre>
|}
 
==archive:extract-to==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>archive:extract-to(
$path as xs:string,
$archive as xs:base64Binary,
$entries as item()* := ()
) as empty-sequence()</pre>
|- valign="top"
| '''Summary'''
|This convenience function writes files of an {{Code|$archive}} directly to the specified directory {{Code|$path}}.<br/>The archive entries to be written can be restricted via {{Code|$entries}}. The format of the argument is the same as for {{Function||archive:create}} (attributes will be ignored).
|- valign="top"
| '''Errors'''
|{{Error|error|#Errors}} archive creation failed.
|- valign="top"
| '''Examples'''
|The following expression unzips all files of an archive to the current directory:
<pre lang='xquery'>
archive:extract-to('.', file:read-binary('archive.zip'))
</pre>
|}
 
==archive:write==
 
{| width='100%'
|- valign="top"
| width='120' | '''Signature'''
|<pre>archive:write(
$path as xs:string,
$entries as item(),
$contents as item()*,
$options as map(*)? := map { }
) as xs:base64Binary</pre>
|- valign="top"
| '''Summary'''
|This convenience function creates a new archive from the specified {{Code|$entries}} and {{Code|$contents}} and writes it disk.<br/> See {{Function||archive:create}} for more details.
|- valign="top"
| '''Errors'''
|{{Error|number|#Errors}} the number of entries and contents differs.<br/>{{Error|format|#Errors}} the specified option or its value is invalid or not supported.<br/>{{Error|descriptor|#Errors}} entry descriptors contain invalid entry names, timestamps or compression levels.<br/>{{Error|encode|#Errors}} the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if {{Option|CHECKSTRINGS}} is turned off.<br/>{{Error|single|#Errors}} the chosen archive format only allows single entries.<br/>{{Error|error|#Errors}} archive creation failed.
|- valign="top"
| '''Examples'''
|All mp3 files from a directory are zipped and written to a file, along with an info file:
<pre lang='xquery'>
let $files := file:children('music')[ends-with(., 'mp3')]
return archive:write(
'music.zip',
('info.txt', $files ! file:name(.)),
('Archive with MP3 files', $files ! file:read-binary(.))
)
</pre>
|}
{| class="wikitable" width="100%"
! width="5%110"|Code! width|Description|- valign="top"|{{Code|descriptor}}|Entry descriptors contain invalid entry names, timestamps or compression levels.|- valign="95%top"|{{Code|encode}}|DescriptionThe specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if {{Option|CHECKSTRINGS}} is turned off.|- valign="top"|{{Code|error}}| processing failed.|- valign="top"|{{Code|format}}|The archive format or the specified option is invalid or not supported.|- valign="top"|{{Code|modify}}|The entries of the given archive cannot be modified.|-valign="top"|{{Code|ARCH0001number}}
|The number of specified entries and contents differs.
|-valign="top"|{{Code|ARCH0002single}}|The packing chosen archive format is unknown or not supported.|-|{{Code|ARCH0003}}|Entry descriptors contain invalid entry names, timestamps or compression levels.|-|{{Code|ARCH0004}}|The specified encoding is invalid or not supported, or the string conversion failed.|-|{{Code|ARCH0005}}|The specified option or its value is invalid or not supported.|-|{{Code|ARCH9999}}|ZIP processing failed for some other reasononly allows single entries.
|}
=Changelog=
 
;Version 9.6
* Added: {{Function||archive:write}}
 
;Version 9.0
* Updated: {{Function||archive:create-from}}: options added
* Updated: error codes updated; errors now use the module namespace
 
;Version 8.5
* Updated: {{Function||archive:options}}: map returned instead of element
 
;Version 8.3
* Added: {{Function||archive:create-from}}, {{Function||archive:extract-to}} (replaces <code>archive:write</code>)
The module was introduced with Version 7.3.
 
[[Category:XQuery]]
Bureaucrats, editor, reviewer, Administrators
13,554

edits

Navigation menu