Sagar
Sagar

Reputation: 2385

Compressing Image Files using gzip

I have .PNG files in a folder. I want to compress them and create .gz file.I appended the data but still it contains a single file for many compressed files. and as the zlib.compress() accept the string to compress so I have to read the data in from file and then have to compress it and write in the gz file. that eventually create the single file. But question comes is ,How do I decompress those files then again.

Tried out this much untill now:

gip = gzip.open("out.gz",'a')
for f in image_list:
   indata = open(f,'r').read()
   gip.write(zlib.compress(indata))
gip.close()

Upvotes: 2

Views: 2812

Answers (1)

Mark Adler
Mark Adler

Reputation: 112339

gzip is only for compression of a single stream. To maintain the structure of multiple files and directories, you need the tar format, which is usually compressed with gzip. The resulting filename suffix is .tar.gz.

Use tarfile in Python, which will handle both the tarring and the gzipping.

Upvotes: 1

Related Questions