Difference between revisions of "Archive Module"
m (Text replacement - "<br />" to "<br/>") |
|||
Line 12: | Line 12: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:entries( |
+ | $archive as xs:base64Binary | ||
+ | ) as element(archive:entry)*</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 41: | Line 43: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:options( |
+ | $archive as xs:base64Binary | ||
+ | ) as map(*)</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 64: | Line 68: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:extract-text( |
+ | $archive as xs:base64Binary | ||
+ | $entries as item()* := () | ||
+ | $encoding as xs:string := () | ||
+ | ) as xs:string*</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 86: | Line 94: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:extract-binary( |
+ | $archive as xs:base64Binary | ||
+ | $entries as item()* := () | ||
+ | ) as xs:base64Binary*</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 113: | Line 124: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:create( |
+ | $entries as item() | ||
+ | $contents as item()* | ||
+ | $options as map(*)? := () | ||
+ | ) as xs:base64Binary</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 155: | Line 170: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:update( |
+ | $archive as xs:base64Binary | ||
+ | $entries as item()* | ||
+ | $contents as item()* | ||
+ | ) as xs:base64Binary</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 185: | Line 204: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:delete( |
+ | $archive as xs:base64Binary | ||
+ | $entries as item()* | ||
+ | ) as xs:base64Binary</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 209: | Line 231: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:create-from( |
+ | $path as xs:string | ||
+ | $options as map(*)? := () | ||
+ | $entries as item()* := () | ||
+ | ) as xs:base64Binary</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 232: | Line 258: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:extract-to( |
+ | $path as xs:string | ||
+ | $archive as xs:base64Binary | ||
+ | $entries as item()* := () | ||
+ | ) as empty-sequence()</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' | ||
Line 252: | Line 282: | ||
|- valign="top" | |- valign="top" | ||
| width='120' | '''Signatures''' | | width='120' | '''Signatures''' | ||
− | | | + | |<pre>archive:write( |
+ | $path as xs:string | ||
+ | $entries as item() | ||
+ | $contents as item()* | ||
+ | $options as map(*)? := () | ||
+ | ) as xs:base64Binary</pre> | ||
|- valign="top" | |- valign="top" | ||
| '''Summary''' | | '''Summary''' |
Revision as of 13:02, 9 March 2023
This XQuery Module contains functions to handle 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. The archive:extract-binary
function includes an example for writing the contents of an archive to disk.
Contents
Conventions
All functions and errors in this module are assigned to the http://basex.org/modules/archive
namespace, which is statically bound to the archive
prefix.
Content Handling
archive:entries
Signatures | archive:entries( $archive as xs:base64Binary ) as element(archive:entry)* |
Summary | Returns the entry descriptors of the specified $archive . A descriptor contains the following attributes, provided that they are available in the archive format:
An example: <syntaxhighlight lang="xml"> <archive:entry size="1840" last-modified="2009-03-20T03:30:32" compressed-size="672"> doc/index.html </archive:entry> </syntaxhighlight> |
Errors | error : archive creation failed.
|
Examples | Sums up the file sizes of all entries of a JAR file:
<syntaxhighlight lang="xquery"> sum(archive:entries(file:read-binary('zip.zip'))/@size) </syntaxhighlight> |
archive:options
Signatures | archive:options( $archive as xs:base64Binary ) as map(*) |
Summary | Returns the options of the specified $archive in the format specified by archive:create .
|
Errors | format : The archive format is not supported.error : archive creation failed.
|
Examples | A standard ZIP archive will return the following options:
<syntaxhighlight lang="xquery"> map { "format": "zip", "algorithm": "deflate" } </syntaxhighlight> |
archive:extract-text
Signatures | archive:extract-text( $archive as xs:base64Binary $entries as item()* := () $encoding as xs:string := () ) as xs:string* |
Summary | Extracts entries of the specified $archive and returns them as texts.The returned entries can be limited via $entries . The format of the argument is the same as for archive:create (attributes will be ignored).The encoding of the input files can be specified via $encoding .
|
Errors | encode : the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if CHECKSTRINGS is turned off.error : archive creation failed.
|
Examples | The following expression extracts all .txt files from an archive:
<syntaxhighlight lang="xquery"> let $archive := file:read-binary("documents.zip") for $entry in archive:entries($archive)[ends-with(., '.txt')] return archive:extract-text($archive, $entry) </syntaxhighlight> |
archive:extract-binary
Signatures | archive:extract-binary( $archive as xs:base64Binary $entries as item()* := () ) as xs:base64Binary* |
Summary | Extracts entries of the specified $archive and returns them as binaries.The returned entries can be limited via $entries . The format of the argument is the same as for archive:create (attributes will be ignored).
|
Errors | error : archive creation failed.
|
Examples | This example unzips all files of an archive to the current directory:
<syntaxhighlight lang="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, $content) { file:create-dir(replace($entry, "[^/]+$", "")), file:write-binary($entry, $content) })</syntaxhighlight> |
Updates
archive:create
Signatures | archive:create( $entries as item() $contents as item()* $options as map(*)? := () ) as xs:base64Binary |
Summary | Creates a new archive from the specified entries and contents. The $entries argument contains meta information required to create new entries. All items may either be of type xs:string , representing the entry name, or element(archive:entry) , containing the name as text node and additional, optional attributes:
An example: <syntaxhighlight lang="xml"> <archive:entry last-modified='2011-11-11T11:11:11' compression-level='8' encoding='US-ASCII'>hello.txt</archive:entry> </syntaxhighlight>
The actual
|
Errors | number : the number of entries and contents differs.format : the specified option or its value is invalid or not supported.descriptor : entry descriptors contain invalid entry names, timestamps or compression levels.encode : the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if CHECKSTRINGS is turned off.single : the chosen archive format only allows single entries.error : archive creation failed.
|
Examples | The following one-liner creates an archive archive.zip with one file file.txt :
<syntaxhighlight lang="xquery">
archive:create(<archive:entry>file.txt</archive:entry>, 'Hello World')
</syntaxhighlight>
The following function creates an archive for $file in $files return file:read-binary($path || $file) ) return file:write-binary('mp3.zip', $zip)</syntaxhighlight> |
archive:update
Signatures | archive:update( $archive as xs:base64Binary $entries as item()* $contents as item()* ) as xs:base64Binary |
Summary | Creates an updated version of the specified $archive with new or replaced entries.The format of $entries and $contents is the same as for archive:create .
|
Errors | number : the number of entries and contents differs.descriptor : entry descriptors contain invalid entry names, timestamps, compression levels or encodings.encode : the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if CHECKSTRINGS is turned off.modify : the entries of the given archive cannot be modified.error : archive creation failed.
|
Examples | This example replaces texts in a Word document:
<syntaxhighlight lang="xquery"> declare variable $input := "HelloWorld.docx"; declare variable $output := "HelloUniverse.docx"; declare variable $doc := "word/document.xml"; let $archive := file:read-binary($input) let $entry := copy $c := fn:parse-xml(archive:extract-text($archive, $doc)) modify replace value of node $c//*[text() = "HELLO WORLD!"] with "HELLO UNIVERSE!" return fn:serialize($c) let $updated := archive:update($archive, $doc, $entry) return file:write-binary($output, $updated) </syntaxhighlight> |
archive:delete
Signatures | archive:delete( $archive as xs:base64Binary $entries as item()* ) as xs:base64Binary |
Summary | Deletes entries from an $archive .The format of $entries is the same as for archive:create .
|
Errors | modify : the entries of the given archive cannot be modified.error : archive creation failed.
|
Examples | This example deletes all HTML files in an archive and creates a new file:
<syntaxhighlight lang="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)) </syntaxhighlight> |
Convenience
archive:create-from
Signatures | archive:create-from( $path as xs:string $options as map(*)? := () $entries as item()* := () ) as xs:base64Binary |
Summary | This convenience function creates an archive from all files in the specified directory $path .The $options parameter contains archiving options, and the files to be archived can be limited via $entries . The format of the two last arguments is identical to archive:create , but two additional options are available:
|
Errors | file:no-dir : the specified path does not point to a directory.file:is-dir : one of the specified entries points to a directory.file:not-found : a specified entry does not exist.error : archive creation failed.
|
Examples | This example writes the files of a user’s home directory to archive.zip :
<syntaxhighlight lang="xquery"> let $zip := archive:create-from('/home/user/') return file:write-binary('archive.zip', $zip) </syntaxhighlight> |
archive:extract-to
Signatures | archive:extract-to( $path as xs:string $archive as xs:base64Binary $entries as item()* := () ) as empty-sequence() |
Summary | This convenience function writes files of an $archive directly to the specified directory $path .The archive entries to be written can be restricted via $entries . The format of the argument is the same as for archive:create (attributes will be ignored).
|
Errors | error : archive creation failed.
|
Examples | The following expression unzips all files of an archive to the current directory:
<syntaxhighlight lang="xquery"> archive:extract-to('.', file:read-binary('archive.zip')) </syntaxhighlight> |
archive:write
Signatures | archive:write( $path as xs:string $entries as item() $contents as item()* $options as map(*)? := () ) as xs:base64Binary |
Summary | This convenience function creates a new archive from the specified $entries and $contents and writes it disk.See archive:create for more details.
|
Errors | number : the number of entries and contents differs.format : the specified option or its value is invalid or not supported.descriptor : entry descriptors contain invalid entry names, timestamps or compression levels.encode : the specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if CHECKSTRINGS is turned off.single : the chosen archive format only allows single entries.error : archive creation failed.
|
Examples | All mp3 files from a directory are zipped and written to a file, along with an info file:
<syntaxhighlight 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(.)) ) </syntaxhighlight> |
Errors
Code | Description |
---|---|
descriptor
|
Entry descriptors contain invalid entry names, timestamps or compression levels. |
encode
|
The specified encoding is invalid or not supported, or the string conversion failed. Invalid XML characters will be ignored if CHECKSTRINGS is turned off.
|
error
|
processing failed. |
format
|
The archive format or the specified option is invalid or not supported. |
modify
|
The entries of the given archive cannot be modified. |
number
|
The number of specified entries and contents differs. |
single
|
The chosen archive format only allows single entries. |
Changelog
- Version 9.6
- Added:
archive:write
- Version 9.0
- Updated:
archive:create-from
: options added - Updated: error codes updated; errors now use the module namespace
- Version 8.5
- Updated:
archive:options
: map returned instead of element
- Version 8.3
- Added:
archive:create-from
,archive:extract-to
(replacesarchive:write
)
The module was introduced with Version 7.3.