python是如何壓縮zip文件的-創(chuàng)新互聯(lián)

創(chuàng)新互聯(lián)www.cdcxhl.cn八線動(dòng)態(tài)BGP香港云服務(wù)器提供商,新人活動(dòng)買多久送多久,劃算不套路!

10年積累的網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站設(shè)計(jì)后付款的網(wǎng)站建設(shè)流程,更有乾安免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

小編給大家分享一下python是如何壓縮zip文件的,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

zipfile zip文件操作

引入模塊:

import zipfile

zip文件格式是通用的文檔壓縮標(biāo)準(zhǔn),在ziplib模塊中,使用ZipFile類來操作zip文件,下面具體介紹一下:

zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])

功能:創(chuàng)建一個(gè)ZipFile對(duì)象,表示一個(gè)zip文件。

參數(shù):

-參數(shù)file表示文件的路徑或類文件對(duì)象(file-like object)。

-參數(shù)mode指示打開zip文件的模式,默認(rèn)值為r。

r    表示讀已經(jīng)存在的zip文件。

w   表示新建一個(gè)zip文檔或覆蓋一個(gè)已經(jīng)存在的zip文檔。

a    表示將數(shù)據(jù)附加到一個(gè)現(xiàn)存的zip文檔中。

-參數(shù)compression表示在寫zip文檔時(shí)使用的壓縮方法。

zipfile.ZIP_STORED      只是存儲(chǔ)模式,不會(huì)對(duì)文件進(jìn)行壓縮,這個(gè)是默認(rèn)值

zipfile.ZIP_DEFLATED    對(duì)文件進(jìn)行壓縮

-如果要操作的zip文件大小超過2G,應(yīng)該將allowZip64設(shè)置為True。

ZipFile還提供了如下常用的方法和屬性:

ZipFile.getinfo(name)

功能:獲取zip文檔內(nèi)指定文件的信息。返回一個(gè)zipfile.ZipInfo對(duì)象,它包括文件的詳細(xì)信息。將在下面 具體介紹該對(duì)象。

ZipFile.infolist()

功能:獲取zip文檔內(nèi)所有文件的信息,返回一個(gè)zipfile.ZipInfo的列表。

ZipFile.namelist()

功能:獲取zip文檔內(nèi)所有文件的名稱列表。

ZipFile.extract(member[, path[, pwd]])

功能:將zip文檔內(nèi)的指定文件解壓到當(dāng)前目錄。

參數(shù):

member      指定要解壓的文件名稱或?qū)?yīng)的ZipInfo對(duì)象

path        指定解析文件保存的文件夾

pwd         解壓密碼

下面一個(gè)例子將保存在程序根目錄下的txt.zip內(nèi)的所有文件解壓到D:/Work目錄:

import zipfile, os
zipFile = zipfile.ZipFile(os.path.join(os.getcwd(), 'txt.zip'))
for file in zipFile.namelist():
    zipFile.extract(file, r'd:/Work')
zipFile.close()
import zipfile, os
zipFile = zipfile.ZipFile(os.path.join(os.getcwd(), 'txt.zip'))
for file in zipFile.namelist():
    zipFile.extract(file, r'd:/Work')
zipFile.close()

ZipFile.extractall([path[, members[, pwd]]])

功能:解壓zip文檔中的所有文件到當(dāng)前目錄。

參數(shù):

members      默認(rèn)值為zip文檔內(nèi)的所有文件名稱列表,也可以自己設(shè)置,選擇要解壓的文件名稱。

ZipFile.printdir()

功能:將zip文檔內(nèi)的信息打印到控制臺(tái)上。

ZipFile.setpassword(pwd)

功能:設(shè)置zip文檔的密碼。

ZipFile.read(name[, pwd])

功能:獲取zip文檔內(nèi)指定文件的二進(jìn)制數(shù)據(jù)。

