@@ -104,6 +104,11 @@ public abstract class AbstractCompressFunction extends BasicFunction
104
104
protected final static SequenceType STRIP_PREFIX_PARAM = new FunctionParameterSequenceType ("strip-prefix" , Type .STRING , Cardinality .EXACTLY_ONE , "This prefix is stripped from the Entrys name" );
105
105
protected final static SequenceType ENCODING_PARAM = new FunctionParameterSequenceType ("encoding" , Type .STRING , Cardinality .EXACTLY_ONE , "This encoding to be used for filenames inside the compressed file" );
106
106
107
+ private enum ZipMethod {
108
+ DEFLATE ,
109
+ STORE
110
+ }
111
+
107
112
108
113
public AbstractCompressFunction (final XQueryContext context , final FunctionSignature signature ) {
109
114
super (context , signature );
@@ -159,7 +164,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
159
164
if (item instanceof Element element ) {
160
165
compressElement (os , element , useHierarchy , stripOffset , sbWriter );
161
166
} else {
162
- compressFromUri (os , ((AnyURIValue ) item ).toURI (), useHierarchy , stripOffset , "" , null , sbWriter );
167
+ compressFromUri (os , ((AnyURIValue ) item ).toURI (), useHierarchy , stripOffset , ZipMethod . DEFLATE , null , sbWriter );
163
168
}
164
169
}
165
170
@@ -176,7 +181,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
176
181
}
177
182
}
178
183
179
- private void compressFromUri (final OutputStream os , final URI uri , final boolean useHierarchy , final String stripOffset , final String method , final String resourceName , final StringBuilderWriter sbWriter ) throws XPathException
184
+ private void compressFromUri (final OutputStream os , final URI uri , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final String resourceName , final StringBuilderWriter sbWriter ) throws XPathException
180
185
{
181
186
try {
182
187
if ("file" .equals (uri .getScheme ())) {
@@ -198,7 +203,7 @@ private void compressFromUri(final OutputStream os, final URI uri, final boolean
198
203
// try for a collection
199
204
try (final Collection collection = context .getBroker ().openCollection (xmldburi , LockMode .READ_LOCK )) {
200
205
if (collection != null ) {
201
- compressCollection (os , collection , useHierarchy , stripOffset , sbWriter );
206
+ compressCollection (os , collection , useHierarchy , stripOffset , method , sbWriter );
202
207
return ;
203
208
}
204
209
} catch (final PermissionDeniedException | LockException | SAXException | IOException pde ) {
@@ -245,7 +250,7 @@ private void compressFromUri(final OutputStream os, final URI uri, final boolean
245
250
* @param method the Zip method.
246
251
* @param name the name of the entry.
247
252
*/
248
- private void compressFile (final OutputStream os , final Path file , final boolean useHierarchy , final String stripOffset , final String method , final String name ) throws IOException {
253
+ private void compressFile (final OutputStream os , final Path file , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final String name ) throws IOException {
249
254
250
255
if (!Files .isDirectory (file )) {
251
256
@@ -262,9 +267,7 @@ private void compressFile(final OutputStream os, final Path file, final boolean
262
267
final byte [] value = Files .readAllBytes (file );
263
268
264
269
// close the entry
265
- final CRC32 chksum = new CRC32 ();
266
- if (entry instanceof ZipEntry &&
267
- "store" .equals (method )) {
270
+ if (entry instanceof ZipEntry && method == ZipMethod .STORE ) {
268
271
((ZipEntry ) entry ).setMethod (ZipOutputStream .STORED );
269
272
chksum .update (value );
270
273
((ZipEntry ) entry ).setCrc (chksum .getValue ());
@@ -311,13 +314,19 @@ private void compressElement(final OutputStream os, final Element element, final
311
314
// throw new XPathException(this, "Entry must have name attribute.");
312
315
313
316
final String type = element .getAttribute ("type" );
317
+ ZipMethod method ;
318
+ try {
319
+ method = ZipMethod .valueOf (element .getAttribute ("method" ).toUpperCase ());
320
+ } catch (final IllegalArgumentException e ) {
321
+ method = ZipMethod .DEFLATE ;
322
+ }
314
323
315
324
if ("uri" .equals (type )) {
316
325
@ Nullable final String uri = element .getFirstChild ().getNodeValue ();
317
326
if (isNullOrEmpty (uri )) {
318
327
throw new XPathException (this , "Entry with type uri must contain a URI." );
319
328
}
320
- compressFromUri (os , URI .create (uri ), useHierarchy , stripOffset , element . getAttribute ( " method" ) , name , sbWriter );
329
+ compressFromUri (os , URI .create (uri ), useHierarchy , stripOffset , method , name , sbWriter );
321
330
return ;
322
331
}
323
332
@@ -368,8 +377,7 @@ private void compressElement(final OutputStream os, final Element element, final
368
377
}
369
378
}
370
379
371
- if (entry instanceof ZipEntry &&
372
- "store" .equals (element .getAttribute ("method" ))) {
380
+ if (entry instanceof ZipEntry && method == ZipMethod .STORE ) {
373
381
((ZipEntry ) entry ).setMethod (ZipOutputStream .STORED );
374
382
chksum .update (value );
375
383
((ZipEntry ) entry ).setCrc (chksum .getValue ());
@@ -415,7 +423,7 @@ private void getDynamicSerializerOptions(final Serializer serializer) throws SAX
415
423
* @param name the name of the entry.
416
424
* @param sbWriter a StringBuilderWriter to reuse
417
425
*/
418
- private void compressResource (final OutputStream os , final DocumentImpl doc , final boolean useHierarchy , final String stripOffset , final String method , final String name , final StringBuilderWriter sbWriter ) throws IOException , SAXException {
426
+ private void compressResource (final OutputStream os , final DocumentImpl doc , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final String name , final StringBuilderWriter sbWriter ) throws IOException , SAXException {
419
427
// create an entry in the Tar for the document
420
428
final Object entry ;
421
429
if (name != null ) {
@@ -456,8 +464,7 @@ private void compressResource(final OutputStream os, final DocumentImpl doc, fin
456
464
457
465
// close the entry
458
466
final CRC32 chksum = new CRC32 ();
459
- if (entry instanceof ZipEntry &&
460
- "store" .equals (method )) {
467
+ if (entry instanceof ZipEntry && method == ZipMethod .STORE ) {
461
468
((ZipEntry ) entry ).setMethod (ZipOutputStream .STORED );
462
469
chksum .update (value );
463
470
((ZipEntry ) entry ).setCrc (chksum .getValue ());
@@ -476,18 +483,19 @@ private void compressResource(final OutputStream os, final DocumentImpl doc, fin
476
483
* @param col The Collection to add to the archive.
477
484
* @param useHierarchy Whether to use a folder hierarchy in the archive file that reflects the collection hierarchy.
478
485
* @param stripOffset a string that should be stripped from the start of the entry name.
486
+ * @param method the Zip method.
479
487
* @param sbWriter a StringBuilderWriter to reuse
480
488
*/
481
- private void compressCollection (final OutputStream os , final Collection col , final boolean useHierarchy , final String stripOffset , final StringBuilderWriter sbWriter ) throws IOException , SAXException , LockException , PermissionDeniedException {
489
+ private void compressCollection (final OutputStream os , final Collection col , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final StringBuilderWriter sbWriter ) throws IOException , SAXException , LockException , PermissionDeniedException {
482
490
// iterate over child documents
483
491
final DBBroker broker = context .getBroker ();
484
492
final LockManager lockManager = broker .getBrokerPool ().getLockManager ();
485
493
final MutableDocumentSet childDocs = new DefaultDocumentSet ();
486
494
col .getDocuments (broker , childDocs );
487
495
for (final Iterator <DocumentImpl > itChildDocs = childDocs .getDocumentIterator (); itChildDocs .hasNext ();) {
488
- final DocumentImpl childDoc = itChildDocs .next ();
496
+ final DocumentImpl childDoc = itChildDocs .next ();
489
497
try (final ManagedDocumentLock updateLock = lockManager .acquireDocumentReadLock (childDoc .getURI ())) {
490
- compressResource (os , childDoc , useHierarchy , stripOffset , "" , null , sbWriter );
498
+ compressResource (os , childDoc , useHierarchy , stripOffset , method , null , sbWriter );
491
499
}
492
500
}
493
501
// iterate over child collections
@@ -496,7 +504,7 @@ private void compressCollection(final OutputStream os, final Collection col, fin
496
504
final XmldbURI childColURI = itChildCols .next ();
497
505
final Collection childCol = broker .getCollection (col .getURI ().append (childColURI ));
498
506
// recurse
499
- compressCollection (os , childCol , useHierarchy , stripOffset , sbWriter );
507
+ compressCollection (os , childCol , useHierarchy , stripOffset , method , sbWriter );
500
508
}
501
509
}
502
510
0 commit comments