Sign in to follow this  

gzip uncompressed file size

This topic is 2546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using the following function to decompress gzipped game assets into a buffer (using the zlib library).

int decompressFileBuffer(const string &fileName, char *pBuffer, int bufferLen)
{
string path;
gzFile file;
int len;
int err;

DEBUGLOG("Decompressing %s...", fileName.c_str());

path = getResourcePath(fileName);

file = gzopen(path.c_str(), "rb");

len = gzread(file, pBuffer, bufferLen);

if (len < 0)
DEBUGLOG("decompressFileBuffer Error: %s", gzerror(file, &err));

gzclose(file);

return len;
}



The problem is I am dealing with a limited RAM platform. I know I can get the size of the compressed file using:

gzseek(file, 0, SEEK_END);
z_off_t fileSize = gztell(file);
gzseek(file, 0, SEEK_SET);


But how about getting the size of the uncompressed file so I can malloc the correct buffer size?

According to the zlib format specs @ http://www.gzip.org/zlib/rfc-gzip.html the header contains

ISIZE (Input SIZE)
This contains the size of the original (uncompressed) input data modulo 2^32.


How can I use zlib to get this value?

Share this post


Link to post
Share on other sites
Okay found this little piece of info

[indent]Quote:The last 4 bytes is the "size of the original (uncompressed) input data modulo 2^32." (gzip.org/zlib/rfc-gzip.html)[/indent]

Based on that I wrote the following function.

[source]unsigned int getGZipFileSize(const string &fileName)
{
string path;
FILE *file;
unsigned int len;
unsigned char bufSize[4];
unsigned int fileSize;

path = getResourcePath(fileName);

file = fopen(path.c_str(), "rb");

if(file == NULL)
return 0;

if(fseek(file, -4, SEEK_END) != 0)
return 0;

len = fread(&bufSize[0], sizeof(unsigned char), 4, file);

if(len != 4)
{
fclose(file);

return 0;
}

fileSize = (unsigned int) ((bufSize[3] << 24) | (bufSize[2] << 16) | (bufSize[1] << 8) | bufSize[0]);

DEBUGLOG("Uncompressed FileSize: %u", fileSize);

fclose(file);

return fileSize;
}[/source]


This seems to work fine except I am wondering about the "input data modulo 2^32" part. Is there an extra calculation I'm missing?

Share this post


Link to post
Share on other sites
Well, if the original file size is less than 2^32 (4 gigabytes if that's 2^32 bytes) then the original file size is equal to the original file size modulo 2^32.

If your original file size is greater than 4 gigabytes you can't find out what size the original file is from just that information.

EDIT: fixed 512 megabytes to 4 gigabytes

[Edited by - sprite_hound on December 26, 2010 1:32:24 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by sprite_hound
Well, if the original file size is less than 2^32 (512 megabytes if that's 2^32 bits) then the original file size is equal to the original file size modulo 2^32.

If your original file size is greater than 512 megabytes you can't find out what size the original file is from just that information.


I think he means 4 gigabytes, not 512. Or am I missing something?

Share this post


Link to post
Share on other sites
Quote:
Original post by fitfool
Quote:
Original post by sprite_hound
Well, if the original file size is less than 2^32 (512 megabytes if that's 2^32 bits) then the original file size is equal to the original file size modulo 2^32.

If your original file size is greater than 512 megabytes you can't find out what size the original file is from just that information.


I think he means 4 gigabytes, not 512. Or am I missing something?


Ah, yes, googling reveals that its in fact in bytes. *fixes*

Share this post


Link to post
Share on other sites

This topic is 2546 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this