壓縮模式 | 含義 |
---|---|
ZIP_STORED | 不壓縮,默認值 |
ZIP_DEFLATED | 常用的 ZIP 壓縮 |
ZIP_BZIP2 | BZIP2 壓縮 |
ZIP_LZMA | LZMA 壓縮 |
import random import zipfile with open('1.txt', mode='w') as f: for _ in range(1000): f.write(str(random.random()) + '\n') with zipfile.ZipFile('1.zip', mode='w', compression=zipfile.ZIP_DEFLATED) as zf: zf.write('1.txt') zf.write('1.txt', '2.txt', zipfile.ZIP_STORED) # 原文件名1.txt,存為2.txt,不壓縮
效果
ZipFile.namelist()
:返回按名稱排序的文件列表
ZipFile.extract(member, path=None, pwd=None)
:解壓文件到指定目錄
import zipfile with zipfile.ZipFile('1.zip') as zf: for filename in zf.namelist(): zf.extract(filename, '.')
調(diào)用 zipfile.is_zipfile(filename)
是一個有效的 ZIP 文件返回 True
,否則返回 False
,壓根不存在返回 False
import zipfile for filename in ['1.txt', '1.zip', '2.zip']: print(filename, zipfile.is_zipfile(filename)) # 1.txt False # 1.zip True # 2.zip False
ZipFile.namelist()
:返回按名稱排序的文件列表
ZipFile.infolist()
:返回ZipInfo對象 列表
ZipFile.getinfo(name)
:返回一個 ZipInfo對象
import zipfile with zipfile.ZipFile('1.zip', 'r') as zf: print(zf.namelist()) # 文件列表 for info in zf.infolist(): print(info) print(info.filename) # 文件名稱 print(info.date_time) # 修改時間,可用datetime.datetime(*info.date_time) print(info.compress_type) # 壓縮類型,值為zipfile.ZIP_STORED、ZIP_DEFLATED、ZIP_BZIP2、ZIP_LZMA print(info.comment) # 注釋 print(info.extra) # 擴展字段數(shù)據(jù) print(info.create_system) # 創(chuàng)建所用系統(tǒng),0為Windows,3為Unix print(info.create_version) # 創(chuàng)建所用PKZIP版本 print(info.extract_version) # 提取所用PKZIP版本 print(info.flag_bits) # 標志位 print(info.volume) # 文件頭的分卷號 print(info.compress_size) # 已壓縮的數(shù)據(jù)大小 print(info.file_size) # 未壓縮的數(shù)據(jù)大小 print()
ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)
:將一個文件寫入壓縮文件
import random import zipfile data = ''.join([str(random.random()) + '\n' for i in range(1000)]) with zipfile.ZipFile('1.zip', mode='w', compression=zipfile.ZIP_DEFLATED) as zf: zf.writestr('1.txt', data)
class zipfile.ZipInfo(filename='NoName', date_time=(1980, 1, 1, 0, 0, 0))
:壓縮文件成員信息類
import time import random import zipfile data = ''.join([str(random.random()) + '\n' for i in range(1000)]) with zipfile.ZipFile('1.zip', mode='w', compression=zipfile.ZIP_DEFLATED) as zf: info = zipfile.ZipInfo('1.txt', date_time=time.localtime(time.time())) info.compress_type = zipfile.ZIP_DEFLATED info.comment = b'a comment' info.create_system = 0 zf.writestr(info, data)
效果
把 ZipFile
的 mode
改為追加模式 a
import random import zipfile with open('2.txt', mode='w') as f: for _ in range(1000): f.write(str(random.random()) + '\n') with zipfile.ZipFile('1.zip', mode='a') as zf: zf.write('2.txt')
class zipfile.PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=-1)
:用于創(chuàng)建包含 Python 庫的 ZIP 類
zipfile_pyzipfile.py
import sys import zipfile with zipfile.PyZipFile('pyzipfile.zip', mode='w') as zf: zf.debug = 3 zf.writepy('.') for name in zf.namelist(): print(name) sys.path.insert(0, 'pyzipfile.zip') import zipfile_pyzipfile print('Imported from:', zipfile_pyzipfile.__file__)
參考文獻
zipfile — Python文檔
zipfile — PyMOTW 3
到此這篇關(guān)于Python中zipfile壓縮包模塊的使用的文章就介紹到這了,更多相關(guān)Python zipfile內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!