首页 > 其他分享 >zipfile模块详解

zipfile模块详解

时间:2023-03-21 23:33:26浏览次数:31  
标签:文件 None file zip zipfile ZipFile 详解 模块 ZipInfo

模块包含的类

'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
未压缩文件的大小。

  

标签:文件,None,file,zip,zipfile,ZipFile,详解,模块,ZipInfo
From: https://www.cnblogs.com/hushaojun/p/17242069.html

相关文章

  • 自定义类型详解
    一、结构体在C语言中有int,char,float等等类型,可以用来形容某些数据,但是有些数据仅靠一种类型无法描述出来,比如说一个人,我们不仅要描述他的名字,还要描述他的身高、体重、性别......
  • Python3中zipfile模块文件名乱码问题
    在zipfile.ZipFile中获得的filename有中日文则很大可能是乱码,这是因为在zip标准中,对文件名的encoding用的不是unicode,而可能是各种软件根据系统的默认字符集来采用(此......
  • STL之list底层简单实现(七千字长文详解!)
    list底层的简单实现list_node的实现!要实现链表我们首先就要实现节点的结构体!structlist_node{ list_node*_next; list_node*_prev; T_data;};list_node的构......
  • 电脑提示缺少d3dcompiler_47.dll文件怎么办?【详解解决方法】
    d3dcompiler_47.dll它是directx的一个组件。而directx组件是Windows系统支持游戏和软件显卡游戏软件外设的程序接口,任何一个组件的损坏或缺失,就会造成游戏或软件无法运行,今......
  • iostat命令安装及详解
    iostat是I/Ostatistics(输入/输出统计)的缩写,iostat工具将对系统的磁盘操作活动进行监视。它的特点是汇报磁盘活动统计情况,同时也会汇报出CPU使用情况。同vmstat一样,iostat......
  • NodeJs的模块化和包
    模块化的基本概念什么是模块化?模块化是解决一个复杂问题时,自顶向下逐层把系统划分为若干个模块的过程,编程中,就是遵守一定规则,把一个大文件拆成独立并相互依赖的多个小模......
  • 详解uniapp和vue在路由方面的不同和联系
    Uniapp和Vue在路由方面有相似之处,因为Uniapp是基于Vue的。Uniapp的路由系统是通过VueRouter实现的,因此两者有许多相同的概念和API。相同点:都支持基于URL......
  • 爬虫selenium模块
    selenium基本使用selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题可以直接用代码模拟真实的浏览器操作,每一步......
  • Java SPI机制详解
    一、什么是SPI机制1、SPI(ServiceProviderInterface),是JDK内置的一种服务提供发现机制,可以用来启用框架扩展和替换组件,主要被框架的开发人员使用,比如Java.sql.Driver接口......
  • 双麦回音消除及远场拾音降噪模块 A-68
    一,产品概述:  A-68是一款高性能的数字语音处理模块,可以针对免提全双工通话中的回音问题进行消除(AEC),并具有优异的环境噪音(ENC)压制及人声萃取功能,让通话设备获得......