Methods of zip and unzip data in Python

Source: Internet
Author: User
Tags crc32
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.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.