#!/usr/bin/env python#-*-coding:utf-8-*- fromZipFileImport*ImportZipFile#Unzip the zip filedefunzip (): Source_zip="C:\\update\\sw_servers_20120815.zip"Target_dir="c:\\update\\"Myzip=ZipFile (source_zip) myfilelist=myzip.namelist () forNameinchMyfilelist:f_handle=open (Target_dir+name,"WB") F_handle.write (myzip.read (name)) F_handle.close () myzip.close ()#add a file to an existing ZIP packagedefAddzip (): F= ZipFile. ZipFile ('Archive.zip','W', ZipFile. zip_deflated) F.write ('file_to_add.py') F.close ()#package The files in the entire folderdefAdddirfile (): F= ZipFile. ZipFile ('Archive.zip','W', ZipFile. zip_deflated) Startdir="c:\\mydirectory" forDirpath, Dirnames, filenamesinchOs.walk (startdir): forFileNameinchFilenames:f.write (Os.path.join (dirpath,filename)) F.close ()
Python uses the ZipFile module to package files or directories, unzip the zip file instance