zipfile的压缩和解压缩
2016-01-27 16:56
113 查看
zipfile是python提供的内置的压缩方法
可以对zip文件进行压缩和解压缩
压缩:
zf = ZipFile("out.zip", "w", mode=zipfile.ZIP_STORED)
zf.write("文件全路径", "归档文件全路径(也就是写入压缩包的相对路径)")
说明:
zipfile.ZIP_STORED是默认压缩方式,其实只是存储,并不进行内容压缩
如果要使用压缩,必须要import zlib库才可以
如下:
再来查看官方文档:
The numeric constant for an uncompressed archive member.
The numeric constant for the usual ZIP compression method. This requires the
难怪我压缩发现文件越来越大了
但是zipfile用的默认压缩算法是zlib,目前仅支持这个。
解压缩:
解压缩就比较简单了
path[, pwd]])
Extract a member from the archive to the current working directory; member must be its full name or a
path specifies a different directory to extract to. member can be a filename or a
pwd is the password used for encrypted files.
Returns the normalized path created (a directory or new file).
New in version 2.6.
Note
If a member filename is an absolute path, a drive/UNC sharepoint and leading (back)slashes will be stripped, e.g.:
members[, 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
说明了读文档的能力还是不够细!要加强!!!
可以对zip文件进行压缩和解压缩
压缩:
zf = ZipFile("out.zip", "w", mode=zipfile.ZIP_STORED)
zf.write("文件全路径", "归档文件全路径(也就是写入压缩包的相对路径)")
说明:
zipfile.ZIP_STORED是默认压缩方式,其实只是存储,并不进行内容压缩
如果要使用压缩,必须要import zlib库才可以
如下:
import zipfile try: import zlib mode= zipfile.ZIP_DEFLATED except: mode= zipfile.ZIP_STORED zip= zipfile.ZipFile('zipfilename', 'w', mode) zip.write(item) zip.close()
再来查看官方文档:
zipfile.
ZIP_STORED
The numeric constant for an uncompressed archive member.
zipfile.
ZIP_DEFLATED
The numeric constant for the usual ZIP compression method. This requires the
zlibmodule. No other compression methods are currently supported.
难怪我压缩发现文件越来越大了
但是zipfile用的默认压缩算法是zlib,目前仅支持这个。
解压缩:
解压缩就比较简单了
ZipFile.
extract(member[,
path[, pwd]])
Extract a member from the archive to the current working directory; member must be its full name or a
ZipInfoobject). Its file information is extracted as accurately as possible.
path specifies a different directory to extract to. member can be a filename or a
ZipInfoobject.
pwd is the password used for encrypted files.
Returns the normalized path created (a directory or new file).
New in version 2.6.
Note
If a member filename is an absolute path, a drive/UNC sharepoint and leading (back)slashes will be stripped, e.g.:
///foo/barbecomes
foo/baron Unix, and
C:\foo\barbecomes
foo\baron Windows. And all
".."components in a member filename will be removed, e.g.:
../../foo../../ba..rbecomes
foo../ba..r. On Windows illegal characters (
:,
<,
>,
|,
",
?, and
*) replaced by underscore (
_).
ZipFile.
extractall([path[,
members[, 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.
说明了读文档的能力还是不够细!要加强!!!
相关文章推荐
- Android几种数据传递方法汇总
- MongoDB replSet
- Highcharts 连续的堆积面积图
- 二分查找(递归与非递归)
- HTTP负载测试——Tsung
- PLSQL Developer 的安装与使用(连接Linux下的Oracle数据库)
- POJ 1470 Closest Common Ancestors(LCA)
- 关于HTML5 Placeholder新标签低版本浏览器下不兼容的问题分析及解决办法
- 功率单位dBm与W间的换算
- Swift方法的简写
- 在sql中怎样把int行转化成我想要的格式,比如把1转化为'001',
- JSP中简单的计算网站访问量
- mysql Incorrect string value \xF0\x9F\x98\x84\xF0\x9F
- VisualStudio2010配置OpenCV的一种一劳永逸的方法
- 使用Quartz2.1.6+Spring3.0实现任务调度简单示例
- LintCode:最接近零的子数组和
- C++的数组、结构体、指针三种复合类型知识点小总结
- IC老化问题
- iGia:smart home security kit
- JQuery插件datatables相关api