0

I want to create a temporary file as a ZIP archive. The first step is basically

File.createTempFile ("xxx", ".zip", null);

However, now I cannot get TrueZip add anything to this "archive" -- it obviously isn't an archive yet, it is a zero-byte placeholder file. However, the library doesn't recognize it, tries to read the file and bumps into EOF, of course:

Exception in thread "main" java.io.EOFException
         at de.schlichtherle.truezip.rof.AbstractReadOnlyFile.readFully(AbstractReadOnlyFile.java:37)
         ...

I tried to call TFile.mkdir() on it first, but this gives the same exception as TFile.cp*(), i.e. TrueZip still doesn't want to overwrite the file.

I can of course delete the file first, but that's not quite a proper solution, because it creates a race condition.

  • You're already creating a race condition, because `File.createTempFile` does not open or otherwise lock the created file. – Sneftel Dec 02 '14 at 16:59
  • Well, as I understand other processes shouldn't access temporary files they don't create. –  Dec 02 '14 at 17:12
  • Typical XY problem - why are you creating the file in the first place? Let the zip library handle file creation. – Durandal Dec 02 '14 at 17:33
  • @Durandal: Care to read the question? –  Dec 02 '14 at 17:58
  • Which part? The part "because it would create a reace condition"? Which you have anyway? – Durandal Dec 02 '14 at 18:06
  • 1
    You really need to delete the temporary file first or otherwise TrueZIP will only treat it like a false positive archive file, i.e. like a plain file. – Christian Schlichtherle Dec 03 '14 at 02:33
  • 1
    @ChristianSchlichtherle: OK, thank you for the definitive answer (I recognize your name ;). Please consider this a feature request to request TrueZIP to overwrite an archive file. –  Dec 03 '14 at 09:01
  • Well, this isn't a bug, it's a feature: TrueZIP checks the true state of any prospective archive file and acts accordingly. In this case, the true state is a plain file (empty files are not valid ZIP files) and so you can only treat it like a plain file, e.g. overwrite it with a (T)FileOutputStream. So removing the file and recreating it with TFile.mkdir() is the correct way of changing its true state from a plain file to an archive file / ZIP file. See https://truezip.java.net/apidocs/de/schlichtherle/truezip/file/TFile.html#falsePositives . – Christian Schlichtherle Dec 03 '14 at 15:16
  • That's what I do as a workaround currently. However, there is a tiny chance that between deletion of file and the time TrueZIP writes a new archive to that name, another process creates a temporary file with that exact name. I think that's the reason createTempFile() returns an existing file rather than just the name: to prevent situation where two processes decide to use the same temporary name and then race over who is first to create. –  Dec 03 '14 at 15:43

0 Answers0