Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I easily compress and decompress files using zlib? [closed]

How can I easily compress and decompress files using zlib?

like image 330
Datoxalas Avatar asked Apr 13 '11 12:04

Datoxalas


People also ask

How do I decompress a string in zlib?

zlib.decompress (s) in Python. With the help of zlib.decompress (s) method, we can decompress the compressed bytes of string into original string by using zlib.decompress (s) method. Return : Return decompressed string.

Is there a utility to uncompress raw ZLib data?

Is there a utility to uncompress raw zlib data? It is also possible to decompress it using standard shell-script + gzip, if you don't have, or want to use openssl or other tools. This Q on SO gives more information about this approach. An answer there suggests that there is also an 8 byte footer.

How to use gzip with zlib?

zlib implements the compression used by gzip, but not the file format. Instead, you should use the gzip module, which itself uses zlib. import gzip s = '...' with gzip.open ('/tmp/data', 'w') as f: f.write (s) ok, but my situation is that i have tens/hundreds thousands of those files created, so.. :) so... your files are incomplete.

What is the read-write option used for in zlib?

This read-write option is used by decompressing channels to control the maximum number of bytes ahead to read from the underlying data source. This defaults to 1, which ensures that data is always decompressed correctly, but may be increased to improve performance. This is more useful when the channel is non-blocking. zlib stream mode ? options?


2 Answers

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);
like image 56
Cristi Avatar answered Oct 23 '22 14:10

Cristi


Please read through this. The information is already available here:
That is the first link that shows up even on google.

like image 21
Alok Save Avatar answered Oct 23 '22 13:10

Alok Save