zlib and gzip

Discussion in 'Python' started by Greg Bakken, May 12, 2004.

  1. Greg Bakken

    Greg Bakken Guest

    I am writing a python program that recieves 'compressed and encoded'
    strings from another piece of software. I can successfully uncompress
    and decode the strings via

    import base64, zlib
    def getString(s):
    s = base64.decodestring(s)
    s = zlib.decompress(s, -15)
    return s

    Now, I want to take a 'readable' text string, and compress and encode
    it the same way as the other piece of software. I first tried the
    seemingly obvious (but incorrect)

    import base64, zlib
    def makeString(s):
    s = zlib.compress(s, 9)
    s = base64.encodestring(s)
    return s

    I have also tried using the gzip module to do this like

    import base64, gzip, StringIO
    def makeString(s):
    sio = StringIO.StringIO()
    gzipper = gzip.GzipFile(mode="wb", fileobj=sio)
    gzipper.write(s)
    s = base64.encodestring(sio.getvalue())
    return s

    What I would like to be able to do is take a string s, pass it through
    the makeString function, and pass the result through the getString
    function, and end up with the original string s back. I have to stick
    with the way getString is, so I can handle strings from another
    program, so I need to adapt makeString accordingly, but cannot figure
    out how.

    Greg
     
    Greg Bakken, May 12, 2004
    #1
    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. Bakken, Gregory A

    RE: zlib and gzip

    Bakken, Gregory A, May 12, 2004, in forum: Python
    Replies:
    0
    Views:
    783
    Bakken, Gregory A
    May 12, 2004
  2. flebber

    Gzip - gunzip using zlib

    flebber, Jun 9, 2007, in forum: Python
    Replies:
    2
    Views:
    380
    Stefan Behnel
    Jun 9, 2007
  3. Rémi Gagnon
    Replies:
    2
    Views:
    396
    Michel Brito
    Apr 23, 2009
  4. Piotr MÄ…sior

    Zlib gzip Iconv, what is going on with UTF-8

    Piotr MÄ…sior, Feb 1, 2010, in forum: Ruby
    Replies:
    2
    Views:
    156
    Piotr MÄ…sior
    Feb 1, 2010
  5. Atoli Atoli

    open-uri + Zlib: not in gzip format

    Atoli Atoli, Oct 23, 2010, in forum: Ruby
    Replies:
    0
    Views:
    266
    Atoli Atoli
    Oct 23, 2010
Loading...

Share This Page