user2960606
user2960606

Reputation: 95

Java: How to decompress files (zip or tar.gz) with low memory usage?

I have the following requirement: I need to unpack a zip or tar.gz file. The target platform where this code will run is an AEM 6.1 environment. The system had some performance issues in the past. Especially memory usage was much to high. Therefore I have to save memory. The zip/tar.gz file contains some text-Files, SVG, PNG and EPS files as well as more files I don't need. The archive file will be downloaded and available as an ByteArrayInputStream.

I did some research and tried to figure out, which is the best way to do that. Apache commons provides libraries to unpack archives as well as the JDK. But I could not figure out, what implementation uses less memory.

I think, it would be the best, if I could open the archive while it is still compressed, and read and unpack the containing files separately. So I would just have the compressed archive and the one of its containing file in in memory.

But I'm not sure, which implementation provides this possibility or if there is a better way to do that.

Has somebody a good advice?

Thank you and best regards.

Upvotes: 0

Views: 1436

Answers (1)

bashnesnos
bashnesnos

Reputation: 816

ZipInputStream from the JDK does just what you need: https://docs.oracle.com/javase/8/docs/api/java/util/zip/ZipInputStream.html

You can find the entry you need via getNextEntry().getName(), and read the bytes just for that entry. The ZipInputStream.read method allows you to implement buffered read, so you can easily limit the memory consumption if you don't need the whole decompressed entry in memory (i.e. if you write the entry into output as you read it).

In this case you can minimize the footprint of you application as well, since you won't need any extra libraries.

Upvotes: 2

Related Questions