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