Python compression and decompression zip file _python

Source: Internet
Author: User
Tags zip

The zip file is one of the packaging formats that we often use, and it is efficient to extract and compress the python zip. Python Unzip zip document:

Copy Code code as follows:

#/usr/bin/python
#coding =utf-8

Import Os,sys,time
Import ZipFile

filename = ' Callofdutyblackopszombies_1349649132343_my.zip ' #要解压的文件
Filedir = ' data/' #解压后放入的目录
r = zipfile.is_zipfile (filename)
If R:
StartTime = Time.time ()
FZ = ZipFile. ZipFile (filename, ' R ')
For file in Fz.namelist ():
Print (file) #打印zip归档中目录
Fz.extract (File,filedir)
Endtime = Time.time ()
Times = Endtime-starttime
Else
Print (' This file was not zip file ')
Print (' Times ' + str)


The Python compressed folder is zip
Copy Code code as follows:

#/usr/bin/python
#coding =utf-8


Import OS
Import ZipFile
Import Sys

Try
Import zlib
Compression = ZipFile. zip_deflated
Except
Compression = ZipFile. Zip_stored

Path = ' data/' #要进行压缩的文档目录
Start = Path.rfind (os.sep) + 1
filename = ' Callofdutyblackopszombies_1349649132343_my.zip ' #压缩后的文件名

z = zipfile. ZipFile (Filename,mode = "W", compression = compression)
Try
For dirpath,dirs,files in Os.walk (path):
For file in Files:
if file = = FileName or file = = "zip.py":
Continue
Print (file)
Z_path = Os.path.join (dirpath,file)
Z.write (Z_path,z_path[start:])
Z.close ()
Except
If z:
Z.close ()

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.