# gzip uncompressed file size

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

## 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 &lt; 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 on other sites
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 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 on other sites
Quote:
 Original post by sprite_houndWell, 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 on other sites
Quote:
Original post by fitfool
Quote:
 Original post by sprite_houndWell, 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 on other sites
The largest files I'm decompressing are only a few megabytes in size so I take it the code will be fine as is.

1. 1
Rutin
25
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 14
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631757
• Total Posts
3002139
×