~zorba-coders/zorba/zorba_zip_module

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
xquery version "1.0";

(:
 : Copyright 2012 The FLWOR Foundation.
 :
 : Licensed under the Apache License, Version 2.0 (the "License");
 : you may not use this file except in compliance with the License.
 : You may obtain a copy of the License at
 :
 : http://www.apache.org/licenses/LICENSE-2.0
 :
 : Unless required by applicable law or agreed to in writing, software
 : distributed under the License is distributed on an "AS IS" BASIS,
 : WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 : See the License for the specific language governing permissions and
 : limitations under the License.
 :)

(:~
 : <p>This module provides functionality to work with (possibly compressed)
 : archives. For example, it provides functions to retrieve the names or
 : extract the values of several entries in a ZIP archive. Moreover,
 : there exist functions that allow to create or update archives.</p>
 :
 : <p>The following archive formats and compression algorithms are supported:
 : <ul>
 :   <li>ZIP (with compression DEFLATE or STORE)</li>
 :   <li>TAR (with compression GZIP or BZIP2)</li>
 : </ul>
 : </p>
 : 
 : @author Luis Rodgriguez, Juan Zacarias, and Matthias Brantner
 :
 : @library <a href="http://code.google.com/p/libarchive/">libarchive</a>
 :)
module namespace a = "http://www.expath.org/ns/archive";
 
import schema namespace options = "http://www.expath.org/ns/archive";

declare namespace ver = "http://www.zorba-xquery.com/options/versioning";
declare option ver:module-version "1.0";
  
(:~
 : Creates a new ZIP archive out of the given entries and contents.
 :
 : <p>All entries are compressed with the DEFLATE compression algorithm.</p>
 : 
 : <p>The parameters $entries and $contents have the same meaning as for
 : the function a:create with three arguments.</p>
 :
 : @param $entries the meta data for the entries in the archive. Each entry
 :   can be of type xs:string or an element with name a:entry.
 : @param $contents the content for the archive. Each item in the sequence
 :   can be of type xs:string or xs:base64Binary.
 :
 : @return the generated archive as xs:base64Binary
 :
 : @error a:ARCH0001 if the number of entry elements differs from the number
 :        of items in the $contents sequence: count($entries) ne count($contents)
 : @error a:ARCH0003 if a value for an entry element is invalid
 : @error a:ARCH0004 if a given encoding is invalid or not supported
 : @error err:FORG0006 if an item in the contents sequence is not of type xs:string
 :   or xs:base64Binary
 :)
declare function a:create(
  $entries as item()*,
  $contents as item()*)
    as xs:base64Binary external;
 
(:~
 : Creates a new archive out of the given entries and contents.
 :
 : <p>The $entries arguments provides meta data for each entry in the archive.
 : For example, the name of the entry (mandatory) or the last-modified date
 : (optional). An entry can either be of type xs:string to describe the entry
 : name or of type xs:base64Binary to provide additional meta data.</p>
 :
 : <p>The $contents sequence provides the data (xs:string or xs:base64Binary) for
 : the entries that should be included in the archive. Its length needs to
 : match the length of the $entries sequence (a:ARCH0001). All items of type
 : xs:base64Binary are decoded before being added to the archive.</p>
 :
 : <p>For each entry, the name, last-modified date and time, and compression
 : can be specified. In addition, an encoding can be specified which is used to
 : store entries of type xs:string. If no last-modified attribute is given, the
 : default is the current date and time. The compression is useful if various
 : entries in a ZIP archive are compressed using different compression
 : algorithms (i.e. store or deflate).</p>
 :
 : <p>For example, the following sequence may be used to describe an archive
 : containing three elements:
 : <pre>
 : &lt;a:entry last-modified="{fn:current-dateTime()}">myfile.txt&lt;/a:entry>
 : &lt;a:entry encoding="ISO-8859-1" compression="store">dir/myfile.xml&lt;/a:entry>
 : </pre>
 : </p>
 :
 : <p>The $options argument may be used to describe general options for the
 : archive.  For example, the following option element can be used to create a ZIP
 : archive in which all entries are compressed with the DEFLATE compression
 : algorithm:
 : <pre>
 : &lt;archive:options>
 :   &lt;archive:format>ZIP&lt;/archive:format>
 :   &lt;archive:compression>DEFLATE&lt;/archive:compression>
 : &lt;/archive:options>
 : </pre>
 : </p>
 :
 : <p>The result of the function is the generated archive as a item of type
 : xs:base64Binary.</p>
 :
 :
 : @param $entries the meta data for the entries in the archive. Each entry
 :   can be of type xs:string or an element with name a:entry.
 : @param $contents the content for the archive. Each item in the sequence
 :   can be of type xs:string or xs:base64Binary.
 : @param $options the options used to generate the archive.
 :
 : @return the generated archive as xs:base64Binary
 :
 : @error a:ARCH0001 if the number of entry elements differs from the number
 :        of items in the $contents sequence: count($entries) ne count($contents)
 : @error a:ARCH0002 if the options argument contains invalid values
 : @error a:ARCH0003 if a value for an entry element is invalid
 : @error a:ARCH0004 if a given encoding is invalid or not supported
 : @error err:FORG0006 if an item in the contents sequence is not of type xs:string
 :   or xs:base64Binary
 :)
