Paul Taylor Paul Taylor - 4 years ago 72
Java Question

Problem creating a multiple entry zip file with java

ANyone know whats wrong with this code for creating a zip file containing a number of entries

private File zipAttachments(List<File> licenses) throws IOException
{
byte[] buf = new byte[1024];
File licenseZip = new File("license.zip");
FileOutputStream fos = new FileOutputStream(licenseZip);
ZipOutputStream zip = new ZipOutputStream(fos);
for(File license:licenses)
{
ZipEntry zipEntry = new ZipEntry(license.getName());
FileInputStream in = new FileInputStream(license);
zip.putNextEntry(zipEntry);
int len;
while ((len = in.read(buf)) > 0)
{
zip.write(buf, 0, len);
}
zip.closeEntry();
in.close();
}
zip.close();

return licenseZip;
}


and the stack is

java.util.zip.ZipException: ZIP file must have at least one entry
at java.util.zip.ZipOutputStream.finish(ZipOutputStream.java:304)
at java.util.zip.DeflaterOutputStream.close(DeflaterOutputStream.java:146)
at java.util.zip.ZipOutputStream.close(ZipOutputStream.java:321)


but im pretty sure that the licenses paramter is not an empty list so doesnt that mean I am creating zip entries ?

Answer Source

I think you're missing this as the first lines of your method:

if (licenses.isEmpty())
    throw new IllegalArgumentException("licenses is empty");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download