I wrote a JUnit 5 test and I need to create a zip file, put some text files on it and remove the zip file.
I don't have any problem for creating the zip file and the text files inside it, but whenever I call file.delete()
it returns false
.
I even tried to create an empty zip file and it also fails to delete it. Is there a way to solve this ?
static File file;
@BeforeAll
static void setUp() throws IOException {
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file=File.createTempFile("tempDir",".zip")));
ZipEntry e = new ZipEntry("emptyFile.txt");
out.putNextEntry(e);
out.closeEntry();
e = new ZipEntry("oneLineFile.txt");
out.putNextEntry(e);
StringBuilder sb;
byte[] data;
sb = new StringBuilder();
sb.append("route_id,agency_id,route_short_name,route_long_name,route_type");
data = sb.toString().getBytes();
out.write(data, 0, data.length);
out.closeEntry();
out.close();
}
@AfterAll
static void set(){
file.delete(); // return false
}