The examples in this article describe the Python zip and unzip data methods. Share to everyone for your reference. The implementation methods are as follows:
?
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 45 46 47 48 49 50 51 52 |
# zipping and unzipping a string using the Zlib module # A very large string could be Z Ipped and saved to a file speeding the up file writing time # and later reloaded with unzipped up R Eading of the file # tested with Python24 vegaseat 15aug2005 import zlib str1 = "" Dallas Cowboys football practice at Val Ley Ranch is delayed on Wednesday for nearly two hours. One of the players, while on the he way to the locker room happened to look down and notice a suspicious looking, unknown whi Te powdery substance on the practice field. The coaching staff immediately suspended practice while the FBI is called in to investigate. After a complete field analysis, the FBI determined, the "white substance" unknown to the players is the goal line. Practice was resumed the FBI Special Agents decided the "team would the" not being likely to encounter the substance again. "" "Print"-' *70 # dashes for the fun of it print str1 print '-' *70 crc_chEck1 = ZLIB.CRC32 (str1) print "CRC before Zip=", crc_check1 print "Length of original str1 =", Len (str1) # zip compress th E string zstr1 = Zlib.compress (str1) print "Length of zipped str1 =", Len (zstr1) filename = ' Dallas.zap ' # write the Zippe D string to a file Fout = open (filename, ' W ') Try:print >> fout, zstr1 except Ioerror:print "Failed to open file. . "Else:print writing", FileName fout.close () # Read the zip file back fin = open (filename, ' r ') try:zstr2 = FIN.R EAD () except Ioerror:print "Failed to open File ..." else:print "done reading", filename fin.close () # Unzip the zipped s Tring from the file str2 = zlib.decompress (zstr2) print '-' *70 print str2 print '-' *70 Crc_check2 = ZLIB.CRC32 (str2) print "CRC after Unzip =", Crc_check2, "(check sums should match)" |
I hope this article will help you with your Python programming.