@@ -385,7 +385,9 @@ protected Boolean doInBackground() {
385385
386386 JStock .instance ().commitBeforeSaveToCloud ();
387387
388- final File zipFile = getJStockZipFile ();
388+ // Passing null. The file will be deleted automatically when the
389+ // app quit.
390+ final File zipFile = getJStockZipFile (null );
389391
390392 // Place isCancelled check after time consuming operation.
391393 // Not the best way, but perhaps the easiest way to cancel
@@ -577,17 +579,18 @@ private static List<String> getExtensions(String name) {
577579 return java .util .Collections .unmodifiableList (extensions );
578580 }
579581
580- private File getJStockZipFile () {
582+ // filename can be null, to indicate temp file.
583+ private File getJStockZipFile (final String filename ) {
581584 // Look for "user-defined-database.xml" for all countries.
582585 final List <File > files = getUserDefinedDatabaseFiles ();
583586 final List <FileEx > fileExs = new ArrayList <>();
584587 for (File file : files ) {
585- final String filename = file .getAbsolutePath ();
586- final int index = filename .indexOf (Utils .getApplicationVersionString ());
588+ final String absolutePath = file .getAbsolutePath ();
589+ final int index = absolutePath .indexOf (Utils .getApplicationVersionString ());
587590 if (index < 0 ) {
588591 continue ;
589592 }
590- final String output = filename .substring (index + Utils .getApplicationVersionString ().length () + File .separator .length ());
593+ final String output = absolutePath .substring (index + Utils .getApplicationVersionString ().length () + File .separator .length ());
591594 fileExs .add (FileEx .newInstance (file , output ));
592595 }
593596
@@ -636,13 +639,21 @@ private File getJStockZipFile() {
636639
637640 ZipOutputStream out = null ;
638641 File temp = null ;
639-
642+
643+ boolean exceptionOccur = false ;
644+
640645 try {
641646
642- // Create the ZIP file
643- temp = File .createTempFile (Utils .getJStockUUID (), ".zip" );
644- // Delete temp file when program exits.
645- temp .deleteOnExit ();
647+ if (Utils .isNullOrEmpty (filename )) {
648+ // Create the ZIP file
649+ temp = File .createTempFile (Utils .getJStockUUID (), ".zip" );
650+
651+ // Delete temp file when program exits.
652+ temp .deleteOnExit ();
653+ } else {
654+ temp = new File (filename );
655+ }
656+
646657 out = new ZipOutputStream (new FileOutputStream (temp ));
647658
648659 // Compress the files
@@ -659,26 +670,29 @@ private File getJStockZipFile() {
659670 while ((len = in .read (buf )) > 0 ) {
660671 out .write (buf , 0 , len );
661672 }
662- }
663- catch (IOException exp ) {
664- log .error (null , exp );
665- // Should we return null? As the saved information is not complete.
666- continue ;
667- }
668- finally {
673+ } finally {
669674 // Complete the entry
670675 Utils .closeEntry (out );
671676 Utils .close (in );
672677 }
673678 }
674- }
675- catch (IOException exp ) {
679+ } catch (IOException exp ) {
676680 log .error (null , exp );
681+
682+ exceptionOccur = true ;
683+
677684 return null ;
678- }
679- finally {
685+ } finally {
680686 Utils .close (out );
687+
688+ // Deletion should be performed after Utils.close to free up resource.
689+ if (exceptionOccur ) {
690+ if (temp != null ) {
691+ temp .delete ();
692+ }
693+ }
681694 }
695+
682696 return temp ;
683697 }
684698
0 commit comments