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