42
42
43
43
import org .apache .commons .compress .archivers .ArchiveEntry ;
44
44
import org .apache .commons .compress .archivers .ArchiveInputStream ;
45
+ import org .apache .commons .compress .archivers .tar .TarArchiveEntry ;
45
46
import org .apache .commons .compress .archivers .tar .TarArchiveInputStream ;
46
47
import org .apache .commons .compress .archivers .zip .ZipArchiveInputStream ;
47
48
import org .apache .commons .compress .compressors .gzip .GzipCompressorInputStream ;
57
58
import org .scijava .plugin .Menu ;
58
59
import org .scijava .plugin .Parameter ;
59
60
import org .scijava .plugin .Plugin ;
61
+ import org .scijava .task .Task ;
62
+ import org .scijava .task .TaskService ;
60
63
import org .scijava .ui .DialogPrompt ;
61
64
import org .scijava .ui .UIService ;
62
65
import org .scijava .util .AppUtils ;
@@ -87,6 +90,9 @@ public class ImageJUpdater implements UpdaterUI {
87
90
@ Parameter
88
91
private DownloadService downloadService ;
89
92
93
+ @ Parameter
94
+ private TaskService taskService ;
95
+
90
96
@ Parameter
91
97
private LocationService locationService ;
92
98
@@ -323,32 +329,56 @@ private boolean updateJava(final Map<String, String> jdkVersions,
323
329
324
330
String javaLoc = jdkDlLoc .getAbsolutePath ();
325
331
int extensionLength = 0 ;
332
+ int entryCount = 0 ;
326
333
327
334
// Extract the JDK
328
335
if (jdkDlLoc .toString ().endsWith ("tar.gz" )) {
336
+ extensionLength = 7 ;
337
+ // sadly this is the only way to determine how long extraction
329
338
try (FileInputStream fis = new FileInputStream (jdkDlLoc );
330
339
GzipCompressorInputStream gzIn = new GzipCompressorInputStream (fis );
331
340
TarArchiveInputStream tarIn = new TarArchiveInputStream (gzIn ))
332
341
{
333
- doExtraction (jdkDir , tarIn );
334
- extensionLength = 7 ;
342
+ entryCount = countArchiveEntries (tarIn );
335
343
}
336
344
catch (IOException e ) {
337
345
log .error (e );
338
346
return false ;
339
347
}
348
+ // Do the actual extraction
349
+ try (FileInputStream fis = new FileInputStream (jdkDlLoc );
350
+ GzipCompressorInputStream gzIn = new GzipCompressorInputStream (fis );
351
+ TarArchiveInputStream tarIn = new TarArchiveInputStream (gzIn ))
352
+ {
353
+ doExtraction (jdkDir , tarIn , entryCount );
354
+ }
355
+ catch (ExecutionException | InterruptedException | IOException e ) {
356
+ log .error (e );
357
+ return false ;
358
+ }
340
359
}
341
360
else if (jdkDlLoc .toString ().endsWith ("zip" )) {
361
+ extensionLength = 4 ;
362
+ // sadly this is the only way to determine how long extraction will take
342
363
try (FileInputStream fis = new FileInputStream (jdkDlLoc );
343
364
ZipArchiveInputStream zis = new ZipArchiveInputStream (fis ))
344
365
{
345
- doExtraction (jdkDir , zis );
346
- extensionLength = 4 ;
366
+ entryCount = countArchiveEntries (zis );
347
367
}
348
368
catch (IOException e ) {
349
369
log .error (e );
350
370
return false ;
351
371
}
372
+ // Do the actual extraction
373
+ try (FileInputStream fis = new FileInputStream (jdkDlLoc );
374
+ ZipArchiveInputStream zis = new ZipArchiveInputStream (fis ))
375
+ {
376
+ doExtraction (jdkDir , zis , entryCount );
377
+ }
378
+ catch (ExecutionException | InterruptedException | IOException e ) {
379
+ log .error (e );
380
+ return false ;
381
+ }
352
382
}
353
383
354
384
// Notify user of success
@@ -373,27 +403,58 @@ else if (jdkDlLoc.toString().endsWith("zip")) {
373
403
}
374
404
375
405
/**
376
- * Helper method to extract an archive
406
+ * Helper method to count the entries in an archive
377
407
*/
378
- private void doExtraction (final File jdkDir , final ArchiveInputStream tarIn )
408
+ private int countArchiveEntries (final ArchiveInputStream ais )
379
409
throws IOException
380
410
{
411
+ int entryCount = 0 ;
381
412
ArchiveEntry entry ;
382
- while ((entry = tarIn .getNextEntry ()) != null ) {
383
- if (entry .isDirectory ()) {
384
- new File (jdkDir , entry .getName ()).mkdirs ();
385
- }
386
- else {
387
- byte [] buffer = new byte [1024 ];
388
- File outputFile = new File (jdkDir , entry .getName ());
389
- OutputStream fos = new FileOutputStream (outputFile );
390
- int len ;
391
- while ((len = tarIn .read (buffer )) != -1 ) {
392
- fos .write (buffer , 0 , len );
413
+ while ((entry = ais .getNextEntry ()) != null ) {
414
+ entryCount ++;
415
+ }
416
+ return entryCount ;
417
+ }
418
+
419
+ /**
420
+ * Helper method to extract an archive
421
+ */
422
+ private void doExtraction (final File jdkDir , final ArchiveInputStream ais ,
423
+ final int entryCount ) throws IOException , ExecutionException ,
424
+ InterruptedException
425
+ {
426
+ Task task = taskService .createTask ("Extracting JDK" );
427
+ task .setProgressMaximum (entryCount );
428
+ task .setProgressValue (0 );
429
+ task .start ();
430
+ task .run (() -> {
431
+ try {
432
+ int currentEntry = 0 ;
433
+ ArchiveEntry entry ;
434
+ while ((entry = ais .getNextEntry ()) != null ) {
435
+ if (task != null && task .isCanceled ()) break ;
436
+ if (entry .isDirectory ()) {
437
+ new File (jdkDir , entry .getName ()).mkdirs ();
438
+ }
439
+ else {
440
+ byte [] buffer = new byte [1024 ];
441
+ File outputFile = new File (jdkDir , entry .getName ());
442
+ OutputStream fos = new FileOutputStream (outputFile );
443
+ int len ;
444
+ while ((len = ais .read (buffer )) != -1 ) {
445
+ fos .write (buffer , 0 , len );
446
+ }
447
+ fos .close ();
448
+ }
449
+ task .setProgressValue (++currentEntry );
393
450
}
394
- fos .close ();
395
451
}
396
- }
452
+ catch (IOException e ) {
453
+ task .setStatusMessage ("Java extraction failed!" );
454
+ }
455
+ });
456
+ task .waitFor ();
457
+ task .finish ();
397
458
}
398
459
399
460
/**
0 commit comments