How can I easily compress and decompress files using zlib?
Asked
Active
Viewed 3.0k times
14
-
Have you read the [zlib](http://zlib.net/manual.html) [documentation](http://zlib.net/zlib_how.html)? If so, what have you tried? If what you tried didn't work, then post your code and say what happened. – Mark Adler Oct 16 '17 at 23:30
3 Answers
18
For decompression:
char buf[1024*1024*16];
gzFile *fi = (gzFile *)gzopen("file.gz","rb");
gzrewind(fi);
while(!gzeof(fi))
{
int len = gzread(fi,buf,sizeof(buf));
//buf contains len bytes of decompressed data
}
gzclose(fi);
For compression
gzFile *fi = (gzFile *)gzopen("file.gz","wb");
gzwrite(fi,"my decompressed data",strlen("my decompressed data"));
gzclose(fi);

Cristi
- 1,488
- 1
- 14
- 14
-
@Cristi: I'm getting an error: `main.cpp invalid conversion from `const void*' to `void*'` on this line: `gzwrite(fi,"my decompressed data",strlen("my decompressed data"));`. Could you please help me? – Datoxalas Apr 13 '11 at 12:37
-
try gzwrite(fi,(void const *)"my decompressed data",strlen("my decompressed data")); – Cristi Apr 13 '11 at 12:43
-
-
well it seems that in your zlib distribution gzwrite takes void * as second parameter instead of const void * as mine does. A quick bad fix would be gzwrite(fi,(void*)"my decompressed data",strlen("my decompressed data")); – Cristi Apr 13 '11 at 12:50
-
1@Cristi: Now I am getting: `[Linker error] undefined reference to 'gzopen'`. – Datoxalas Apr 13 '11 at 12:57
-
if you are using visual studio you need to add the zlib .lib file to your project. In project properties: "configuration properties-> linker >general-> additional lib directories" add the folder path to the library and in "configuration properties-> linker >input-> additional dependencies" at the zlib lib file name. The name of the zlib lib file name may vary according to your distribution. it may be zdll.lib if you link dynamically to zlib or zlib.lib for static linkage. – Cristi Apr 13 '11 at 13:12
-
@Cristi: Okay, now the compressing is working, but the decompressing is not. `error C3861: 'gzrewind': identifier not found` And `error C3861: 'gzeof': identifier not found`. – Datoxalas Apr 13 '11 at 13:41
-
I'm using zlib version 1.2.5, April 19th, 2010, what version are you using? You can find the version in zlib.h, if you have a old version can't you just update to the latest version? If not I guess you could try: gzFile *fi = (gzFile *)gzopen("file.gz","rb"); int len=1; while(len>0) { len = gzread(fi,buf,sizeof(buf)); //buf contains len bytes of decompressed data } gzclose(fi); Not the code above is untested. – Cristi Apr 13 '11 at 13:49
-
@cristi: Okay, I've upgraded my zlib.h, zdll.lib and zlib1.dll. Now I am getting the following error: `Unhandled exception at 0x00411707 in zlib2.exe: 0xC00000FD: Stack overflow.` Do i also need to upgrade other files? – Datoxalas Apr 13 '11 at 14:25
-
make sure you do a clean rebuild and replace zlib1.dll located in the exe folder with the new one and it shoud work – Cristi Apr 13 '11 at 14:38
-
@Cristi: I have the new version, 1.2.5. Could you make a package for me please? so I can download it? – Datoxalas Apr 14 '11 at 07:24
1
Please read through this. The information is already available here:
That is the first link that shows up even on google.

Alok Save
- 202,538
- 53
- 430
- 533
-
3I don't find the documentation very easy. Isn't there just a simple function like compress() decompress()? – Datoxalas Apr 13 '11 at 12:29
-
1@Downvoter: Care to explain why the downvote? The OP never mentioned in the Orginal Question that he/she did not understand the examples on zlib website. I provided link to those, whats wrong with that? – Alok Save May 06 '11 at 07:57
-
1i'd say anyone who asks a question like this on SO should be pretty obvious that they already RTFM and still need help, I find the zlib "examples" to be very unhelpful myself, most of us arent trying to uncompress stdin, for example. – osirisgothra Nov 15 '13 at 13:14
-
@Datoxalas Yes, there are in fact `compress()` and `uncompress()` functions provided by zlib. Have you even looked at the documentation? – Mark Adler Oct 16 '17 at 23:27
-1
If you can use boost, I would recommend the boost iostreams API. See the boost iostreams tutorial. It has support for GZIP and BZIP2 compressors and decompressors.

Dan
- 1,339
- 3
- 13
- 19