Creating ZIP files

February 11, 2009 Data & AI

The latest version of the DataDirect Data Integration Suiteincludes the ability to create and update ZIP files, straight from within XQuery.

 

Why would you want to create ZIP files with XQuery? Before answering the question, let's first take a "how to look" using DataDirect XQuery, part of the Data Integration Suite.

 

We have talked about the ddtek:serialize-to-url() function before in this blog. My colleague Minollo did for example in How to split a large XML document in many smaller ones? or check out my series XQuery generating multiple XML documents. So we know how to use XQuery and create for example XML documents or EDI messages. And now we can also create ZIP files.

 

In the next example we create a ZIP file and add three XML documents to it.

 

[cc lang="xquery"]ddtek:serialize-to-url(1, "zip:file:///C:/example.zip!/example1.xml?auto-create=yes", ""), ddtek:serialize-to-url(2, "zip:file:///C:/example.zip!/dir1/example2.xml", ""), ddtek:serialize-to-url(3, "zip:file:///C:/example.zip!/dir2/example3.xml", "")[/cc]

 

Note the first invocation of ddtek:serialize-to-url() includes the auto-create=yes option, making sure a new ZIP file is created. The following two invocations are adding more documents to the ZIP. The zip URL consists basically out of two parts. First the location of the ZIP file is specified. And second, after the exclamation mark, the location where the file needs to be added in the ZIP file.

 

Executing the above XQuery, results in a ZIP file as follows.

 

Let's now have a look at a more concrete example. In Create (EDI) messages out of your database we explained how easy it is to create EDI messages based on your data in a relational database. But assume now each of these EDI messages should be compressed in a ZIP file. Not much is changed compared to what we know already.

 

The next example creates for each supplier with orders for today, a ZIP file named after the supplier. Each of the ZIP files contain a single EANCOM ORDERS message. Note that only the "let $url" clause is specific to the creation of the ZIP files.

 

[cc lang="xquery"]for $supplier in collection("SUPPLIERS")/SUPPLIERS let $orders := collection("ORDERS")/ORDERS [SUP_ID = $supplier/ID and ORDER_DATE = current-date()] where $orders return let $url := concat("zip:file:///C:/output/", $supplier/NAME, ".zip!/", $supplier/NAME, "edi?auto-create=yes") let $edi := (: this message is incomplete :)

UNOA 4

MYBOOKSHOP 666

{ $supplier/NAME/text() } { $supplier/BUSINESS_ID/text() }

6002

{ for $order at $lineitem in $orders return

{$lineitem} 1

{ $order/ISBN/text() } IB

1 { $order/QUANTITY/text() }

}

return ddtek:serialize-to-url($edi, $url, "method=EDI") [/cc]

 

Back to our question, "Why would you want to create ZIP files in XQuery?".Yes there is more than the type of use cases shown above. Remember a rather old post XQuery your office documents. Both Office Open XML and OpenDocument Format are based on the ZIP standard. The ability to create ZIP files with DataDirect XQuery, opens the world of creating office documents. In my next post I'll show how to create Microsoft Excel spreadsheets in a few lines of XQuery. Followed with demonstrating the equivalent for OpenOffice.

Marc Van Cappellen