|
17 | 17 | import java.util.ArrayList; |
18 | 18 | import java.util.Collection; |
19 | 19 | import java.util.HashMap; |
| 20 | +import java.util.HashSet; |
20 | 21 | import java.util.List; |
21 | 22 | import java.util.Locale; |
22 | 23 | import java.util.Map; |
| 24 | +import java.util.Set; |
23 | 25 | import java.util.TimeZone; |
24 | 26 | import java.util.concurrent.ExecutorService; |
25 | 27 | import java.util.concurrent.Executors; |
@@ -424,8 +426,24 @@ private Response handleDownload(Map<String, Collection<String>> data) throws IOE |
424 | 426 | // download multiple as zip |
425 | 427 | ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
426 | 428 | ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream); |
| 429 | + Set<String> fileNameSet = new HashSet<>(); |
427 | 430 | for (String fileName : fileNames) { |
428 | 431 | Response response = getFile(remoteHost, fileName, false); |
| 432 | + // de-duplicate the fileName entry to avoid a duplicate entry/zip exception |
| 433 | + int index = 1; |
| 434 | + String originalName = fileName; |
| 435 | + while (fileNameSet.contains(fileName)) { |
| 436 | + int dot = originalName.lastIndexOf('.'); |
| 437 | + if (dot != -1) { |
| 438 | + String extension = originalName.substring(dot + 1); |
| 439 | + String name = originalName.substring(0, dot); |
| 440 | + fileName = String.format(Locale.ENGLISH, "%s (%d).%s", name, index, extension); |
| 441 | + } else { |
| 442 | + fileName = String.format(Locale.ENGLISH, "%s (%d)", originalName, index); |
| 443 | + } |
| 444 | + index++; |
| 445 | + } |
| 446 | + fileNameSet.add(fileName); |
429 | 447 | ZipEntry entry = new ZipEntry(fileName); |
430 | 448 | zipOutputStream.putNextEntry(entry); |
431 | 449 | response.toStream(zipOutputStream); |
|
0 commit comments