Changes

Jump to navigation Jump to search
204 bytes added ,  14:53, 27 February 2020
no edit 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:
| '''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')
return file:read-binary($path || $file)
)
return file:write-binary('mp3.zip', $zip)</presyntaxhighlight>
|}
| '''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'''
|'''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>
|}
| '''Examples'''
|A standard ZIP archive will return the following options:
<pre classsyntaxhighlight lang="brush:xquery">
map {
"format": "zip",
"algorithm": "deflate"
}
</presyntaxhighlight>
|}
| '''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>
|}
| '''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>
|}
| '''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>
|}
| '''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>
|}
| '''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>
|}
Bureaucrats, editor, reviewer, Administrators
13,550

edits

Navigation menu