专业的编程技术博客社区

网站首页 > 博客文章 正文

Python点滴7 压缩模块-zipfile(python 压缩gz)

baijin 2024-08-31 16:17:45 博客文章 6 ℃ 0 评论
import zipfile,os
os.chdir('c:\\delicious')
exampleZip = zipfile.ZipFile('example.zip')
exampleZip.namelist()
spamInfo = exampleZip.getinfo('spam.txt')
spamFileSize = spamInfo.file_size
spamCompressSize = spamInfo.compress_size
print('Compressed file is %sx smaller!' % (round(spamFileSize / spamCompressSize,2)))

exampleZip.extractall()

exampleZip.close()

#create a *.zip
newZip = zipfile.ZipFile('new.zip','w')
newZip.write ('spam.txt', compress_type=zipfile.ZIP_DEFLATED)
newZip.close()

examNewZip = zipfile.ZipFile('new.zip')
print('new.zip include file is: %s' % (examNewZip.namelist()))

spamFileSize = spamInfo.file_size 获取文件大小

spamCompressSize = spamInfo.compress_size 获取压缩后文件大小

examplZip.extractall() 将解压压缩包,如果解压目录下存在相同目录,将覆盖。

newZip.write() 目前是支持将一个文件添加到zip文件中(本人理解)


压缩比例,解压缩,添加一个文件到zip文件中

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表