Sells, Fred wrote: > I'm tring to unzip a buffer that is uploaded to django/python. I can > unzip the file in batch mode just fine, but when I get the buffer I get > a "BadZipfile exception. I wrote this snippet to try to isolate the > issue but I don't understand what's going on. I'm guessing that I'm > losing some header/trailer somewhere?
Why are you copying the file before you try to unzip it? > def unittestZipfile(filename): > buffer = '' > f = open(filename) Open the file in binary mode: f = open(filename, "rb") > for i in range(22): > block = f.read() > if len(block) == 0: > break > else: > buffer += block > print len(buffer) > tmp = open('tmp.zip', 'w') Again, open the file in binary mode ("wb"). > tmp.write(buffer) > tmp.close() Not essential to the problem you are encountering, but you want shutil.copyfile(). Also, have a look at the shutil.copyfileobj() implementation to learn how to properly copy a file in chunks of limited size. > zf = zipfile.ZipFile('tmp.zip') > print dir(zf) > for name in zf.namelist(): > print name > print zf.read(name) > ____________________________________________________________ > 2.6.6 (r266:84297, Aug 24 2010, 18:46:32) [MSC v.1500 32 bit (Intel)] > Traceback (most recent call last): > File > "C:\all\projects\AccMDS30Server\mds30\app\uploaders\xmitzipfile.py", > line 162, in <module> > unittestZipfile('wk1live7.8to7.11.zip') > File > "C:\all\projects\AccMDS30Server\mds30\app\uploaders\xmitzipfile.py", > line 146, in unittestZipfile > print zf.read(name) > File "C:\alltools\python26\lib\zipfile.py", line 837, in read > return self.open(name, "r", pwd).read() > File "C:\alltools\python26\lib\zipfile.py", line 867, in open > raise BadZipfile, "Bad magic number for file header" > zipfile.BadZipfile: Bad magic number for file header -- http://mail.python.org/mailman/listinfo/python-list