How to Use Python to compress and decompress zip files

Source: Internet
Author: User
Tags decompress zip

How to Use Python to compress and decompress zip files

This article mainly introduces how to use Python to compress and decompress zip files. It mainly uses the zipfile package. For more information, see

Python zipfile provides a very convenient way to compress and decompress zip files.

For example, in the directory where The py script is located, there are the following files:

The Code is as follows:

Readability/readability. js

Readability/readability.txt

Read/readability-print.css

Read/sprite-readability.png

Readability/readability.css

Compress the files in the readability directory to the readability.zip file in the script directory, maintain the same file structure, and then print the file list of the generated compressed package, decompress the file to the output directory and output/bak directory of the script directory in two ways.

The script 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

#! /Usr/vin/env python

# Coding: UTF-8

"""

Compress and decompress zip files

"""

 

Import OS

Import zipfile

 

Def compress (zip_file, input_dir ):

F_zip = zipfile. ZipFile (zip_file, 'w ')

For root, dirs, files in OS. walk (input_dir ):

For f in files:

# Obtain the relative file path and create the same directory structure in the compressed package

Abs_path = OS. path. join (OS. path. join (root, f ))

Rel_path = OS. path. relpath (abs_path, OS. path. dirname (input_dir ))

F_zip.write (abs_path, rel_path, zipfile. ZIP_STORED)

 

Def extract (zip_file, output_dir ):

F_zip = zipfile. ZipFile (zip_file, 'R ')

 

# Decompress all files to the specified directory

F_zip.extractall (output_dir)

 

# Decompress files one by one to the specified directory

For f in f_zip.namelist ():

F_zip.extract (f, OS. path. join (output_dir, 'bak '))

 

Def printdir (zip_file ):

F_zip = zipfile. ZipFile (zip_file, 'R ')

Print '= printdir () ========================='

F_zip.printdir ()

Print

Print '= namelist () ========================='

For f in f_zip.namelist ():

Print f

 

If _ name _ = '_ main __':

Zip_file = 'readability.zip'

Compress (zip_file, OS. path. join (OS. getcwd (), 'readability '))

Printdirzip_file)

Extract (zip_file, 'output') </pre>

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.