国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python中的zipfile模塊使用詳解

2019-11-25 17:15:54
字體:
來源:轉載
供稿:網友

zip文件格式是通用的文檔壓縮標準,在ziplib模塊中,使用ZipFile類來操作zip文件,下面具體介紹一下:
class zipfile.ZipFile(file[, mode[, compression[, allowZip64]]])

創建一個ZipFile對象,表示一個zip文件。參數file表示文件的路徑或類文件對象(file-like object);參數mode指示打開zip文件的模式,默認值為'r',表示讀已經存在的zip文件,也可以為'w'或'a','w'表示新建一個zip文檔或覆蓋一個已經存在的zip文檔,'a'表示將數據附加到一個現存的zip文檔中。參數compression表示在寫zip文檔時使用的壓縮方法,它的值可以是zipfile. ZIP_STORED 或zipfile. ZIP_DEFLATED。如果要操作的zip文件大小超過2G,應該將allowZip64設置為True。

ZipFile還提供了如下常用的方法和屬性:
ZipFile.getinfo(name):

獲取zip文檔內指定文件的信息。返回一個zipfile.ZipInfo對象,它包括文件的詳細信息。將在下面 具體介紹該對象。
ZipFile.infolist()

獲取zip文檔內所有文件的信息,返回一個zipfile.ZipInfo的列表。
ZipFile.namelist()

獲取zip文檔內所有文件的名稱列表。
ZipFile.extract(member[, path[, pwd]])

將zip文檔內的指定文件解壓到當前目錄。參數member指定要解壓的文件名稱或對應的ZipInfo對象;參數path指定了解析文件保存的文件夾;參數pwd為解壓密碼。下面一個例子將保存在程序根目錄下的txt.zip內的所有文件解壓到D:/Work目錄:
 

import zipfile, oszipFile = 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文檔中的所有文件到當前目錄。參數members的默認值為zip文檔內的所有文件名稱列表,也可以自己設置,選擇要解壓的文件名稱。
ZipFile.printdir()

將zip文檔內的信息打印到控制臺上。
ZipFile.setpassword(pwd)

設置zip文檔的密碼。
ZipFile.read(name[, pwd])

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

#coding=gbkimport zipfile, oszipFile = 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文檔中。filename為文件路徑,arcname為添加到zip文檔之后保存的名稱, 參數compress_type表示壓縮方法,它的值可以是zipfile. ZIP_STORED 或zipfile. ZIP_DEFLATED。下面的例子演示了如何創建一個zip文檔,并將文件D:/test.doc添加到壓縮文檔中。
 

import zipfile, oszipFile = zipfile.ZipFile(r'D:/test.zip'), 'w')zipFile.write(r'D:/test.doc', 'ok.doc', zipfile.ZIP_DEFLATED)zipFile.close()ZipFile.writestr(zinfo_or_arcname, bytes)

writestr()支持將二進制數據直接寫入到壓縮文檔。
Class ZipInfo

ZipFile.getinfo(name) 方法返回的是一個ZipInfo對象,表示zip文檔中相應文件的信息。它支持如下屬性:

  • ZipInfo.filename: 獲取文件名稱。
  • ZipInfo.date_time: 獲取文件最后修改時間。返回一個包含6個元素的元組:(年, 月, 日, 時, 分, 秒)
  • ZipInfo.compress_type: 壓縮類型。
  • ZipInfo.comment: 文檔說明。
  • ZipInfo.extr: 擴展項數據。
  • ZipInfo.create_system: 獲取創建該zip文檔的系統。
  • ZipInfo.create_version: 獲取 創建zip文檔的PKZIP版本。
  • ZipInfo.extract_version: 獲取 解壓zip文檔所需的PKZIP版本。
  • ZipInfo.reserved: 預留字段,當前實現總是返回0。
  • ZipInfo.flag_bits: zip標志位。
  • ZipInfo.volume: 文件頭的卷標。
  • ZipInfo.internal_attr: 內部屬性。
  • ZipInfo.external_attr: 外部屬性。
  • ZipInfo.header_offset: 文件頭偏移位。
  • ZipInfo.CRC: 未壓縮文件的CRC-32。
  • ZipInfo.compress_size: 獲取壓縮后的大小。
  • ZipInfo.file_size: 獲取未壓縮的文件大小。

下面一個簡單的例子說明這些屬性的意思:
 

import zipfile, oszipFile = zipfile.ZipFile(os.path.join(os.getcwd(), 'txt.zip'))zipInfo = zipFile.getinfo('doc.doc')print 'filename:', zipInfo.filenameprint 'date_time:', zipInfo.date_timeprint 'compress_type:', zipInfo.compress_typeprint 'comment:', zipInfo.commentprint 'extra:', zipInfo.extraprint 'create_system:', zipInfo.create_systemprint 'create_version:', zipInfo.create_versionprint 'extract_version:', zipInfo.extract_versionprint 'extract_version:', zipInfo.reservedprint 'flag_bits:', zipInfo.flag_bitsprint 'volume:', zipInfo.volumeprint 'internal_attr:', zipInfo.internal_attrprint 'external_attr:', zipInfo.external_attrprint 'header_offset:', zipInfo.header_offsetprint 'CRC:', zipInfo.CRCprint 'compress_size:', zipInfo.compress_sizeprint 'file_size:', zipInfo.file_sizezipFile.close()

感覺使用zipfile模塊來處理zip文件真的很簡單。想當初在.NET平臺下,使用sharpziplib壓縮、解壓一個文件,我花了N多時間,找了N多英文資源,才寫出一個能壓縮文件的demo。而現在使用Python,通過閱讀python手冊,一兩個小時就掌握了zipfile模塊的基本使用。哈哈,使用Python,真爽!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 镇雄县| 绥江县| 呈贡县| 兴文县| 屯昌县| 铁力市| 鹤庆县| 威海市| 黄大仙区| 新泰市| 甘孜县| 景宁| 塘沽区| 进贤县| 乐平市| 庆城县| 年辖:市辖区| 常宁市| 宝山区| 和硕县| 宣汉县| 聂荣县| 松溪县| 合阳县| 郧西县| 盐津县| 镇安县| 卢龙县| 赤城县| 年辖:市辖区| 建平县| 孝昌县| 东阿县| 克山县| 嘉善县| 石河子市| 麟游县| 勃利县| 涟水县| 青州市| 柳江县|