Welcome, guest | Sign In | My Account | Store | Cart

This receipe arose when I wanted to add a larger blob (some autocad data) to a xml document. As I did not want to get a large xml document and as I wanted to avoid CDATA sections I wrote the functions below.

Python, 46 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import zlib, base64

def asciiCompress(data, level=9):
    """ compress data to printable ascii-code """

    code = zlib.compress(data,level)
    csum = zlib.crc32(code)
    code = base64.encodestring(code)
    return code, csum

def asciiDecompress(code):
    """ decompress result of asciiCompress """

    code = base64.decodestring(code)
    csum = zlib.crc32(code)
    data = zlib.decompress(code)
    return data, csum

if __name__ == "__main__":

    print
    print "TEST SELF COMPRESSION"

  
    text = file(__file__, "r").read()

    data = text+text+text

    print
    print "size uncompressed ............. :", len(data)

    code, csum1 = asciiCompress(data)

    print
    print "compressed data looks like this :", code[:35]
    print
    print "size compressed ............... :", len(code)
    print "compression effect ............ : %.1f %%" % (len(code)*100.0/len(data))
    print 

    datanew, csum2 = asciiDecompress(code)

    if datanew == data and csum1 == csum2:
        print "compress + decompress succeeded"
    else:
        print "compress + decompress failed"
    

Using base64.encodestring weakens the effect or zlib.compress, but I still get a good compression ratio in my application. I think the code is self explaining. So I have nothing to say here...

Created by Uwe Schmitt on Fri, 3 Dec 2004 (PSF)
Python recipes (4591)
Uwe Schmitt's recipes (4)

Required Modules

Other Information and Tasks