ZIP Module: Examples

From Spex: Specifications for XQuery Modules
Jump to: navigation, search

This page lists some code examples for the Archive Module.

  • Return the archive structure of HelloWorld.docx using the following query:
archive:entries(file:read-binary("HelloWorld.docx"))
  • Extract all files of an archive to the current directory (fast version):
let $archive  := file:read-binary('archive.zip')
let $entries  := archive:entries($archive)
let $contents := archive:extract-binary($archive)
for $entry at $p in $entries
return (
  file:create-dir(file:dir-name($entry)),
  file:write-binary($entry, $contents[$p])
)
  • Return the node representation of all archived XML documents:
let $archive := file:read-binary("documents.zip")
for $entry in archive:entries($archive)
where matches($entry, '\.xml$', 'i')
return fn:parse-xml(archive:extract-texts($archive, $entry))
  • Extract and zip all files of an archive in one go:
let $old := file:read-binary('old.zip')
let $entries  := archive:entries($old)
let $contents := archive:extract-binary($old)
let $new := archive:create($entries, $contents)
return file:write-binary('new.zip', $new) 
  • Locate the root elements containing text HELLO WORLD! in word/document.xml:
let $docx := file:read-binary("HelloWorld.docx")
let $text := archive:extract-texts($docx, 'word/document.xml')
let $xml  := fn:parse-xml($text)
return $xml//*[not(*)][contains(., "HELLO WORLD!")]
  • Modify the contents of Word document:
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-texts($archive, $doc))
  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)
  • Modify fonts in a Word document:
declare variable $input  := "Arial.docx";
declare variable $output := "Times.docx";
declare variable $doc    := "word/document.xml";

let $archive := file:read-binary($input)
let $entry   :=
  copy $c := fn:parse-xml(archive:extract-texts($archive, $doc))
  modify for $i in $c//@*[data() = "Arial"]
         return replace value of node $i with "Times New Roman"
  return fn:serialize($c)
let $updated := archive:update($archive, <entry>{ $doc }</entry>, $entry)
return file:write-binary($output, $updated)
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox