EDIT: The error was not in this code. The download on the front-end was erroneous. The zip on the server was fine but downloading it via js didn't work.
I'm writing two xml files into a zip file. I can't open the resulting file (error: no file and no folder).
Tuple is a class that contains two Strings (A and B), my xml content. I first tried this without closeEntry
and without setSize
, the result is the same.
Any ideas? How can I debug this?
private static byte[] createArchive(final Tuple<String, String> body) throws IOException {
try (final ByteArrayOutputStream bos = new ByteArrayOutputStream();
final ZipOutputStream zos = new ZipOutputStream(bos)) {
final ZipEntry firstEntry = new ZipEntry("first.xml");
firstEntry.setSize(body.getA().getBytes().length);
zos.putNextEntry(firstEntry);
zos.write(body.getA().getBytes());
zos.closeEntry();
final ZipEntry secondEntry = new ZipEntry("second.xml");
secondEntry.setSize(body.getB().getBytes().length);
zos.putNextEntry(secondEntry);
zos.write(body.getB().getBytes());
zos.closeEntry();
zos.close();
return bos.toByteArray();
}
}