Changes

Jump to navigation Jump to search
233 bytes added ,  14:53, 27 February 2020
no edit summary
=Conventions=
All functions and errors in this module are assigned to the <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><nowiki>http://basex.org/errors</nowiki></code> namespace, which is statically bound to the {{Code|bxerr}} prefix.
=Functions=
|-
| width='120' | '''Signatures'''
|{{Func|archive:create|$entries as item(), $contents as item()*|xs:base64Binary}}<br />{{Func|archive:create|$entries as item(), $contents as item()*, $options as map(xs:string, xs:string*)?|xs:base64Binary}}<br />
|-
| '''Summary'''
* {{Code|encoding}}: for textual entries (default: UTF-8)
An example:
<pre classsyntaxhighlight lang="brush:xml">
<archive:entry last-modified='2011-11-11T11:11:11'
compression-level='8'
encoding='US-ASCII'>hello.txt</archive:entry>
</presyntaxhighlight>
The actual {{Code|$contents}} must be {{Code|xs:string}} or {{Code|xs:base64Binary}} items.<br/>
The {{Code|$options}} parameter contains archiving options:
|-
| '''Errors'''
|{{Error|ARCH0001number|#Errors}} the number of entries and contents differs.<br />{{Error|ARCH0002format|#Errors}} the specified option or its value is invalid or not supported.<br />{{Error|ARCH0003descriptor|#Errors}} entry descriptors contain invalid entry names, timestamps or compression levels.<br/>{{Error|ARCH0004encode|#Errors}} the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if the <code>[[Options#CHECKSTRINGS{{Option|CHECKSTRINGS]]</code> option }} is turned off.<br/>{{Error|ARCH0005single|#Errors}} the chosen archive format only allows single entries.<br />{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
| '''Examples'''
|The following one-liner creates an archive {{Code|archive.zip}} with one file {{Code|file.txt}}:
<pre classsyntaxhighlight lang="brush:xquery">
archive:create(<archive:entry>file.txt</archive:entry>, 'Hello World')
</presyntaxhighlight>
The following function creates an archive {{Code|mp3.zip}}, which contains all MP3 files of a local directory:
<pre classsyntaxhighlight lang="brush:xquery">
let $path := 'audio/'
let $files := file:list($path, true(), '*.mp3')
let $zip := archive:create( $files ! element archive:entry { . }, for $file in $files ! return file:read-binary($path || .$file))return file:write-binary('mp3.zip', $zip)</presyntaxhighlight>
|}
|-
| width='120' | '''Signatures'''
|{{Func|archive:create-from|$path as xs:string|xs:base64Binary}}<br/>{{Func|archive:create-from|$path as xs:string, $options as map(xs:string, xs:string*)?|xs:base64Binary}}<br/>{{Func|archive:create-from|$path as xs:string, $options as map(xs:string, xs:string*)?, $entries as item()*|xs:base64Binary}}
|-
| '''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 the same as for identical to [[#archive:create|archive:create]], but two additional options are available:* {{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}}).
|-
| '''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|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
| '''Examples'''
|This example writes the files of a user’s home directory to <code>archive.zip</code>:
<pre classsyntaxhighlight lang="brush:xquery">
let $zip := archive:create-from('/home/user/')
return file:write-binary('archive.zip', $zip)
</presyntaxhighlight>
|}
* {{Code|compressed-size}}: compressed file size
An example:
<pre classsyntaxhighlight lang="brush:xml">
<archive:entry size="1840" last-modified="2009-03-20T03:30:32" compressed-size="672">
doc/index.html
</archive:entry>
</presyntaxhighlight>
|-
| '''Errors'''
|{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
|'''Examples'''
|Sums up the file sizes of all entries of a JAR file:
<pre classsyntaxhighlight lang="brush:xquery">
sum(archive:entries(file:read-binary('zip.zip'))/@size)
</presyntaxhighlight>
|}
|-
| width='120' | '''Signatures'''
|{{Func|archive:options|$archive as xs:base64Binary|elementmap(archive:options*)}}<br />
|-
| '''Summary'''
|-
| '''Errors'''
|{{Error|ARCH0002format|#Errors}} The packing format is not supported.<br/>{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
| '''Examples'''
|A standard ZIP archive will return the following options:
<pre classsyntaxhighlight lang="brush:xmlxquery"><archive:options xmlns:archive=map { "http://basex.org/modules/archiveformat"> <archive:format value="zip"/>, <archive"algorithm":algorithm value="deflate"/></archive:options>}</presyntaxhighlight>
|}
|-
| '''Errors'''
|{{Error|ARCH0004encode|#Errors}} the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if the <code>[[Options#CHECKSTRINGS{{Option|CHECKSTRINGS]]</code> option }} is turned off.<br />{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
| '''Examples'''
|The following expression extracts all {{Code|.txt}} files from an archive:
<pre classsyntaxhighlight lang="brush:xquery">
let $archive := file:read-binary("documents.zip")
for $entry in archive:entries($archive)[ends-with(., '.txt')]
return archive:extract-text($archive, $entry)
</presyntaxhighlight>
|}
|-
| '''Errors'''
|{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
| '''Examples'''
|This example unzips all files of an archive to the current directory:
<pre classsyntaxhighlight lang="brush:xquery">
let $archive := file:read-binary('archive.zip')
let $entries := archive:entries($archive)
file:create-dir(replace($entry, "[^/]+$", "")),
file:write-binary($entry, $content)
})</presyntaxhighlight>
|}
|-
| '''Errors'''
|{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
| '''Examples'''
|The following expression unzips all files of an archive to the current directory:
<pre classsyntaxhighlight lang="brush:xquery">
archive:extract-to('.', file:read-binary('archive.zip'))
</presyntaxhighlight>
|}
|-
| '''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 the <code>[[Options#CHECKSTRINGS{{Option|CHECKSTRINGS]]</code> option }} is turned off.<br />{{Error|ARCH0005modify|#Errors}} the entries of the given archive cannot be modified.<br/>{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
| '''Examples'''
|This example replaces texts in a Word document:
<pre classsyntaxhighlight lang="brush:xquery">
declare variable $input := "HelloWorld.docx";
declare variable $output := "HelloUniverse.docx";
let $updated := archive:update($archive, $doc, $entry)
return file:write-binary($output, $updated)
</presyntaxhighlight>
|}
|-
| '''Errors'''
|{{Error|ARCH0005modify|#Errors}} the entries of the given archive cannot be modified.<br/>{{Error|ARCH9999error|#Errors}} archive creation failed for some other reason.
|-
| '''Examples'''
|This example deletes all HTML files in an archive and creates a new file:
<pre classsyntaxhighlight lang="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))
</presyntaxhighlight>
|}
|Description
|-
|{{Code|ARCH0001descriptor}}|The number of specified entries and contents differsEntry descriptors contain invalid entry names, timestamps or compression levels.
|-
|{{Code|ARCH0002encode}}|The packing format or the specified option encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if {{Option|CHECKSTRINGS}} is turned off.
|-
|{{Code|ARCH0003error}}|Entry descriptors contain invalid entry names, timestamps or compression levelsArchive processing failed for some other reason.
|-
|{{Code|ARCH0004format}}|The packing format or the specified encoding option is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if the <code>[[Options#CHECKSTRINGS|CHECKSTRINGS]]</code> option is turned off.
|-
|{{Code|ARCH0005modify}}
|The entries of the given archive cannot be modified.
|-
|{{Code|ARCH0006number}}|The number of specified entries and contents differs.|-|{{Code|single}}
|The chosen archive format only allows single entries.
|-
|{{Code|ARCH9999}}
|Archive processing failed for some other reason.
|}
=Changelog=
 
;Version 9.0
 
* Updated: [[#archive:create-from|archive:create-from]]: options added
 
;Version 9.0
 
* Updated: error codes updated; errors now use the module namespace
 
;Version 8.5
 
* Updated: [[#archive:options|archive:options]]: map returned instead of element
;Version 8.3
 
* Added: [[#archive:create-from|archive:create-from]], [[#archive:extract-to|archive:extract-to]] (replaces <code>archive:write</code>)
;Version 7.7
 
* Added: [[#archive:write|archive:write]]
The module was introduced with Version 7.3.
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu