I have a list of files and that list may contains duplicate file name but those files resides in different location with different data. Now when I am trying to add those files in zip I am getting java.lang.Exception: duplicate entry: File1.xlsx. Please suggest how I can add duplicate file names. One solution is like if I can rename the dulpicate file as File , File_1,File_2.. But I am not sure how I can achieve it. Please help !!! Below is my working code if all the file names are unique.
Resource resource = null;
try (ZipOutputStream zippedOut = new ZipOutputStream(response.getOutputStream())) {
for (String file : fileNames) {
resource = new FileSystemResource(file);
if(!resource.exists() && resource != null) {
ZipEntry e = new ZipEntry(resource.getFilename());
//Configure the zip entry, the properties of the file
e.setSize(resource.contentLength());
e.setTime(System.currentTimeMillis());
// etc.
zippedOut.putNextEntry(e);
//And the content of the resource:
StreamUtils.copy(resource.getInputStream(), zippedOut);
zippedOut.closeEntry();
}
}
//zippedOut.close();
zippedOut.finish();
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=download.zip").body(zippedOut);
} catch (Exception e) {
throw new Exception(e.getMessage());
}