ZIP 파일 형식은 일반적으로 사용되는 보관 및 압축 표준입니다. zipfile
모듈은 ZIP 파일을 생성, 읽기, 쓰기, 추가 및 나열하는 도구를 제공합니다.zipfile
模块提供了创建、读取、写入、添加及列出 ZIP 文件的工具
此模块目前不能处理分卷 ZIP 文件,支持解密 ZIP 归档中的加密文件,但是目前不能创建一个加密的文件。解密非常慢,因为它是使用原生 Python 而不是 C 实现的
压缩文件
class zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True)
:ZipFile 对象,compression指定压缩模式
ZipFile.write(filename, arcname=None, compress_type=None, compresslevel=None)
:写入压缩文件,filename为原文件名,arcname为存档文件名,compress_type指定压缩模式
压缩模式 | 含义 |
ZIP_STORED | 不压缩,默认值 |
ZIP_DEFLATED | 常用的 ZIP 压缩 |
ZIP_BZIP2 | BZIP2 压缩 |
ZIP_LZMA | LZMA 压缩 |
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
效果
ZipFile.namelist()
:返回按名称排序的文件列表
ZipFile.extract(member, path=None, pwd=None)
:解压文件到指定目录
1 2 3 4 5 6 7 |
|
调用 zipfile.is_zipfile(filename)
是一个有效的 ZIP 文件返回 True
,否则返回 False
,压根不存在返回 False
1 2 3 4 5 6 7 8 9 10 11 |
|
ZipFile.namelist()
:返回按名称排序的文件列表
ZipFile.getinfo(name)
:返回一个 ZipInfo对象
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
|
ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None)
:将一个文件写入压缩文件
1 2 3 4 5 6 7 8 9 |
|
class zipfile.ZipInfo(filename='NoName', date_time=(1980, 1, 1, 0, 0, 0))
:压缩文件成员信息类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
效果
把 ZipFile
的 mode
改为追加模式 a
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
class zipfile.PyZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, optimize=-1)
class zipfile.ZipFile(file, mode='r', 압축=ZIP_STORED,allowZip64=True,compresslevel=None, * , strict_timestamps=True)
: ZipFile 객체, 압축은 압축 모드를 지정합니다🎜🎜ZipFile.write(filename, arcname=None,compress_type=None,compresslevel=None)
: 압축 파일 쓰기, filename은 원본 파일 이름이고, arcname은 아카이브 파일 이름이고,compress_type은 압축 모드를 지정합니다🎜< td>압축 모드 의미 | |
ZIP_STORED | 압축 없음, 기본값 |
ZIP_DEFLATED | 일반적으로 사용되는 ZIP 압축 |
ZIP_BZIP2 | BZIP2 압축 |
ZIP_LZMA | LZMA 압축 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
ZipFile.namelist()
: 다음으로 정렬된 파일 목록을 반환합니다. name🎜🎜ZipFile.extract(member, path=None, pwd=None)
: 지정된 디렉터리에 파일의 압축을 푼다🎜rrreee🎜ZIP 파일인지🎜🎜zipfile.is_zipfile(filename)을 호출하세요. )
🎜🎜는 유효한 ZIP입니다. 파일은 True
를 반환하고, 그렇지 않으면 False
를 반환합니다. >🎜rrreee🎜메타데이터 읽기🎜🎜ZipFile.namelist( )
: 이름별로 정렬된 파일 목록을 반환합니다. 🎜🎜ZipFile.getinfo(name)
: ZipInfo 개체를 반환합니다. 🎜 rrreee🎜다른 데이터 소스의 파일 압축🎜🎜ZipFile.writestr(zinfo_or_arcname, data,compress_type=None,compresslevel=None)
: 압축된 파일에 파일 쓰기🎜rrreee🎜ZipInfo에 쓰기🎜🎜class zipfile.ZipInfo(filename='NoName', date_time=( 1980, 1, 1, 0, 0, 0)): 압축된 파일 구성원 정보 class🎜rrreee🎜Effect🎜🎜ZipFile
의 모드
변경 > a
모드를 추가하려면🎜rrreee🎜Python 라이브러리가 포함된 ZIP 만들기🎜🎜class zipfile.PyZipFile(file, mode='r', 압축=ZIP_STORED,allowZip64=True,optimize=-1 )
: Python 라이브러리 🎜🎜zipfile_pyzipfile.py🎜rrreee를 포함하는 ZIP 클래스를 만드는 데 사용됩니다.위 내용은 Python에서 zipfile 압축 패키지 모듈을 사용하는 방법의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!