views:

247

answers:

6

I hate generating an exception for things that I can simply test with an if statement. I know that a zero length zip/jar will trigger an exception if you try to access it using the java.util.zip/java.util.jar APIs. So, it seems like there should be a smallest file that these utility APIs are capable of working with.

+2  A: 

You really should put this sort of code into a try/catch as there are many things that can go wrong when reading/writing files?

If you really must know the answer to this then try to add a 1 byte file to a zip and then see if that fails? It's easy code to go through a range of sizes from 1 -> 65536 bytes and add to a zip and see which ones fail?

Gordon Carpenter-Thompson
The code is in a try/catch block. I don't like to write code that uses exceptions to catch a condition that is testable, though.
vkraemer
Your code must be really crap then, if you think checking the size of a zip is enuff and nothing else can be wrong. Let the zip classes do their thing and let them complain via an exception.
mP
+2  A: 

Jar files need to have at least one entry. If you want to make an empty one make a manifest only jar.

See http://java.sun.com/j2se/1.4.2/docs/guide/jar/jar.html#JAR Manifest for more info on jar manifests.

Ceilingfish
So, it seems like the smallest legal zip file will be smaller than the smallest legal jar?
vkraemer
A: 

Theoretically everything > zero size is valid - pratically this depends on the certain compressor implementation as there may be different header information in a WinZip archive than in a 7-ZIP generated archive.

david
No, the zip spec does not allow just *anything* over zero size. There's a format the zip data must follow.
Cheeso
The spec for example has fields such as 'Version made by', 'File name', 'Version needed to extract' or 'File comment' - depending on the chosen compressor these fields may be set differently. So it's hard to predict what is set...
david
+2  A: 

According to ZIP file format specs a zip file should at least have the central directory structure that is 46 bytes long + 3 variable fields (check the spec by yourself).

Maybe we should assume at least 1 entry that implies the file header for that entry.

Jack
(PlugIn/WebStart will reject any zip/jar that doesn't start with a an entry header magic number (for protection against GIFARs).)
Tom Hawtin - tackline
No, the smallest legal/valid zip file is 22 bytes: `80 75 05 06 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00`. I don't know about jar files, though.
Cheeso
A: 

The smallest legal zip contains zero entries, and one "empty" central directory.

The bytes are:

 80 75 05 06 

followed by 18 bytes of zero (0).

So, 22 bytes long.

VBscript to create it:

Sub NewZip(pathToZipFile)

    WScript.Echo "Newing up a zip file (" & pathToZipFile & ") "

    Dim fso
    Set fso = CreateObject("Scripting.FileSystemObject")
    Dim file
    Set file = fso.CreateTextFile(pathToZipFile)

    file.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
    file.Close

    Set fso = Nothing
    Set file = Nothing
    WScript.Sleep 500
End Sub


NewZip "Empty.zip"
Cheeso
That may work for the zip program, but it seems to be unacceptable to the java.util.zip.* apis.
vkraemer
+1  A: 

I wrote a quick test and the smallest zip that I could create and then read back with the java.util.zip APIs was 118 byte. There may be a way to create a smaller zip file that is spec compliant and readable with the zip utility...

vkraemer