Skip to content

Instantly share code, notes, and snippets.

@joewiz
Created January 17, 2020 18:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joewiz/7d591967fa7841a4c283cd6d741b9c22 to your computer and use it in GitHub Desktop.
Save joewiz/7d591967fa7841a4c283cd6d741b9c22 to your computer and use it in GitHub Desktop.
Construct a zip file and stream it to a browser, with XQuery & eXist
xquery version "3.1";
let $node := <root><x/></root>
let $entry := <entry name="test.xml" type="xml">{$node}</entry>
let $zip := compression:zip($entry, true())
let $name := "test.zip"
return
response:stream-binary($zip, "media-type=application/zip", "test.zip")
xquery version "3.1";
let $zip-filename := "test.zip"
let $contents :=
array {
map {
"content": <root><x/></root>,
"filename": "test.xml",
"serialization": map { "indent": true() }
}
}
let $entries :=
for $item in $contents?*
return
element entry {
attribute name { $item?filename },
attribute type {
if (map:contains($item, "type")) then
$item?type
else (: if ($item instance of node()) then :)
"xml"
(: TODO add support for other types: collection|uri|binary|xml|text :)
},
attribute method {
if (map:contains($item, "method")) then
$item?store
else
"store"
},
if (map:contains($item, "serialization")) then
serialize($item?content, $item?serialization)
else
$item?content
}
let $binary-data := compression:zip($entries, true())
return
response:stream-binary($binary-data, "media-type=application/zip", $zip-filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment