how to zip a StringIO object?

Discussion in 'Python' started by Nagy Viktor, Oct 28, 2009.

  1. Nagy Viktor

    Nagy Viktor Guest

    Hi,

    I try to run the following code:

    def generate_zip(object_list, template):
    result = StringIO.StringIO()
    zipped = zipfile.ZipFile(result, "w")
    for object in object_list:
    pdf = generate_pdf(object, template)
    if not pdf:
    raise IOError("Problem with generating invoice %d" %
    object.pk)
    zipped.writestr("invoice-%d.pdf" % object.pk, pdf)
    zipped.close()
    return result.getvalue()

    where generate_pdf returns a valid pdf file's string content, more
    precisely it returns a StringIO.getvalue() call containing the pdf
    "string".

    When I add this string to the zip file, I get a problem. Namely, the
    zipped files are chmoded to non-readable when extracted. How can I
    change this?

    thanks,
    V
    Nagy Viktor, Oct 28, 2009
    #1
    1. Advertising

  2. Nagy Viktor

    Dave Angel Guest

    Nagy Viktor wrote:
    > Hi,
    >
    > I try to run the following code:
    >
    > def generate_zip(object_list, template):
    > result = StringIO.StringIO()
    > zipped = zipfile.ZipFile(result, "w")
    > for object in object_list:
    > pdf = generate_pdf(object, template)
    > if not pdf:
    > raise IOError("Problem with generating invoice %d" %
    > object.pk)
    > zipped.writestr("invoice-%d.pdf" % object.pk, pdf)
    > zipped.close()
    > return result.getvalue()
    >
    > where generate_pdf returns a valid pdf file's string content, more
    > precisely it returns a StringIO.getvalue() call containing the pdf
    > "string".
    >
    > When I add this string to the zip file, I get a problem. Namely, the
    > zipped files are chmoded to non-readable when extracted. How can I
    > change this?
    >
    > thanks,
    > V
    >
    >

    You don't tell us the version of Python, nor the OS environment, nor
    what utility you're using to extract these files.

    When I supply the missing imports, shortcircuit the missing classes and
    functions, and add some mainline code, I can run this with no problem.
    Are you also running on Windows XP, using 2.6.2, using the same missing
    code as I, and extracting with Winzip 11.2?

    DaveA
    Dave Angel, Oct 28, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Skip Montanaro
    Replies:
    0
    Views:
    144
    Skip Montanaro
    May 30, 2013
  2. Cameron Simpson
    Replies:
    0
    Views:
    87
    Cameron Simpson
    May 31, 2013
  3. Göktuğ Kayaalp
    Replies:
    0
    Views:
    108
    Göktuğ Kayaalp
    May 31, 2013
  4. Skip Montanaro
    Replies:
    0
    Views:
    106
    Skip Montanaro
    May 31, 2013
  5. Serhiy Storchaka
    Replies:
    0
    Views:
    76
    Serhiy Storchaka
    May 31, 2013
Loading...

Share This Page