This example describes the Python zip and unzip data methods. Share to everyone for your reference. The implementation method is as follows:
# Zipping and unzipping a string using the zlib module# a very large string could be zipped and saved to a file speeding U P File Writing time # and later reloaded and unzipped by another program speeding up reading of the file# tested with Pyth ON24 vegaseat 15aug2005import zlibstr1 = \ "" "Dallas Cowboys football practice at Valley Ranch is delayed on Wednesday For nearly the hours. One of the players, while in his-to-lockerroom happened to look-down and notice a suspicious looking, unknown whit Epowdery substance on the practice field. The coaching staff immediately suspended practice and the FBI wascalled in to investigate. After a all field analysis, the fbidetermined the substance unknown to the players is the goalline. Practice was resumed when FBI special Agents decided that the team would notbe likely to encounter the substance again. "" "Print '-' *70 # dashes for the fun of Itprint str1print '-' *70crc_check1 = ZLIB.CRC32 (str1) print" CRC before Zip= ", Crc_check1print "Length of original str1 =", Len (str1) # Zip compress the stringzstr1 = zlib.compress (str1) print "Length o F zipped str1 = ", Len (zstr1) filename = ' Dallas.zap ' # write the zipped string to a filefout = open (filename, ' W ') Try:prin T >> fout, zstr1except ioerror:print "Failed to open File ..." else:print "Done Writing", Filenamefout.close () # RE Ad the zip file Backfin = open (filename, ' r ') try:zstr2 = Fin.read () except Ioerror:print "Failed to open File ..." Else: Print "Done reading", Filenamefin.close () # Unzip the zipped string from the FILESTR2 = zlib.decompress (zstr2) print '-' *70 Print Str2print '-' *70crc_check2 = ZLIB.CRC32 (str2) print "CRC after Unzip =", Crc_check2, "(check sums should match)
Hopefully this article will help you with Python programming.