I'd like to create a zip which stores two different files with the same name, but I'm unable (using java.util.zip.ZipOutputStream
) due to
java.util.zip.ZipException: duplicate entry:
exception. I know that it's possible, but I need an advise which library I can use for that purposes. Thanks!
UPD the code I'm using:
File zipFile = new File("C:\\Users\\user\\Desktop\\old.zip");
File outFile = new File("C:\\Users\\user\\Desktop\\new.zip");
if(!outFile.exists()) {
outFile.getParentFile().mkdirs();
outFile.createNewFile();
}
byte[] buf = new byte[1024];
ZipInputStream zin = new ZipInputStream(new FileInputStream(zipFile));
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outFile));
ZipEntry entry = zin.getNextEntry();
while (entry != null) {
String name = entry.getName();
out.putNextEntry(new ZipEntry(name));
int len;
while ((len = zin.read(buf)) > 0) {
out.write(buf, 0, len);
}
entry = zin.getNextEntry();
if("file".equals(name)) {
File fakeFile = new File("C:\\Users\\user\\Desktop\\file");
InputStream in = new FileInputStream(fakeFile);
out.putNextEntry(new ZipEntry("file"));
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();
}
}
zin.close();
out.close();
if (! names.add(e.name)) {
but you may check wherenames
object is used and suddenly only to disallow duplicates. So it's possible but not with that library – Assonance