# Calculating the true impact of zip file entries

DevFeed: [Calculating the true impact of zip file entries](<https://devfeed.tech/articles/calculating-the-true-impact-of-zip-file-entries-20921.md>)

Original publisher: [Read original article](<https://jakewharton.com/calculating-zip-file-entry-true-impact/>)

Published: 2019-09-20T00:00:00Z

Content type: article

Language: en

Sources: [Jake Wharton](<https://devfeed.tech/sources/jake-wharton.md>)

Topics: [Zip](<https://devfeed.tech/topics/zip.md>), [Java](<https://devfeed.tech/topics/java.md>), [Filesystems](<https://devfeed.tech/topics/filesystems.md>), [mount](<https://devfeed.tech/topics/mount.md>), [Streams](<https://devfeed.tech/topics/streams.md>)

Tags: [api](<https://devfeed.tech/tags/api.md>), [files](<https://devfeed.tech/tags/files.md>), [filesystem](<https://devfeed.tech/tags/filesystem.md>), [java](<https://devfeed.tech/tags/java.md>), [mount](<https://devfeed.tech/tags/mount.md>), [open](<https://devfeed.tech/tags/open.md>), [random](<https://devfeed.tech/tags/random.md>)

## AI overview

This article examines how to calculate the true contribution of each entry to a ZIP file's total size in Java. It explains why summing compressed entry sizes is insufficient and considers the entry headers and central-directory records that also contribute to the final size.

## Source excerpt

How can we determine the impact of each entry on a zip file's size? It seems like a trivial problem, but things quickly don't add up. There's three built-in ways to read information about the contents of zip file in Java: Mount the zip as a FileSystem using FileSystems.newFileSystem and then access its contents using Paths. Open it with ZipInputStream for a one-shot iteration over the zip entries. Open it with ZipFile for random access to the zip entries. The first mechanism is extremely convenient. It allows interacting with the contents of a zip file using the same APIs as normal files. Unfortunately, by virtue of being exposed like regular files, you only have one way to check their size: Files.size(Path). This delegates to an API called BasicFileAttributes.size() which returns size of the file contents. While there is a ZipFileAttributes.compressedSize() for returning the size of the compressed contents, it's internal to the JDK and not available for our use. The other two mechanisms,ZipInputStream and ZipFile, both expose entries using the ZipEntry type. These being zip-centric APIs, many of the properties of the zip file format are directly available. Notably for our use case, there's a getCompressedSize() method. Problem solved? Not exactly... If you sum the compressed size of all entries in a zip the result will not equal the size of the zip file. This isn't entirely unexpected. After all, the zip file format surely requires additional metadata to track per-entry information like the relative path of each compressed file. So if we're looking to calculate the actual size impact of an entry on the final zip, can we do it? Zip file format An overview of the zip file format specification can be found on Wikipedia. It consists of a list of entries which are each defined as header followed by the compressed data (whose length is specified in the header). Finally, at the end, there is a central directory which lists all of the entries available in the file. A slight