Get the size of uncompressed data in zlib?

I am creating something that includes a file upload service and I need to store data compressed using the zlib compress () function. I send it over the Internet, already compressed, but I need to know the size of the uncompressed file on the remote server. Is there any way to understand this information without unpacking (first) the data on the server, just for efficiency? This is how I do it now, but if there is a shortcut, I would gladly accept it.

By the way, why is it called uncompress? That sounds pretty awful to me, I always thought it would be unpacking ...

+5
source share
3 answers

zlib , , . gzip format (ISIZE), , , , , .

, , , , - , . uncompress-to-/dev/null , , zlib , .

+3

. , , zlib- ( 7 8 , , , ).

, (, file.zip, file.zip.size), , .

, , , (, nice Linux). , , ( ).

" ", : -)

+4

raw 'compress', . Pax .
4- - , 4 .
C :

 uint8_t *compressBuffer = calloc(bufsize + sizeof (uLongf), 0);
 uLongf compressedSize = bufsize;
 *((uLongf *)compressBuffer) = filesize;
 compress(compressBuffer + sizeof (uLongf), &compressedSize, sourceBuffer, bufsize);

compressBuffer compressizeize size = sizeof (uLongf). , , :

 // data is in compressBuffer, assume you already know compressed size.
 uLongf originalSize = *((uLongf *)compressBuffer);
 uint8_t *realCompressBuffer = compressBuffer + sizeof (uLongf);

If you do not trust the client to send the correct size, you will need to perform some verification of the uncompressed data by the size of the server. The suggestion of using uncompress for / dev / null is reasonable.
If you download a .zip file, it contains a directory that indicates the size of the file when it is not compressed. This information is again embedded in the file format, although it depends on malicious clients.

+3
source

All Articles