I am using FileUtils.saveAndUnzipToTempDir
for unzipping folder/files to a temp directory, and I want to delete that folder once I'm finished going through the folder.
I don't have a great java experience, but I think that the saveAndUnzipToTempDir
process is not finished yet, and don't know how to wait the process to finish, or how to kill it:
This is my code:
public boolean postFile(@RequestParam String filename, @RequestParam MultipartFile file) {
FileResultsDTO fileResultsDTO = FileUtils.saveAndUnzipToTempDir(file, filename);
for(String fileName : fileResultsDTO.getUnzipDir().list()){
// do something.
}
//Find the temp folder where the other files/folders are uploaded and
//deleted those
File directory = new File(FileUtils.getTempDirectoryPath());
FileUtils.deleteDirectory(directory);
FileUtils.forceDelete(directory);
delete(directory);
}
public static void delete(File file)
throws IOException {
if(file.isDirectory()){
//directory is empty, then delete it
if(file.list().length==0){
FileDeleteStrategy.FORCE.delete(file);
FileUtils.deleteQuietly(file);
file.delete();
System.out.println("Directory is deleted : "
+ file.getAbsolutePath());
}else{
//list all the directory contents
String files[] = file.list();
for (String temp : files) {
//construct the file structure
File fileDelete = new File(file, temp);
//recursive delete
delete(fileDelete);
}
//check the directory again, if empty then delete it
if(file.list().length==0){
FileDeleteStrategy.FORCE.delete(file);
FileUtils.deleteQuietly(file);
file.delete();
System.out.println("Directory is deleted : "
+ file.getAbsolutePath());
}
}
}else{
//if file, then delete it
FileDeleteStrategy.FORCE.delete(file);
FileUtils.deleteQuietly(file);
file.delete();
System.out.println("File is deleted : " + file.getAbsolutePath());
}
}
So if you see the code closely, you can see that I've tried using also:
FileDeleteStrategy.FORCE.delete(file);
FileUtils.deleteQuietly(file);
file.delete();