Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of an empty ByteArrayOutputStream / ZipOutputStream = 22?

Tags:

java

I have a little question about stream size. Here's my try :

ByteArrayOutputStream outStream = new ByteArrayOutputStream();
ZipOutputStream zipStream = new ZipOutputStream(outStream);
zipStream.close();
outStream.close();
System.err.println(outStream.toByteArray().length);

The result length is always 22. Can you explain me why ? Thank you for your help.

like image 727
soane Avatar asked Oct 27 '25 05:10

soane


1 Answers

Because a ZipOutputStream is writing the ZIP file format, and the ZIP file format contains metadata that will always be present, even in an empty zip file. Specifically, an empty zip only contains the End of Central Directory Record:

https://en.wikipedia.org/wiki/Zip_(file_format)#Limits

The minimum size of a .ZIP file is 22 bytes. Such empty zip file contains only an End of Central Directory Record (EOCD): [0x50,0x4B,0x05,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00]

like image 193
weston Avatar answered Oct 29 '25 18:10

weston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!