下面的例子演示了read()的使用,zip文檔內(nèi)包括一個(gè)txt.txt的文本文件,使用read()方法讀取其二進(jìn)制數(shù)據(jù),然后保存到D:/txt.txt。

import zipfile, os
zipFile = zipfile.ZipFile(os.path.join(os.getcwd(), 'txt.zip'))
data = zipFile.read('txt.txt')
#一行語句就完成了寫文件操作。
(lambda f, d: (f.write(d), f.close()))(open(r'd:/txt.txt', 'wb'), data)
zipFile.close()

ZipFile.write(filename[, arcname[, compress_type]])

功能:將指定文件添加到zip文檔中。

參數(shù):

filename      文件路徑

arcname       添加到zip文檔之后保存的名稱

compress_type 壓縮方法,它的值可以是zipfile.ZIP_STORED 或zipfile.ZIP_DEFLATED

ZipFile.writestr(zinfo_or_arcname, bytes)

功能:writestr()支持將二進(jìn)制數(shù)據(jù)直接寫入到壓縮文檔。

ZipFile.getinfo(name)

功能:返回一個(gè)ZipInfo對(duì)象,表示zip文檔中相應(yīng)文件的信息。

它支持如下屬性:

ZipInfo.filename        獲取文件名稱。

ZipInfo.date_time       獲取文件最后修改時(shí)間。返回一個(gè)包含6個(gè)元素的元組:(年, 月, 日, 時(shí), 分, 秒)

ZipInfo.compress_type   壓縮類型。

ZipInfo.comment         文檔說明。

ZipInfo.extr            擴(kuò)展項(xiàng)數(shù)據(jù)。

ZipInfo.create_system   獲取創(chuàng)建該zip文檔的系統(tǒng)。

ZipInfo.create_version  獲取、創(chuàng)建zip文檔的PKZIP版本。

ZipInfo.extract_versio  獲取、解壓zip文檔所需的PKZIP版本。

ZipInfo.reserved        預(yù)留字段,當(dāng)前實(shí)現(xiàn)總是返回0。

ZipInfo.flag_bits       zip標(biāo)志位。

ZipInfo.volume          文件頭的卷標(biāo)。

ZipInfo.internal_attr   內(nèi)部屬性。

ZipInfo.external_attr   外部屬性。

ZipInfo.header_offset   文件頭偏移位。

ZipInfo.CRC             未壓縮文件的CRC-32。

ZipInfo.compress_size   獲取壓縮后的大小。

ZipInfo.file_size       獲取未壓縮的文件大小。

下面一個(gè)簡(jiǎn)單的例子說明這些屬性的意思:

import zipfile, os
zipFile = zipfile.ZipFile(os.path.join(os.getcwd(), 'txt.zip'))
zipInfo = zipFile.getinfo('doc.doc')
print 'filename:', zipInfo.filename
print 'date_time:', zipInfo.date_time
print 'compress_type:', zipInfo.compress_type
print 'comment:', zipInfo.comment
print 'extra:', zipInfo.extra
print 'create_system:', zipInfo.create_system
print 'create_version:', zipInfo.create_version
print 'extract_version:', zipInfo.extract_version
print 'extract_version:', zipInfo.reserved
print 'flag_bits:', zipInfo.flag_bits
print 'volume:', zipInfo.volume
print 'internal_attr:', zipInfo.internal_attr
print 'external_attr:', zipInfo.external_attr
print 'header_offset:', zipInfo.header_offset
print 'CRC:', zipInfo.CRC
print 'compress_size:', zipInfo.compress_size
print 'file_size:', zipInfo.file_size
zipFile.close()

看完了這篇文章,相信你對(duì)python是如何壓縮zip文件的有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司行業(yè)資訊頻道,感謝各位的閱讀!

文章題目:python是如何壓縮zip文件的-創(chuàng)新互聯(lián)
標(biāo)題來源:http://www.bm7419.com/article28/dgcjjp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站、App開發(fā)、網(wǎng)站導(dǎo)航、外貿(mào)建站企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)