Difference between revisions of "ZIP Module"

From BaseX Documentation
Jump to navigation Jump to search
Line 5: Line 5:
 
=Conventions=
 
=Conventions=
  
All functions in this module are assigned to the <code>http://expath.org/ns/zip</code> namespace, which is statically bound to the <code>zip</code> prefix.<br/>
+
All functions in this module are assigned to the {{Code|http://expath.org/ns/zip}} namespace, which is statically bound to the {{Code|zip}} prefix.<br/>
All errors are assigned to the <code>http://expath.org/ns/error</code> namespace, which is statically bound to the <code>exerr</code> prefix.
+
All errors are assigned to the {{Code|http://expath.org/ns/error}} namespace, which is statically bound to the {{Code|exerr}} prefix.
  
 
=Functions=
 
=Functions=
Line 14: Line 14:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code>'''zip:binary-entry'''($uri as xs:string, $path as xs:string) as xs:base64Binary</code>
+
|{{Func|zip:binary-entry|$uri as xs:string, $path as xs:string|xs:base64Binary}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Extracts the binary file at <code>$path</code> within the ZIP file located at <code>$uri</code> and returns it as an <code>xs:base64Binary</code> item.
+
|Extracts the binary file at {{Code|$path}} within the ZIP file located at {{Code|$uri}} and returns it as an {{Code|xs:base64Binary}} item.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#ZIP Functions Errors|FOZP0001]]''' is raised if the specified path does not exist.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0003]]''' is raised if the operation fails for some other reason.
+
|{{Error|FOZP0001|XQuery Errors#ZIP Functions Errors}} the specified path does not exist.<br />{{Error|FOZP0003|XQuery Errors#ZIP Functions Errors}} the operation fails for some other reason.
 
|}
 
|}
  
Line 27: Line 27:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code>'''zip:text-entry'''($uri as xs:string, $path as xs:string) as xs:string</code><br /><code>'''zip:text-entry'''($uri as xs:string, $path as xs:string, $encoding as xs:string) as xs:string</code>
+
|{{Func|zip:text-entry|$uri as xs:string, $path as xs:string|xs:string}}<br />{{Func|zip:text-entry|$uri as xs:string, $path as xs:string, $encoding as xs:string|xs:string}}
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Extracts the text file at <code>$path</code> within the ZIP file located at <code>$uri</code> and returns it as an <code>xs:string</code> item.<br />An optional encoding can be specified via <code>$encoding</code>.
+
|Extracts the text file at {{Code|$path}} within the ZIP file located at {{Code|$uri}} and returns it as an {{Code|xs:string}} item.<br />An optional encoding can be specified via {{Code|$encoding}}.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#ZIP Functions Errors|FOZP0001]]''' is raised if the specified path does not exist.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0003]]''' is raised if the operation fails for some other reason.
+
|{{Error|FOZP0001|XQuery Errors#ZIP Functions Errors}} the specified path does not exist.<br />{{Error|FOZP0003|XQuery Errors#ZIP Functions Errors}} the operation fails for some other reason.
 
|}
 
|}
  
Line 40: Line 40:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code>'''zip:xml-entry'''($uri as xs:string, $path as xs:string) as document-node()</code><br />
+
|{{Func|zip:xml-entry|$uri as xs:string, $path as xs:string|document-node()}}<br />
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Extracts the XML file at <code>$path</code> within the ZIP file located at <code>$uri</code> and returns it as a document node.
+
|Extracts the XML file at {{Code|$path}} within the ZIP file located at {{Code|$uri}} and returns it as a document node.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#ZIP Functions Errors|FODC0006]]''' is raised if the addressed file is not well-formed.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0001]]''' is raised if the specified path does not exist.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0003]]''' is raised if the operation fails for some other reason.
+
|{{Error|FODC0006|XQuery Errors#ZIP Functions Errors}} the addressed file is not well-formed.<br />{{Error|FOZP0001|XQuery Errors#ZIP Functions Errors}} the specified path does not exist.<br />{{Error|FOZP0003|XQuery Errors#ZIP Functions Errors}} the operation fails for some other reason.
 
|}
 
|}
  
Line 53: Line 53:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code>'''zip:html-entry'''($uri as xs:string, $path as xs:string) as document-node()</code><br />
+
|{{Func|zip:html-entry|$uri as xs:string, $path as xs:string|document-node()}}<br />
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Extracts the HTML file at <code>$path</code> within the ZIP file located at <code>$uri</code> and returns it as a document node. The file is converted to XML first if [http://home.ccil.org/~cowan/XML/tagsoup/ Tagsoup] is found in the classpath.
+
|Extracts the HTML file at {{Code|$path}} within the ZIP file located at {{Code|$uri}} and returns it as a document node. The file is converted to XML first if [http://home.ccil.org/~cowan/XML/tagsoup/ Tagsoup] is found in the classpath.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#ZIP Functions Errors|FODC0006]]''' is raised if the addressed file is not well-formed, or cannot be converted to correct XML.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0001]]''' is raised if the specified path does not exist.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0003]]''' is raised if the operation fails for some other reason.
+
|{{Error|FODC0006|XQuery Errors#ZIP Functions Errors}} the addressed file is not well-formed, or cannot be converted to correct XML.<br />{{Error|FOZP0001|XQuery Errors#ZIP Functions Errors}} the specified path does not exist.<br />{{Error|FOZP0003|XQuery Errors#ZIP Functions Errors}} the operation fails for some other reason.
 
|}
 
|}
  
Line 66: Line 66:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code>'''zip:entries'''($uri as xs:string) as element(zip:file)</code><br />
+
|{{Func|zip:entries|$uri as xs:string|element(zip:file)}}<br />
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Generates an [http://expath.org/spec/zip#spec-file-handling-elements-sect ZIP XML Representation] of the hierarchical structure of the ZIP file located at <code>$uri</code> and returns it as an element node. The file contents are not returned by this function.
+
|Generates an [http://expath.org/spec/zip#spec-file-handling-elements-sect ZIP XML Representation] of the hierarchical structure of the ZIP file located at {{Code|$uri}} and returns it as an element node. The file contents are not returned by this function.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#ZIP Functions Errors|FOZP0001]]''' is raised if the specified path does not exist.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0003]]''' is raised if the operation fails for some other reason.
+
|{{Error|FOZP0001|XQuery Errors#ZIP Functions Errors}} the specified path does not exist.<br />{{Error|FOZP0003|XQuery Errors#ZIP Functions Errors}} the operation fails for some other reason.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* If the ZIP archive <code>archive.zip</code> is empty, <code>zip:entries('archive.zip')</code> returns <code><zip:file xmlns:zip="http://expath.org/ns/zip" href="archive.zip"/></code>.
+
* If the ZIP archive {{Code|archive.zip}} is empty, {{Code|zip:entries('archive.zip')}} returns {{Code|<zip:file xmlns:zip="http://expath.org/ns/zip" href="archive.zip"/>}}.
 
|}
 
|}
  
Line 83: Line 83:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code>'''zip:zip-file'''($zip as element(zip:file)) as empty-sequence()</code><br />
+
|{{Func|zip:zip-file|$zip as element(zip:file)|empty-sequence()}}<br />
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Creates a new ZIP archive with the characteristics described by <code>$zip</code>, the [http://expath.org/spec/zip#spec-file-handling-elements-sect ZIP XML Representation].
+
|Creates a new ZIP archive with the characteristics described by {{Code|$zip}}, the [http://expath.org/spec/zip#spec-file-handling-elements-sect ZIP XML Representation].
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#ZIP Functions Errors|FOZP0001]]''' is raised if an addressed file does not exist.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0002]]''' is raised if entries in the ZIP archive description are unknown, missing, or invalid.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0003]]''' is raised if the operation fails for some other reason.<br />'''[[XQuery Errors#Serialization Errors|Serialization Errors]]''' are raised if an inlined XML fragment cannot be successfully serialized.
+
|{{Error|FOZP0001|XQuery Errors#ZIP Functions Errors}} an addressed file does not exist.<br />{{Error|FOZP0002|XQuery Errors#ZIP Functions Errors}} entries in the ZIP archive description are unknown, missing, or invalid.<br />{{Error|FOZP0003|XQuery Errors#ZIP Functions Errors}} the operation fails for some other reason.<br />{{Error|Serialization Errors|XQuery Errors#Serialization Errors}} an inlined XML fragment cannot be successfully serialized.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* The following function creates a file <code>archive.zip</code> with the file <code>file.txt</code> inside:
+
* The following function creates a file {{Code|archive.zip}} with the file {{Code|file.txt}} inside:
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
 
zip:zip-file(
 
zip:zip-file(
Line 100: Line 100:
 
   </file>)
 
   </file>)
 
</pre>
 
</pre>
* The following function creates a file <code>archive.zip</code>. It contains one file <code>readme</code> with the content "<code>thanks</code>":
+
* The following function creates a file {{Code|archive.zip}}. It contains one file {{Code|readme}} with the content "{{Code|thanks}}":
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
 
zip:zip-file(
 
zip:zip-file(
Line 113: Line 113:
 
|-
 
|-
 
| width='90' | '''Signatures'''
 
| width='90' | '''Signatures'''
|<code>'''zip:update-entries'''($zip as element(zip:file), $output as xs:string) as empty-sequence()</code><br />
+
|{{Func|zip:update-entries|$zip as element(zip:file), $output as xs:string|empty-sequence()}}<br />
 
|-
 
|-
 
| '''Summary'''
 
| '''Summary'''
|Updates an existing ZIP archive or creates a modifed copy, based on the characteristics described by <code>$zip</code>, the [http://expath.org/spec/zip#spec-file-handling-elements-sect ZIP XML Representation]. The <code>$output</code> argument is the URI where the modified ZIP file is copied to.
+
|Updates an existing ZIP archive or creates a modifed copy, based on the characteristics described by {{Code|$zip}}, the [http://expath.org/spec/zip#spec-file-handling-elements-sect ZIP XML Representation]. The {{Code|$output}} argument is the URI where the modified ZIP file is copied to.
 
|-
 
|-
 
| '''Errors'''
 
| '''Errors'''
|'''[[XQuery Errors#ZIP Functions Errors|FOZP0001]]''' is raised if an addressed file does not exist.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0002]]''' is raised if entries in the ZIP archive description are unknown, missing, or invalid.<br />'''[[XQuery Errors#ZIP Functions Errors|FOZP0003]]''' is raised if the operation fails for some other reason.<br />'''[[XQuery Errors#Serialization Errors|Serialization Errors]]''' are raised if an inlined XML fragment cannot be successfully serialized.
+
|{{Error|FOZP0001|XQuery Errors#ZIP Functions Errors}} an addressed file does not exist.<br />{{Error|FOZP0002|XQuery Errors#ZIP Functions Errors}} entries in the ZIP archive description are unknown, missing, or invalid.<br />{{Error|FOZP0003|XQuery Errors#ZIP Functions Errors}} the operation fails for some other reason.<br />{{Error|Serialization Errors|XQuery Errors#Serialization Errors}} an inlined XML fragment cannot be successfully serialized.
 
|-
 
|-
 
| '''Examples'''
 
| '''Examples'''
 
|
 
|
* The following function creates a copy <code>new.zip</code> of the existing <code>archive.zip</code> file:
+
* The following function creates a copy {{Code|new.zip}} of the existing {{Code|archive.zip}} file:
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
 
zip:update-entries(zip:entries('archive.zip'), 'new.zip')
 
zip:update-entries(zip:entries('archive.zip'), 'new.zip')
 
</pre>
 
</pre>
* The following function deletes all PNG files from <code>archive.zip</code>:
+
* The following function deletes all PNG files from {{Code|archive.zip}}:
 
<pre class="brush:xquery">
 
<pre class="brush:xquery">
 
declare namespace zip = "http://expath.org/ns/zip";
 
declare namespace zip = "http://expath.org/ns/zip";

Revision as of 16:21, 26 May 2012

This XQuery Module contains functions to handle ZIP archives. The contents of ZIP files can be extracted and listed, and new archives can be created. The module is based on the EXPath ZIP Module. It will soon be replaced by another ZIP Module.

An additional page in this Wiki demonstrates how to modify Word documents with the ZIP module.

Conventions

All functions in this module are assigned to the http://expath.org/ns/zip namespace, which is statically bound to the zip prefix.
All errors are assigned to the http://expath.org/ns/error namespace, which is statically bound to the exerr prefix.

Functions

zip:binary-entry

Signatures zip:binary-entry($uri as xs:string, $path as xs:string) as xs:base64Binary
Summary Extracts the binary file at $path within the ZIP file located at $uri and returns it as an xs:base64Binary item.
Errors FOZP0001: the specified path does not exist.
FOZP0003: the operation fails for some other reason.

zip:text-entry

Signatures zip:text-entry($uri as xs:string, $path as xs:string) as xs:string
zip:text-entry($uri as xs:string, $path as xs:string, $encoding as xs:string) as xs:string
Summary Extracts the text file at $path within the ZIP file located at $uri and returns it as an xs:string item.
An optional encoding can be specified via $encoding.
Errors FOZP0001: the specified path does not exist.
FOZP0003: the operation fails for some other reason.

zip:xml-entry

Signatures zip:xml-entry($uri as xs:string, $path as xs:string) as document-node()
Summary Extracts the XML file at $path within the ZIP file located at $uri and returns it as a document node.
Errors FODC0006: the addressed file is not well-formed.
FOZP0001: the specified path does not exist.
FOZP0003: the operation fails for some other reason.

zip:html-entry

Signatures zip:html-entry($uri as xs:string, $path as xs:string) as document-node()
Summary Extracts the HTML file at $path within the ZIP file located at $uri and returns it as a document node. The file is converted to XML first if Tagsoup is found in the classpath.
Errors FODC0006: the addressed file is not well-formed, or cannot be converted to correct XML.
FOZP0001: the specified path does not exist.
FOZP0003: the operation fails for some other reason.

zip:entries

Signatures zip:entries($uri as xs:string) as element(zip:file)
Summary Generates an ZIP XML Representation of the hierarchical structure of the ZIP file located at $uri and returns it as an element node. The file contents are not returned by this function.
Errors FOZP0001: the specified path does not exist.
FOZP0003: the operation fails for some other reason.
Examples
  • If the ZIP archive archive.zip is empty, zip:entries('archive.zip') returns {{{1}}}.

zip:zip-file

Signatures zip:zip-file($zip as element(zip:file)) as empty-sequence()
Summary Creates a new ZIP archive with the characteristics described by $zip, the ZIP XML Representation.
Errors FOZP0001: an addressed file does not exist.
FOZP0002: entries in the ZIP archive description are unknown, missing, or invalid.
FOZP0003: the operation fails for some other reason.
Serialization Errors: an inlined XML fragment cannot be successfully serialized.
Examples
  • The following function creates a file archive.zip with the file file.txt inside:
zip:zip-file(
  <file xmlns="http://expath.org/ns/zip" href="archive.zip">
    <entry src="file.txt"/>
  </file>)
  • The following function creates a file archive.zip. It contains one file readme with the content "thanks":
zip:zip-file(
  <file xmlns="http://expath.org/ns/zip" href="archive.zip">
    <entry name="readme">thanks</entry>
  </file>)

zip:update-entries

Signatures zip:update-entries($zip as element(zip:file), $output as xs:string) as empty-sequence()
Summary Updates an existing ZIP archive or creates a modifed copy, based on the characteristics described by $zip, the ZIP XML Representation. The $output argument is the URI where the modified ZIP file is copied to.
Errors FOZP0001: an addressed file does not exist.
FOZP0002: entries in the ZIP archive description are unknown, missing, or invalid.
FOZP0003: the operation fails for some other reason.
Serialization Errors: an inlined XML fragment cannot be successfully serialized.
Examples
  • The following function creates a copy new.zip of the existing archive.zip file:
zip:update-entries(zip:entries('archive.zip'), 'new.zip')
  • The following function deletes all PNG files from archive.zip:
declare namespace zip = "http://expath.org/ns/zip";
copy $doc := zip:entries('archive.zip')
modify delete node $doc//zip:entry[ends-with(lower-case(@name), '.png')]
return zip:update-entries($doc, 'archive.zip')