Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Copying a ZipExtFile
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Moore, Mathew L  
View profile  
 More options Oct 23 2009, 1:15 pm
Newsgroups: comp.lang.python
From: "Moore, Mathew L" <Moor...@BATTELLE.ORG>
Date: Fri, 23 Oct 2009 13:15:33 -0400
Local: Fri, Oct 23 2009 1:15 pm
Subject: Copying a ZipExtFile
Hello all,

A newbie here.  I was wondering why the following fails on Python 2.6.2 (r262:71605) on win32.  Am I doing something inappropriate?

Interestingly, it works in 3.1, but would like to also get it working in 2.6.

Thanks in advance,
--Matt

import io
import shutil
import tempfile
import zipfile

with tempfile.TemporaryFile() as f:
    # (Real code retrieves archive via urllib2.urlopen().)
    zip = zipfile.ZipFile(f, mode='w')
    zip.writestr('unknowndir/src.txt', 'Hello, world!')
    zip.close();

    # (Pretend we just downloaded the zip file.)
    f.seek(0)

    # Result of urlopen() is not seekable, but ZipFile requires a
    # seekable file.  Work around this by copying the file into a
    # memory stream.
    with io.BytesIO() as memio:
        shutil.copyfileobj(f, memio)
        zip = zipfile.ZipFile(file=memio)
        # Can't use zip.extract(), because I want to ignore paths
        # within archive.
        src = zip.open('unknowndir/src.txt')
        with open('dst.txt', mode='wb') as dst:
            shutil.copyfileobj(src, dst)

The last line throws an Error:

Traceback (most recent call last):
  File "test.py", line 25, in <module>
    shutil.copyfileobj(src, dst)
  File "C:\Python26\lib\shutil.py", line 27, in copyfileobj
    buf = fsrc.read(length)
  File "C:\Python26\lib\zipfile.py", line 594, in read
    bytes = self.fileobj.read(bytesToRead)
TypeError: integer argument expected, got 'long'


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gabriel Genellina  
View profile  
 More options Oct 24 2009, 1:49 am
Newsgroups: comp.lang.python
From: "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
Date: Sat, 24 Oct 2009 02:49:28 -0300
Local: Sat, Oct 24 2009 1:49 am
Subject: Re: Copying a ZipExtFile
En Fri, 23 Oct 2009 14:15:33 -0300, Moore, Mathew L <Moor...@battelle.org>  
escribió:

Try adding a length parameter to the copyfileobj call, so the copy is done  
in small enough chunks.

--
Gabriel Genellina


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Moore, Mathew L  
View profile  
 More options Oct 26 2009, 10:36 am
Newsgroups: comp.lang.python
From: "Moore, Mathew L" <Moor...@BATTELLE.ORG>
Date: Mon, 26 Oct 2009 10:36:14 -0400
Local: Mon, Oct 26 2009 10:36 am
Subject: RE: Copying a ZipExtFile

Hmmm...tried a variety of lengths (512, 1024, etc.) with no luck.  Maybe this is a good opportunity for me to learn some Python debugging tools.

Thanks!
--Matt


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ryles  
View profile  
 More options Oct 28 2009, 8:33 pm
Newsgroups: comp.lang.python
From: ryles <ryle...@gmail.com>
Date: Wed, 28 Oct 2009 17:33:10 -0700 (PDT)
Local: Wed, Oct 28 2009 8:33 pm
Subject: Re: Copying a ZipExtFile
On Oct 23, 1:15 pm, "Moore, Mathew L" <Moor...@BATTELLE.ORG> wrote:

It should hopefully work if you use cStringIO/StringIO instead of
BytesIO.

I think the issue is essentially that StringIO.read() will accept a
long object while the backport of bytesio to to 2.6 does an explicit
check for int:

py> StringIO.StringIO("foo").read(long(1))
'f'

py> io.BytesIO("foo").read(long(1))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: integer argument expected, got 'long'

Should this be amended? Perhaps someone on core can consider it.

As for why the bytesToRead calculation in ZipExtFile.read() results in
a long, I've not yet looked at it closely.


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
ryles  
View profile  
 More options Oct 29 2009, 2:27 am
Newsgroups: comp.lang.python
From: ryles <ryle...@gmail.com>
Date: Wed, 28 Oct 2009 23:27:18 -0700 (PDT)
Local: Thurs, Oct 29 2009 2:27 am
Subject: Re: Copying a ZipExtFile
On Oct 28, 8:33 pm, ryles <ryle...@gmail.com> wrote:

> As for why the bytesToRead calculation in ZipExtFile.read() results in
> a long, I've not yet looked at it closely.

Simple, actually:

In ZipExtFile.__init__():

    self.bytes_read = 0L

In ZipExitFile.read():

    bytesToRead = self.compress_size - self.bytes_read

13 - 0L == 13L


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Moore, Mathew L  
View profile  
 More options Oct 29 2009, 9:54 am
Newsgroups: comp.lang.python
From: "Moore, Mathew L" <Moor...@BATTELLE.ORG>
Date: Thu, 29 Oct 2009 09:54:20 -0400
Local: Thurs, Oct 29 2009 9:54 am
Subject: RE: Copying a ZipExtFile

It does! Excellent!  You've saved me the trouble of a weekend debug session.

--Matt


    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2010 Google