0
votes

I have a file that contains an embedded file that is compressed using zlib.

I know where the compressed file starts within the main file and I have the decompressed file size, but don't know where the compressed file ends.

My goal is to extract the compressed file from its containing file and save it as its own file.

Given the start location of the compressed file and the decompressed file size, is there anything that will tell me the compressed file size or where the compressed file ends?

Presently I pass from the zlib header to EOF into a inflate function. This works fine for smaller files as the inflate function will discard any data after the end of the compressed file and only return me the decompressed bytes. However for larger files where the compressed file is located near the top of the file, my system does not have enough memory to pass the majority of the file into the inflate function.

1
minimal reproducible example wouldn't hurt (with something to generate sample file as a bonus ;) ). If you're running out of memory, I'd say there's something wrong with your approach... but hard to tell without seeing the code.Dan Mašek

1 Answers

1
votes

A zlib stream is self-terminating. You find the compressed length by decompressing and seeing how many bytes have been consumed when it finishes.

You mention something about not having enough memory. The main zlib functions process a piece of the stream at a time, which are the inflate* functions for decompression. Read zlib.h.