-1

I am trying to create a zip archive in java. Every iteration adds some data to a ZipEntry and I wanted to flush the contents after every iteration ( due to server restart handling ).

But ZipOutputStream flush() does not work. i.e. contents are not pushed to the exact file. Only close() method pushes all data to the file. Any idea why this happens?

I saw somewhere that flush don't work when file is compressed. Is it true? And how to overcome that?

  • 2
    Is this a continual long-running operation? If yes, create a new file for each iteration. You might consider GZIP instead of ZIP for that. – Andreas Feb 15 '21 at 03:23

1 Answers1

1

If you look at the structure of a zip archive, you'll see that the "central directory" is at the end of the file, i.e. it doesn't get written until you call close().

Calling flush() might force the zipped content to be written to disk, but the critical "directory" information hasn't been written yet, so it cannot get flushed to disk.

ZIP-64 Internal Layout

image

Andreas
  • 154,647
  • 11
  • 152
  • 247