Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.