Search code examples
javafilearchivedelete-filerar

Extract rar file through Junrar and then delete the archive


I'm using junrar but I have a problem to delete the archive after extraction. This is the code:

ExtractArchive extractArchive = new ExtractArchive();  
extractArchive.extractArchive(new File(archiveFilePath), new File(destDirectory));

after this code I can't delete file associated to archiveFilePath. I tried both new File(archiveFilePath).delete(); and apache commons io

FileDeleteStrategy.FORCE.delete(new File(archiveFilePath));

but I receive false or one exception because file is used from another process. Someone has had the same problem with this library? Can I remove the file in other ways? Thanks

Here there is a confirm of the issue: https://github.com/edmund-wagner/junrar/issues/28


Solution

  • The code in ExtractArchive.extractArchive(...) is defect as it creates internally an Archive instance but it never calls close() to free up it's resources and file handles.

    On Windows this results in a locked file that can not be deleted unless the JVM ends or the garbage collector has collected all object instances of that Archive and therefore has freed the file handles.

    On Windows there is no other way to delete the file!