Tutorial :What is the smallest, legal zip/jar file?



Question:

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.


Solution:1

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.


Solution:2

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?


Solution:3

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.


Solution:4

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...


Solution:5

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"  


Solution:6

final static byte[] EmptyZip={80,75,05,06,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00,00};  public static void createEmptyZip(String path){      try{          FileOutputStream fos=new FileOutputStream(new File(path));          fos.write(EmptyZip, 0, 22);          fos.flush();          fos.close();      }catch (FileNotFoundException e){          e.printStackTrace();      } catch (IOException e) {          e.printStackTrace();      }    }  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »