I want to zip multiple files from different locations into one.
Here is my code
public class CreateZipFileFromMultipleFilesWithZipOutputStream {
public static void main(String[] args) {
String zipFile = "C:/archive.zip";
String[] srcFiles = { "C:/data1/srcfile1.txt", "C:/data2/srcfile2.txt", "C:/data3/srcfile2.txt"};
try {
FileOutputStream fos = new FileOutputStream(zipFile);
ZipOutputStream zos = new ZipOutputStream(fos);
for (int i=0; i < srcFiles.length; i++) {
File srcFile = new File(srcFiles[i]);
ZipEntry zipEntry = new ZipEntry(srcFile.getName());
zos.putNextEntry(zipEntry);
zos.write(Files.readAllBytes(srcFile.toPath()));
zos.closeEntry();
}
// close the ZipOutputStream
zos.close();
}
catch (IOException ioe) {
System.out.println("Error creating zip file: " + ioe);
}
}
}
This throws an exception: Error creating zip file: java.util.zip.ZipException: duplicate entry: dsdc.zip
Is there any way to check if the file exists in zip stream? So in that case I can append _1 to the duplicate file name?