The gzip module... neverending story
Andrew M. Kuchling
akuchlin at mems-exchange.org
Tue Apr 11 11:05:45 EDT 2000
"FrancescO" <francesco.contini at tiscalinet.it> writes:
> I have, another questions!!
> How can I zip i list of files? (i have done it, but... the files can't be
> readed from winzip and gunzip...)
First, if you're trying to generate PKZIP-style .zip archives, the
gzip module is the wrong one. Jim Ahlstrom's zipfile.py, which
someone else posted a pointer to, is the right module to use in that
case. Since you mention gunzip, I assume that you really do want
gzip-style files.
> Wich is the correct usage of the fileobj (4th parameters in the
> gzip.GzipFile.. explain..:
> GzipFile ([filename[, mode[, compresslevel[, fileobj]]]]) )?
Did you look at the documentation for the module, at
http://www.python.org/doc/lib/module-gzip.html ? It explains:
The new class instance is based on fileobj, which can be a
regular file, a StringIO object, or any other object which
simulates a file. It defaults to None, in which case filename
is opened to provide a file object.
It's not clear what you're trying to do: compress a list of files into
individual compressed files, or compress a bunch of files into one big
huge file. To do the former, the code would be something like (untested):
import gzip
filenames = ['a.txt', 'b.txt', 'c.exe']
for f in filenames:
input = open(f, 'rb')
output = gzip.open(f + '.gz', 'wb')
# Copy the contents of the input file
while 1:
chunk = input.read(1024)
if chunk == "": break
output.write( chunk )
input.close() ; output.close()
gzip doesn't support storing multiple files, though you can
concatenate several gzipped files; the decompressed data would then be
the contents of the original files in one big file.
--
A.M. Kuchling http://starship.python.net/crew/amk/
Desire was right. Also untrustworthy, acerbic, dangerous, and cruel. But
right. You would have been better off leaving well enough alone.
-- Destruction, in SANDMAN #48: "Brief Lives:8"
More information about the Python-list
mailing list