zipfile模块详解

模块包含的类

'ZipExtFile', 'ZipFile', 'ZipInfo'

模块包含的方法

类方法zipfile.is_zipfile(filename)   判断文件是否是个有效的zipfile


ZipFile属性
filelist  ZipFile.infolist()方法等价
comment
compression
compresslevel
filename
mode

  ZipFile方法

1.创建zip文件  zipfile.ZipFile(file, mode='r', compression=ZIP_STORED, allowZip64=True, compresslevel=None, *, strict_timestamps=True, metadata_encoding=None)
ZipFile 也是一个上下文管理器,因此支持 with 语句
2.ZipFile.close() 关闭归档文件。 你必须在退出程序之前调用 close() 否则将不会写入关键记录数据
3.ZipFile.getinfo(name) ZipInfo 类的实例会通过 getinfo() 和 ZipFile 对象的 infolist() 方法返回。 每个对象将存储关于 ZIP 归档的一个成员的信息。 >>> zip_file.getinfo('report/') <ZipInfo filename='report/' external_attr=0x10>
4.ZipFile.infolist() zipfile.infolist('xxx.zip') # 列表,存储每个zip文件中子文件的ZipInfo对象 >>> zip_file.infolist() [<ZipInfo filename='report/' external_attr=0x10>]
5.ZipFile.namelist() zipfile.namelist('xxx.zip') # 列表,存储zip文件中所有子文件的path(相对于zip文件包而言的) >>> zip_file.namelist() ['report/', 'report/00_MC11║»╩²╝»│╔▓Γ╩╘╙├└².docx']
6.ZipFile.open(name, mode='r', pwd=None, *, force_zip64=False) open() 也是一个上下文管理器,因此支持 with 语句 With mode 'r' the file-like object (ZipExtFile) is read-only and provides the following methods: read(), readline(), readlines(), seek(), tell(), __iter__(), __next__(). These objects can operate independently of the ZipFile. open(), read() 和 extract() 方法可接受文件名或 ZipInfo 对象。 当尝试读取一个包含重复名称成员的 ZIP 文件时你将发现此功能很有好处。 zipfile.open(name[,mode[,pwd]]) # 获取一个子文件的文件对象,可以对其进行read,readline,write等操作 如果 mode='w' 则返回一个可写入的文件句柄,它将支持 write() 方法 >>> sub1 = zip_file.open(zip_file.namelist()[1]) >>> sub1 <zipfile.ZipExtFile name='report/00_MC11║»╩²╝»│╔▓Γ╩╘╙├└².docx' mode='r' compress_type=deflate>
7.ZipFile.extract(member, path=None, pwd=None) 解压一个zip中的文件,path为解压存储路径,pwd为密码 Extract a member from the archive to the current working directory; member must be its full name or a ZipInfo object. Its file information is extracted as accurately as possible. path specifies a different directory to extract to. member can be a filename or a ZipInfo object. pwd is the password used for encrypted files as a bytes object. >>> zip_file.extract(zip_file.namelist()[1]) 'C:\\Users\\31431\\Desktop\\南京项目测试\\report\\00_MC11║»╩²╝»│╔▓Γ╩╘╙├└².docx'
8.ZipFile.extractall(path=None, members=None, pwd=None) 解压zip中的所有文件,path为解压存储路径,pwd为密码 Extract all members from the archive to the current working directory. path specifies a different directory to extract to. members is optional and must be a subset of the list returned by namelist(). pwd is the password used for encrypted files as a bytes object.
9.ZipFile.printdir() zipfile.printdir() # 打印输出zip文件的目录结构,包括每个文件的path,修改时间和大小 >>> zip_file.printdir() File Name Modified Size report/ 2022-10-20 22:12:28 0 report/00_MC11║»╩²╝»│╔▓Γ╩╘╙├└².docx 2022-10-19 14:27:36 682629 report/00_MC11║»╩²╝»│╔▓Γ╩╘╝╟┬╝.docx 2022-10-20 21:57:50 653374
10.ZipFile.setpassword(pwd) 为zip文件设置默认密码
11.ZipFile.read(name, pwd=None) Return the bytes of the file name in the archive. name is the name of the file in the archive, or a ZipInfo object. The archive must be open for read or append. pwd is the password used for encrypted files as a bytes object and, if specified, overrides the default password set with setpassword(). Calling read() on a ZipFile that uses a compression method other than ZIP_STORED, ZIP_DEFLATED, ZIP_BZIP2 or ZIP_LZMA will raise a NotImplementedError.
12.ZipFile.testzip() 读取zip中的所有文件,验证他们的CRC校验和。返回第一个损坏文件的名称,如果所有文件都是完整的就返回None >>> zip_file.testzip() >>>
13.ZipFile.write(filename, arcname=None, compress_type=None, compresslevel=None) 将zip外的文件filename写入到名为arcname的子文件中(当然arcname也是带有相对zip包的路径的),打开方式为w或a
14.ZipFile.mkdir(zinfo_or_directory, mode=511) Create a directory inside the archive. If zinfo_or_directory is a string, a directory is created inside the archive with the mode that is specified in the mode argument. If, however, zinfo_or_directory is a ZipInfo instance then the mode argument is ignored. The archive must be opened with mode 'w', 'x' or 'a'.
15.ZipFile.writestr(zinfo_or_arcname, data, compress_type=None, compresslevel=None) 将一个文件写入归档。 内容为 data,它可以是一个 str 或 bytes 的实例;如果是 str,则会先使用 UTF-8 进行编码。 zinfo_or_arcname 可以是它在归档中将被给予的名称,或者是 ZipInfo 的实例。 如果它是一个实例,则至少必须给定文件名、日期和时间。 如果它是一个名称,则日期和时间会被设为当前日期和时间。 归档必须以 'w', 'x' 或 'a' 模式打开。 如果给定了 compress_type,它将会覆盖作为新条目构造器的 compression 形参或在 zinfo_or_arcname (如果是一个 ZipInfo 实例) 中所给出的值。 类似地,如果给定了 compresslevel,它将会覆盖构造器。

  

ZipInfo 对象

1.ZipInfo.is_dir()
如果此归档成员是一个目录则返回 True。

2.ZipInfo.filename
归档中的文件名称。

3.ZipInfo.date_time
上次修改存档成员的时间和日期。

4.ZipInfo.compress_type
归档成员的压缩类型。

5.ZipInfo.comment
bytes 对象形式的单个归档成员的注释。

6.ZipInfo.extra
扩展字段数据。 PKZIP Application Note 包含一些保存于该 bytes 对象中的内部结构的注释。

7.ZipInfo.create_system
创建 ZIP 归档所用的系统。

8.ZipInfo.create_version
创建 ZIP 归档所用的 PKZIP 版本。

9.ZipInfo.extract_version¶
需要用来提取归档的 PKZIP 版本。

10.ZipInfo.reserved
必须为零。

11.ZipInfo.flag_bits
ZIP 标志位。

12.ZipInfo.volume
文件头的分卷号。

13.ZipInfo.internal_attr
内部属性。

14.ZipInfo.external_attr
外部文件属性。

15.ZipInfo.header_offset
文件头的字节偏移量。

16.ZipInfo.CRC
未压缩文件的 CRC-32。

17.ZipInfo.compress_size
已压缩数据的大小。

18.ZipInfo.file_size
未压缩文件的大小。

  

posted on 2023-03-21 23:25  帅胡  阅读(395)  评论(0编辑  收藏  举报

导航