1

I have the following piece of code -

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
ZipOutputStream zos = new ZipOutputStream(outputStream);

for (int i = 0; i < params.getGrades().size(); i++) {
generateReport(param1, param2, zos);
}

zos.flush();
zos.close();

In the generateReport method, I have code to generate my reports as xls files and add them to ZIP.

Is there any way we can check if any files have been written in the ZIP file, or if the ZIP file is empty? is there any property I can use?

Thanks, Raaz

Raaz
  • 125
  • 1
  • 5
  • 16
  • 1
    Maybe you could simply modify `generateReport` so that it returns `true` when it has added some file. If no call to `generateReport` returns `true`, then your zip is empty… – Didier L Jun 29 '12 at 08:37
  • Thanks, but I thought of that.. I call a lot of methods before I add my files to ZIP, so if I have a flag returning true if my ZIP file is empty, then I would have to pass back the flag from all those methods. – Raaz Jun 29 '12 at 08:55

5 Answers5

3

You can use the ZipFile from the java.util.zip package.

You can invoke the

size()

method.

setzamora
  • 3,560
  • 6
  • 34
  • 48
2

After you close zos, outputStream.size() gives you the number of bytes written. You would have to allow for whatever the ZIP header size is for an empty ZIP file.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • FYI it's `22` http://stackoverflow.com/a/41507106/360211 and surprisingly not a "header" but an end of directory record. – weston Jan 06 '17 at 14:14
0

@Raaz, Please go through this link.

In that you can see a Class called 'ZipEntry'. It represents the files contained in a zip folder. It provides some useful methods such as:

zipEntry.getName(); // name of the file contained by zip.
zipEntry.getSize(); // size of the file contained by zip.
Community
  • 1
  • 1
RAS
  • 8,100
  • 16
  • 64
  • 86
  • I went through the link you had mentioned.. I see that it requires me to have a ZipInoutStream object which I can get entries from. I have a ZipOutputStream Object into which I have written files into.. and would like to know if the file created is empty.. – Raaz Jun 29 '12 at 09:05
-1

@Didier - I decided to take your advice on returning a value, but ended up doing it this way -

Instead of checking if a file has been added to the ZIP, I checked if the list data I'm trying to write in an xls file (the file which in turn gets added to the ZIP) is empty. If it's empty, then I set a error value to "No file generated". If the list is not empty, I assigned the an empty value to the string and returned it to the calling function.

Raaz
  • 125
  • 1
  • 5
  • 16