Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!news2.arglkargh.de!news.wiretrip.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; 'subject:using': 0.04; 'snippet': 0.07; 'exception.': 0.09; 'fred': 0.09; 'mode:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'skip:" 60': 0.09; 'binary': 0.13; 'wrote:': 0.15; '"bad': 0.16; '"r",': 0.16; "'w')": 0.16; 'chunks': 0.16; 'f.read()': 0.16; 'guessing': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:t-dialin.net': 0.16; 'somewhere?': 0.16; 'subject:raw': 0.16; 'tmp': 0.16; 'def': 0.16; 'aug': 0.19; 'subject:problem': 0.19; 'wrote': 0.21; '(most': 0.21; 'fine,': 0.23; 'traceback': 0.25; 'essential': 0.25; "i'm": 0.27; 'raise': 0.28; 'mode': 0.28; 'skip:" 30': 0.28; 'bit': 0.28; 'problem': 0.29; 'from:addr:web.de': 0.30; 'copying': 0.31; 'print': 0.32; 'break': 0.33; "what's": 0.33; 'to:addr:python- list': 0.34; 'header:X-Complaints-To:1': 0.34; 'it?': 0.34; 'last):': 0.35; 'file': 0.36; 'issue': 0.37; 'skip:z 20': 0.37; 'open': 0.37; 'some': 0.37; 'but': 0.37; 'received:org': 0.38; 'subject:: ': 0.38; 'header:Mime-Version:1': 0.39; 'skip:s 20': 0.39; 'to:addr:python.org': 0.39; 'header': 0.40; 'skip:z 10': 0.40; 'batch': 0.68; 'size.': 0.71; 'subject: ': 0.73; 'isolate': 0.84; 'subject:; ': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: reading zipfile; problem using raw buffer Date: Tue, 26 Jul 2011 15:04:59 +0200 Organization: None References: <08098816-0332-4ae2-a125-35418bac0da2@dp9g2000vbb.googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084bafe.dip.t-dialin.net X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 60 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1311685503 news.xs4all.nl 23875 [2001:888:2000:d::a6]:57753 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10333 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 > 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