Jump to content
  • Advertisement
Sign in to follow this  
Headkaze

gzip uncompressed file size

This topic is 2851 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
Advertisement
Okay found this little piece of info

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


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!