Grupos de Google ya no admite publicaciones ni suscripciones nuevas de Usenet. El contenido anterior sigue visible.

zlib and gzip

9 vistas
Ir al primer mensaje no leído

Greg Bakken

no leída,
12 may 2004, 12:56:38 p.m.12/5/2004
para
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

Hornberger, Chris

no leída,
12 may 2004, 1:03:35 p.m.12/5/2004
para Greg Bakken,pytho...@python.org
Silly questions first: Is it just a matter of 72 column-bounding to make it email-compliant? (keep in mind I haven't run your sample code yet, I'm sitting here perusing while on a conference call - meetings suck!!!)

--------------------------
Chris Hornberger
Blackrock - 302.797.2318
chris.ho...@blackrock.com

Card carrying MSDN member since 2004.
No, really. I've got the card to prove it.

Greg
--
http://mail.python.org/mailman/listinfo/python-list

Hornberger, Chris

no leída,
12 may 2004, 1:18:17 p.m.12/5/2004
para pytho...@python.org
I got rid of the offsets for a quick test (see code)

import base64, zlib

def getString(s):
s = base64.decodestring(s)

s = zlib.decompress(s ) #, -15)
return s

def makeString(s):
s = zlib.compress(s ) #, 9)


s = base64.encodestring(s)
return s

initialdata = "this is a test"
print "1)" + initialdata
compresseddata = makeString( initialdata )
print "2)" + compresseddata
uncompressed = getString( compresseddata )
print "3)" + uncompressed

and got the following results:

C:\>comp.py
1)this is a test
2)eJwrycgsVgCiRIWS1OISACYzBRY=

3)this is a test

C:\>

--------------------------
Chris Hornberger
Blackrock - 302.797.2318
chris.ho...@blackrock.com

Card carrying MSDN member since 2004.
No, really. I've got the card to prove it.


-----Original Message-----
From: python-list-bounces+chris.hornberger=blackr...@python.org
[mailto:python-list-bounces+chris.hornberger=blackr...@python.org]On

Behalf Of Hornberger, Chris
Sent: Wednesday, May 12, 2004 1:14 PM
To: Bakken, Gregory A; pytho...@python.org
Subject: RE: zlib and gzip


Ok, fair enough. Can you supply (if it's non-sensitive information) some samples?

I'm on this conference call for the next hour or so, so I'm glued to my desk. I have time to tinker. It's just a general status call. I just have to chime in from time to time.

--------------------------
Chris Hornberger
Blackrock - 302.797.2318
chris.ho...@blackrock.com

Card carrying MSDN member since 2004.
No, really. I've got the card to prove it.


-----Original Message-----
From: Bakken, Gregory A [mailto:gregory_...@groton.pfizer.com]
Sent: Wednesday, May 12, 2004 1:10 PM
To: Hornberger, Chris; pytho...@python.org
Subject: RE: zlib and gzip


The short answer is I don't know. The longer answer is that I really don't
know the driving force behind the compression/encoding that is being used.
What I am doing is a more downstream process (so I am stuck with the chosen
scheme).

Greg
--
http://mail.python.org/mailman/listinfo/python-list


LEGAL NOTICE
Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. Access to this E-mail by anyone else is unauthorized. If you are not an addressee, any disclosure or copying of the contents of this E-mail or any action taken (or not taken) in reliance on it is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately.

--
http://mail.python.org/mailman/listinfo/python-list

Bakken, Gregory A

no leída,
12 may 2004, 1:10:11 p.m.12/5/2004
para Hornberger, Chris,pytho...@python.org

Hornberger, Chris

no leída,
12 may 2004, 1:14:04 p.m.12/5/2004
para Bakken, Gregory A,pytho...@python.org
Ok, fair enough. Can you supply (if it's non-sensitive information) some samples?

I'm on this conference call for the next hour or so, so I'm glued to my desk. I have time to tinker. It's just a general status call. I just have to chime in from time to time.

--------------------------


Chris Hornberger
Blackrock - 302.797.2318
chris.ho...@blackrock.com

Card carrying MSDN member since 2004.
No, really. I've got the card to prove it.


-----Original Message-----
From: Bakken, Gregory A [mailto:gregory_...@groton.pfizer.com]
Sent: Wednesday, May 12, 2004 1:10 PM

Bakken, Gregory A

no leída,
12 may 2004, 1:39:04 p.m.12/5/2004
para Hornberger, Chris,pytho...@python.org
Below is a very simple sample, but I think it illustrates the problem. In
short, I have a text string in readable format, and compressed/encoded by
the other app. My getString function below can recover the original string
from what is provided by the other app. If I take the original string and
try to compress/encode and then recover it, both approaches I have tried
fail. (In other words, the script below will actually crash with an error
something like zlib.error: Error -3 while decompressing data: invalid block
type

import gzip, zlib, StringIO, base64

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

def makeString1(s):


s = zlib.compress(s, 9)
s = base64.encodestring(s)
return s

def makeString2(s):


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

previouslyMadeString = "81RIzFVIVChKTUxJTMpJVSguKcrMSwcA"
readableString = "I am a readable string"

print "Original string"
print readableString
print ""
print "Decoded string form other program"
print getString(previouslyMadeString)
print ""

ms1 = makeString1(readableString)
gs1 = getString(ms1)
print "Decoded from appraoch 1"
print gs1

ms2 = makeString2(readableString)
gs2 = getString(ms2)
print "Decoded from appraoch 1"
print gs2


-----Original Message-----
From: Hornberger, Chris [mailto:Chris.Ho...@blackrock.com]

Sent: Wednesday, May 12, 2004 1:14 PM

0 mensajes nuevos