Zip and unzip data in python

Source: Internet
Author: User
Tags crc32

Zip and unzip data in python

This document describes how to use python zip and unzip data. Share it with you for your reference. The specific implementation method is 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

43

44

45

46

47

48

49

50

51

52

# Zipping and unzipping a string using the zlib module

# A very large string cocould be zipped and saved to a file speeding up file writing time

# And later reloaded and unzipped by another program speeding up reading of the file

# Tested with Python24 vegaseat 15aug2005

Import zlib

Str1 =

"Dallas Cowboys football practice at Valley Ranch was delayed on Wednesday

For nearly two hours. One of the players, while on his way to the locker

Room happened to look down and notice a suspicious looking, unknown white

Powdery substance on the practice field.

The coaching staff immediately susponded practice while the FBI was

Called in to investigate. After a complete field analysis, the FBI

Determined that the white substance unknown to the players was the goal

Line.

Practice was resumed when FBI Special Agents decided that the team wocould not

Be likely to encounter the substance again.

"""

Print '-' * 70 #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 the string

Zstr1 = zlib. compress (str1)

Print "Length of zipped str1 =", len (zstr1)

Filename = 'Dallas. zap'

# Write the zipped string to a file

Fout = open (filename, 'w ')

Try:

Print> fout, zstr1

Handle t IOError:

Print "Failed to open file ..."

Else:

Print "done writing", filename

Fout. close ()

# Read the zip file back

Fin = open (filename, 'R ')

Try:

Zstr2 = fin. read ()

Handle t IOError:

Print "Failed to open file ..."

Else:

Print "done reading", filename

Fin. close ()

# Unzip the zipped string 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 shocould match )"

I hope this article will help you with Python programming.

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.