@@ -157,14 +157,16 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
157
157
final OutputStream os = stream (baos , encoding );
158
158
final StringBuilderWriter sbWriter = new StringBuilderWriter ()) {
159
159
160
+ final CRC32 chksum = new CRC32 ();
161
+
160
162
// iterate through the argument sequence
161
163
for (final SequenceIterator i = args [0 ].iterate (); i .hasNext (); ) {
162
164
final Item item = i .nextItem ();
163
165
164
166
if (item instanceof Element element ) {
165
- compressElement (os , element , useHierarchy , stripOffset , sbWriter );
167
+ compressElement (os , element , useHierarchy , stripOffset , sbWriter , chksum );
166
168
} else {
167
- compressFromUri (os , ((AnyURIValue ) item ).toURI (), useHierarchy , stripOffset , ZipMethod .DEFLATE , null , sbWriter );
169
+ compressFromUri (os , ((AnyURIValue ) item ).toURI (), useHierarchy , stripOffset , ZipMethod .DEFLATE , null , sbWriter , chksum );
168
170
}
169
171
}
170
172
@@ -181,7 +183,7 @@ public Sequence eval(final Sequence[] args, final Sequence contextSequence) thro
181
183
}
182
184
}
183
185
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
186
+ private void compressFromUri (final OutputStream os , final URI uri , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final String resourceName , final StringBuilderWriter sbWriter , final CRC32 chksum ) throws XPathException
185
187
{
186
188
try {
187
189
if ("file" .equals (uri .getScheme ())) {
@@ -194,7 +196,7 @@ private void compressFromUri(final OutputStream os, final URI uri, final boolean
194
196
195
197
// got a file
196
198
final Path file = Paths .get (uri .getPath ());
197
- compressFile (os , file , useHierarchy , stripOffset , method , resourceName );
199
+ compressFile (os , file , useHierarchy , stripOffset , method , resourceName , chksum );
198
200
199
201
} else {
200
202
@@ -203,7 +205,7 @@ private void compressFromUri(final OutputStream os, final URI uri, final boolean
203
205
// try for a collection
204
206
try (final Collection collection = context .getBroker ().openCollection (xmldburi , LockMode .READ_LOCK )) {
205
207
if (collection != null ) {
206
- compressCollection (os , collection , useHierarchy , stripOffset , method , sbWriter );
208
+ compressCollection (os , collection , useHierarchy , stripOffset , method , sbWriter , chksum );
207
209
return ;
208
210
}
209
211
} catch (final PermissionDeniedException | LockException | SAXException | IOException pde ) {
@@ -226,7 +228,7 @@ private void compressFromUri(final OutputStream os, final URI uri, final boolean
226
228
throw new XPathException (this , "Invalid URI: " + uri .toString ());
227
229
}
228
230
229
- compressResource (os , doc .getDocument (), useHierarchy , stripOffset , method , resourceName , sbWriter );
231
+ compressResource (os , doc .getDocument (), useHierarchy , stripOffset , method , resourceName , sbWriter , chksum );
230
232
return ;
231
233
}
232
234
} catch (final PermissionDeniedException | LockException | SAXException | IOException pde ) {
@@ -249,8 +251,9 @@ private void compressFromUri(final OutputStream os, final URI uri, final boolean
249
251
* @param stripOffset a string that should be stripped from the start of the entry name.
250
252
* @param method the Zip method.
251
253
* @param name the name of the entry.
254
+ * @param chksum an object that is used to calculate the checksum.
252
255
*/
253
- private void compressFile (final OutputStream os , final Path file , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final String name ) throws IOException {
256
+ private void compressFile (final OutputStream os , final Path file , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final String name , final CRC32 chksum ) throws IOException {
254
257
255
258
if (!Files .isDirectory (file )) {
256
259
@@ -269,6 +272,7 @@ private void compressFile(final OutputStream os, final Path file, final boolean
269
272
// close the entry
270
273
if (entry instanceof ZipEntry && method == ZipMethod .STORE ) {
271
274
((ZipEntry ) entry ).setMethod (ZipOutputStream .STORED );
275
+ chksum .reset ();
272
276
chksum .update (value );
273
277
((ZipEntry ) entry ).setCrc (chksum .getValue ());
274
278
((ZipEntry ) entry ).setSize (value .length );
@@ -281,7 +285,7 @@ private void compressFile(final OutputStream os, final Path file, final boolean
281
285
} else {
282
286
283
287
for (final Path child : FileUtils .list (file )) {
284
- compressFile (os , file .resolve (child ), useHierarchy , stripOffset , method , null );
288
+ compressFile (os , file .resolve (child ), useHierarchy , stripOffset , method , null , chksum );
285
289
}
286
290
287
291
}
@@ -296,9 +300,10 @@ private void compressFile(final OutputStream os, final Path file, final boolean
296
300
* @param useHierarchy Whether to use a folder hierarchy in the archive file that reflects the collection hierarchy.
297
301
* @param stripOffset a string that should be stripped from the start of the entry name.
298
302
* @param sbWriter a StringBuilderWriter to reuse
303
+ * @param chksum an object that is used to calculate the checksum.
299
304
*/
300
305
private void compressElement (final OutputStream os , final Element element , final boolean useHierarchy ,
301
- final String stripOffset , final StringBuilderWriter sbWriter ) throws XPathException {
306
+ final String stripOffset , final StringBuilderWriter sbWriter , final CRC32 chksum ) throws XPathException {
302
307
303
308
final String ns = element .getNamespaceURI ();
304
309
if (!(element .getNodeName ().equals ("entry" ) || (ns != null && !ns .isEmpty ()))) {
@@ -326,7 +331,7 @@ private void compressElement(final OutputStream os, final Element element, final
326
331
if (isNullOrEmpty (uri )) {
327
332
throw new XPathException (this , "Entry with type uri must contain a URI." );
328
333
}
329
- compressFromUri (os , URI .create (uri ), useHierarchy , stripOffset , method , name , sbWriter );
334
+ compressFromUri (os , URI .create (uri ), useHierarchy , stripOffset , method , name , sbWriter , chksum );
330
335
return ;
331
336
}
332
337
@@ -346,7 +351,6 @@ private void compressElement(final OutputStream os, final Element element, final
346
351
347
352
if (!"collection" .equals (type )) {
348
353
final byte [] value ;
349
- final CRC32 chksum = new CRC32 ();
350
354
final Node content = element .getFirstChild ();
351
355
352
356
if (content == null ) {
@@ -379,6 +383,7 @@ private void compressElement(final OutputStream os, final Element element, final
379
383
380
384
if (entry instanceof ZipEntry && method == ZipMethod .STORE ) {
381
385
((ZipEntry ) entry ).setMethod (ZipOutputStream .STORED );
386
+ chksum .reset ();
382
387
chksum .update (value );
383
388
((ZipEntry ) entry ).setCrc (chksum .getValue ());
384
389
((ZipEntry ) entry ).setSize (value .length );
@@ -422,8 +427,9 @@ private void getDynamicSerializerOptions(final Serializer serializer) throws SAX
422
427
* @param method the Zip method.
423
428
* @param name the name of the entry.
424
429
* @param sbWriter a StringBuilderWriter to reuse
430
+ * @param chksum an object that is used to calculate the checksum.
425
431
*/
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 {
432
+ private void compressResource (final OutputStream os , final DocumentImpl doc , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final String name , final StringBuilderWriter sbWriter , final CRC32 chksum ) throws IOException , SAXException {
427
433
// create an entry in the Tar for the document
428
434
final Object entry ;
429
435
if (name != null ) {
@@ -463,9 +469,9 @@ private void compressResource(final OutputStream os, final DocumentImpl doc, fin
463
469
}
464
470
465
471
// close the entry
466
- final CRC32 chksum = new CRC32 ();
467
472
if (entry instanceof ZipEntry && method == ZipMethod .STORE ) {
468
473
((ZipEntry ) entry ).setMethod (ZipOutputStream .STORED );
474
+ chksum .reset ();
469
475
chksum .update (value );
470
476
((ZipEntry ) entry ).setCrc (chksum .getValue ());
471
477
((ZipEntry ) entry ).setSize (value .length );
@@ -485,8 +491,9 @@ private void compressResource(final OutputStream os, final DocumentImpl doc, fin
485
491
* @param stripOffset a string that should be stripped from the start of the entry name.
486
492
* @param method the Zip method.
487
493
* @param sbWriter a StringBuilderWriter to reuse
494
+ * @param chksum an object that is used to calculate the checksum.
488
495
*/
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 {
496
+ private void compressCollection (final OutputStream os , final Collection col , final boolean useHierarchy , final String stripOffset , final ZipMethod method , final StringBuilderWriter sbWriter , final CRC32 chksum ) throws IOException , SAXException , LockException , PermissionDeniedException {
490
497
// iterate over child documents
491
498
final DBBroker broker = context .getBroker ();
492
499
final LockManager lockManager = broker .getBrokerPool ().getLockManager ();
@@ -495,7 +502,7 @@ private void compressCollection(final OutputStream os, final Collection col, fin
495
502
for (final Iterator <DocumentImpl > itChildDocs = childDocs .getDocumentIterator (); itChildDocs .hasNext ();) {
496
503
final DocumentImpl childDoc = itChildDocs .next ();
497
504
try (final ManagedDocumentLock updateLock = lockManager .acquireDocumentReadLock (childDoc .getURI ())) {
498
- compressResource (os , childDoc , useHierarchy , stripOffset , method , null , sbWriter );
505
+ compressResource (os , childDoc , useHierarchy , stripOffset , method , null , sbWriter , chksum );
499
506
}
500
507
}
501
508
// iterate over child collections
@@ -504,7 +511,7 @@ private void compressCollection(final OutputStream os, final Collection col, fin
504
511
final XmldbURI childColURI = itChildCols .next ();
505
512
final Collection childCol = broker .getCollection (col .getURI ().append (childColURI ));
506
513
// recurse
507
- compressCollection (os , childCol , useHierarchy , stripOffset , method , sbWriter );
514
+ compressCollection (os , childCol , useHierarchy , stripOffset , method , sbWriter , chksum );
508
515
}
509
516
}
510
517
0 commit comments