What is the smallest, legal zip / jar file?

I don't like to throw an exception for things that I can just check with the if statement. I know that zip / jar with zero length will throw an exception if you try to access it using the java.util.zip/java.util.jar API. So it seems that there should be the smallest file that these APIs can work with.

+3
source share
6 answers

In accordance with the ZIP format specifications, the zip file must at least have a central directory structure that is 46 bytes long + 3 variable fields (check the specification yourself).

Perhaps we should accept at least 1 entry, which implies a file header for this entry.

+2
source

Do you really have to put this type of code in try / catch, since there are many things that can go wrong when you read / write files?

If you really need to know the answer to this question, try adding a 1-byte file to the zip file, and then see if it worked out? Lightweight code goes through a range of sizes from 1 to 65536 bytes and adds to zip and sees which ones fail?

+3

, , API java.util.zip 118 . , zip , zip...

+1

"" .

:

 80 75 05 06 

18 (0).

, 22 .

VBscript :

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"
0
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();
    }

}
0

Source: https://habr.com/ru/post/1736797/


All Articles