declare function a:create(
  $entries as item()*,
  $contents as item()*,
  $options as element(a:options))
    as xs:base64Binary external; 
  
(:~
 : Returns the header information of all entries in the given archive.
 :
 : <p>Such information includes the name of the entry, the uncompressed size,
 : as well as the last-modified timestamp. Note that not all values are
 : available in every archive.</p>
 :
 : @param $archive the archive to list the entries from as xs:base64Binary
 :
 : @return a sequence of strings, one for each entry in the archive
 :
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 :)
declare function a:entries($archive as xs:base64Binary)
    as element(a:entry)* external;
  
(:~
 : Extracts the contents of all entries in the given archive as text
 : using UTF-8 as default encoding.
 :
 : @param $archive the archive to extract the entries from as xs:base64Binary
 :
 : @return one string for the contents of each entry in the archive
 :
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 : @error err:FOCH0001 if any of the entries contains invalid utf-8 characters
 :)
declare function a:extract-text($archive as xs:base64Binary)
    as xs:string* external;
  
(:~
 : Extracts the contets of the entries identified by a given sequence of
 : names as text.
 : The default encoding used to read the string is UTF-8.
 :
 : @param $archive the archive to extract the entries from as xs:base64Binary
 : @param $entry-names a sequence of names for entries which should be extracted
 :
 : @return a sequence of strings for the given sequence of names or the
 :   empty sequence if no entries match the given names.
 :
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 : @error err:FOCH0001 if any of the entries requested contains invalid utf-8 characters
 :)
declare function a:extract-text($archive as xs:base64Binary, $entry-names as xs:string*)
    as xs:string* external;
  
(:~
 : Extracts the contets of the entries identified by a given sequence of
 : names as text. Each entry is treated with the given encoding.
 :
 : @param $archive the archive to extract the entries from as xs:base64Binary
 : @param $entry-names a sequence of entry names that should be extracted
 : @param $encoding a encoding for transcoding each of the extracted entries
 :
 : @return a sequence of strings for the given sequence of names or the
 :   empty sequence if no entries match the given names.
 :
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 : @error a:ARCH0004 if the given $encoding is invalid or not supported
 : @error err:FOCH0001 if a transcoding error happens
 :)
declare function a:extract-text(
  $archive as xs:base64Binary,
  $entry-names as xs:string*,
  $encoding as xs:string)
    as xs:string* external;
  
(:~
 : Returns the entries identified by the given paths from the archive
 : as base64Binary.
 :
 : @param $archive the archive to extract the entries from as xs:base64Binary
 :
 : @return one xs:base64Binary item for the contents of each entry in the archive
 :
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 :)
declare function a:extract-binary($archive as xs:base64Binary)
    as xs:base64Binary* external;
  
(:~
 : Returns the entries identified by the given paths from the archive
 : as base64Binary.
 :
 : @param $entry-names a sequence of names for entries which should be extracted
 :
 : @return a sequence of xs:base64Binary itmes for the given sequence of names
 :  or the empty sequence if no entries match the given names.
 :
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 :)
declare function a:extract-binary($archive as xs:base64Binary, $entry-names as xs:string*)
    as xs:base64Binary* external;
  
(:~
 : Adds and replaces entries in an archive according to
 : the given spec. The contents can be string and base64Binary items.
 :
 : <p>The parameters $entries and $contents have the same meaning as for
 : the function a:create with three arguments.</p>
 :  
 : @param $archive the archive to add or replace content
 : @param $entries the meta data for the entries in the archive. Each entry
 :   can be of type xs:string or an element with name a:entry.
 : @param $contents the content for the archive. Each item in the sequence
 :   can be of type xs:string or xs:base64Binary.
 :  
 : @return the updated xs:base64Binary
 :
 : @error a:ARCH0001 if the number of entry elements differs from the number
 :        of items in the $contents sequence: count($entries) ne count($contents) 
 : @error a:ARCH0003 if a value for an entry element is invalid
 : @error a:ARCH0004 if a given encoding is invalid or not supported
 : @error err:FORG0006 if an item in the contents sequence is not of type xs:string
 :   or xs:base64Binary
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 :
 :)
declare function a:update($archive as xs:base64Binary, $entries as item()*, $contents as item()*)
    as xs:base64Binary external;
  
(:~
 : Deletes entries from an archive.
 :
 : @param $archive the archive to extract the entries from as xs:base64Binary
 : @param $entry-names a sequence of names for entries which should be deleted
 : 
 : @return the updated base64Binary
 :
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 :)
declare function a:delete($archive as xs:base64Binary, $entry-names as xs:string*)
    as xs:base64Binary external;

(:~
 : Returns the algorithm and format options of the given archive.
 : For example, for a ZIP archive, the following options element
 : would be returned:
 : <pre>
 : &lt;archive:options>
 :   &lt;archive:format>ZIP&lt;/archive:format>
 :   &lt;archive:compressionDEFLATE&lt;/archive:compression>
 : &lt;/archive:options>
 : </pre>
 :
 : @param $archive the archive as xs:base64Binary
 :
 : @return the algorithm and format options
 :
 : @error a:ARCH9999 if $archive is not an archive or corrupted
 :)
declare function a:options($archive as xs:base64Binary)
  as element(a:options) external;