How to create a zip file where the uncompressed size of an entry is unknown [duplicate]
Asked Answered
R

1

1

The class java.util.zip.ZipEntry has a method getSize that returns -1 if the uncompressed size is unknown.

I understand this can happen if I have a ZipInputStream. Can this also happen if I have a ZipFile?

I have code that reads a .zip file and needs to know the uncompressed size of a zip entry, and I ask because I need to know whether I need to handle the case that getSize returns -1.

If it is possible that getSize returns -1, then I'd like to know how I should go about constructing such a .zip file, so that I can write a test to confirm I'm handling it correctly.

Refreshment answered 8/5 at 9:28 Comment(2)
Thank you for the suggestion, @aled. I now suspect that using ZipFile is enough, but I'm still not sure. I posted a comment over there summarizing my findings.Refreshment
@aled you asked whether this is a duplicate, and I responded No. But now I understand I should have responded Yes. I apologize for this mistake. I don't know how to bring back the question. Anyway, I have now voted to close this as a duplicate.Refreshment
I
1

See the spec: https://pkwaredownloads.blob.core.windows.net/pem/APPNOTE.txt

Specifically, section 4.4.4 "general purpose bit flag". If bit 3 is set, the size fields in the Local File Header may be omitted if not known, and the actual size would then be in the Central Directory Header(at the end of the file).

If you take the ZipEntry from a ZipFile, its properties will be taken form the Central Directory Header, and therefore the size will be known (and not -1).

If you take it from a ZipInputStream, this stream has not yet seen the Central Directory Header (as it's only at the end) and therefore the ZipEntry comes from the Local File Header, which may omit the size.

Indium answered 8/5 at 13:47 Comment(1)
Very interesting, thank you very much. Most fascinating.Refreshment

© 2022 - 2024 — McMap. All rights reserved.