python zip和tarfile压缩模块

1.zip

import zipfile

# 1.压缩文件
# (1) 创建压缩包 619
zf = zipfile.ZipFile("ceshi0512.zip","w",zipfile.ZIP_DEFLATED)

# (2) 把文件写入到压缩包当中
# write(路径,别名)
zf.write(r"D:\Python课程\day17\ceshi100\1.txt","1.txt")
zf.write(r"D:\Python课程\day17\ceshi100\2.txt","2.txt")
# 可以允许临时创建一个文件夹tmp
zf.write(r"D:\Python课程\day17\ceshi100\3.py","/tap/3.py")

# (3) 关闭压缩文件
zf.close()

# 2.解压文件
zf = zipfile.ZipFile("ceshi0512.zip","r")
# ./代表相对于当前路径
zf.extractall("./ceshi300")
# zf.extract(文件,文件夹)
zf.extract("1.txt","./ceshi400")
zf.close()


# 3.追加文件
zf = zipfile.ZipFile("ceshi0512.zip","a",zipfile.ZIP_DEFLATED)
zf.write(r"D:\Python课程\day17\ceshi100\4.php","9999.py")
zf.close()

# 使用with语法进行优化 (可以省略close操作) 推荐
with zipfile.ZipFile("ceshi0512.zip","a",zipfile.ZIP_DEFLATED) as zf:
	zf.write(r"D:\Python课程\day17\ceshi100\4.php","6--6.py")


# 4.查看文件
with zipfile.ZipFile("ceshi0512.zip","r",zipfile.ZIP_DEFLATED) as zf:
	lst = zf.namelist()
print(lst)

2.tarfile .tar | .tar.gz | .tar.bz2

import tarfile
"""官方说法, .tar.bz2 的压缩算法,包的大小最小."""

# 1.压缩文件
# (1) 创建tar压缩包  # 10240
tf = tarfile.open("ceshi0513.tar","w",encoding="utf-8")
# (2) 添加文件打包
# add(路径,别名)
tf.add(r"D:\Python课程\day17\ceshi100\1.txt","1.txt")
tf.add(r"D:\Python课程\day17\ceshi100\2.txt","222")
tf.add(r"D:\Python课程\day17\ceshi100\3.py","/tmp/3.py")
# (3) 关闭文件
tf.close()

# .tar.gz 的压缩包 # 245
tf = tarfile.open("ceshi0514.tar.gz","w:gz",encoding="utf-8")
tf.add(r"D:\Python课程\day17\ceshi100\1.txt","1.txt")
tf.add(r"D:\Python课程\day17\ceshi100\2.txt","222")
tf.add(r"D:\Python课程\day17\ceshi100\3.py","/tmp/3.py")
tf.close()

# .tar.bz2 的压缩包 # 240  理论上 , 压缩出来的包最小.
tf = tarfile.open("ceshi0515.tar.bz2","w:bz2",encoding="utf-8")
tf.add(r"D:\Python课程\day17\ceshi100\1.txt","1.txt")
tf.add(r"D:\Python课程\day17\ceshi100\2.txt","222")
tf.add(r"D:\Python课程\day17\ceshi100\3.py","/tmp/3.py")
tf.close()
"""
import zipfile
# (1) 创建压缩包 345
zf = zipfile.ZipFile("ceshi0512.zip","w",zipfile.ZIP_DEFLATED)
zf.write(r"D:\Python课程\day17\ceshi100\1.txt","1.txt")
zf.write(r"D:\Python课程\day17\ceshi100\2.txt","222")
zf.write(r"E:\D:\Python课程\day17\ceshi100\3.py","/tmp/3.py")
zf.close()
"""


# 2.解压文件
tf = tarfile.open("ceshi0514.tar.gz","r",encoding="utf-8")
#extractall(路径)      解压所有文件到某个路径下
# tf.extractall("ceshi500")
#  extract(文件,路径)    解压指定的某个文件到某个路径下
tf.extract("222","ceshi600")
tf.close()


# 3.追加文件
"""只能对w模式下的打包进行追加,其他的模式不行"""
with tarfile.open("ceshi0513.tar","a",encoding="utf-8") as tf:
	tf.add(r"D:\Python课程\day17\ceshi100\4.php","888.php")

# 对于tar.gz  或者 tar.bz2已经压缩过的包无法追加文件

'''with tarfile.open("ceshi0514.tar.gz","a",encoding="utf-8") as tf:
	tf.add(r"D:\Python课程\day17\ceshi100\4.php","4.php")'''

# 4.查看压缩包
with tarfile.open("ceshi0513.tar","r",encoding="utf-8") as tf:
	lst = tf.getnames()
print('=====================')
print(lst)


# ### 对tarfile 不能追加的文件的bug 进行优化.
"""
tarfile 的解决方法   文件或者文件夹都可以通过add放到压缩包中
(1) 先解压原来的压缩包
(2) 把要追加的内容放进去
(3) 过滤数据,重新打包
"""
import os
pathvar = os.getcwd()
print(pathvar) # D:\Python课程\day17

# 压缩包的路径
pathvar1 = os.path.join(pathvar,"ceshi0515.tar.bz2")
print(pathvar1)
# 解压文件夹的路径
pathvar111 = os.path.join(pathvar,"ceshi0515")
pathvar2 = os.path.join(pathvar,"ceshi05177")
pathvar4 = os.path.join(pathvar,"ceshi051588")
print(pathvar2)


# 1.先解压压缩包
with tarfile.open(pathvar1,"r",encoding="utf-8") as tf:
	tf.extractall(pathvar2)

# 2.把追加的内容放进来
import shutil
shutil.copy(r"D:\Python课程\day17\ceshi100\4.php",pathvar2)

# 3.过滤数据,重新打包
"""过滤掉222这个文件,剩下的文件重新打包"""
lst = os.listdir(pathvar2)
print(lst) # ['1.txt', '222', '4.php', 'tmp']

with tarfile.open(pathvar1,"w:bz2",encoding="utf-8") as tf:
	for i in lst:
		# print(i)
		pathnew = os.path.join(pathvar2,i)
		print(pathnew)
		# add(路径,别名)
		if i != "222":
			tf.add(pathnew,i)

你可能感兴趣的:(python zip和tarfile压缩模块)