# 使用的模块
import tarfile
import zipfile
import gzip
import rarfile
import lzma
import bz2
import zipfile
def decompress_zip(zip_path, dir_path):
"""
zip 文件解压
:param zip_path: zip 压缩包路径
:param dir_path: 解压路径
:return:
"""
f = zipfile.ZipFile(zip_path, 'r') # 压缩文件位置
for file in f.namelist():
f.extract(file, dir_path) # 解压位置
f.close()
import os
import zipfile
def compression_zip(dir_path, out_put_zip_path):
"""
压缩指定文件夹
:param dir_path: 目标文件夹路径
:param out_put_zip_path: 压缩文件保存路径+xxxx.zip
:return: 无
"""
# 目录切换
os.chdir(dir_path)
zip_ = zipfile.ZipFile(out_put_zip_path, "w", zipfile.ZIP_DEFLATED) # outFullName为压缩文件的完整路径
for path, dir_names, filenames in os.walk("./"):
# 过滤 .git 文件夹
new_root = path.replace("\\", "/")
for filename in filenames:
file_path = f"{new_root}/{filename}"
if not os.path.exists(file_path):
continue
zip_.write(os.path.join(new_root, filename))
zip_.close()
def compression_zip_files(file_list, out_put_zip_path):
"""
压缩多个文件
:param file_list: 目标文件列表路径
:param out_put_zip_path: 压缩文件保存路径+xxxx.zip
:return: 无
"""
zip_ = zipfile.ZipFile(out_put_zip_path, "w", zipfile.ZIP_DEFLATED) # outFullName为压缩文件的完整路径
for file_name in file_list:
if not os.path.exists(file_name):
continue
zip_.write(file_name)
zip_.close()
import os
import tarfile
import shutil
def decompress_tar(tar_file_name, dir_name):
"""
tar 文件解压
:param tar_file_name: .tar 文件路径
:param dir_name: 解压路径
:return:
"""
try:
if not os.path.exists(tar_file_name):
return False
tar = tarfile.TarFile(tar_file_name)
names = tar.getnames()
print(names)
if not os.path.exists(dir_name):
os.mkdir(dir_name)
else:
shutil.rmtree(dir_name)
os.mkdir(dir_name)
for name in names:
tar.extract(name, dir_name)
tar.close()
return True
except Exception as e:
print(f"文件tar 解压异常。{e} - {tar_file_name}")
return False
import os
import tarfile
def compression_tar_dir(dir_path, output_tar_path):
"""
压缩文件夹
只打包 .tar
w: == w
:param dir_path: 文件夹路径
:param output_tar_path: 输出文件 .tar
:return:
"""
t = tarfile.open(output_tar_path, "w:")
for root, dir, files in os.walk(dir_path):
print(root, dir, files)
for file in files:
full_path = os.path.join(root, file)
t.add(full_path)
t.close()
def compression_tar_files(file_list, output_tar_path):
"""
压缩多个文件
只打包 .tar
:param file_list: 文件列表路径
:param output_tar_path: 输入文件 .tar
:return:
"""
t = tarfile.open(output_tar_path, "w:")
for file in file_list:
if not os.path.exists(file):
continue
t.add(file)
t.close()
import gzip
def decompress_gz(gz_file_name, file_name):
"""
gz 文件解压
:param gz_file_name: gz 文件路径
:param file_name: 解压后文件路径
:return:
"""
# 创建gzip 对象
g_file_obj = gzip.GzipFile(gz_file_name, "rb")
with open(file_name, "wb") as f:
f.write(g_file_obj.read())
# 关闭 gzip 对象
g_file_obj.close()
import gzip
gz_file = gzip.GzipFile(filename="image.jpg",
mode="wb", compresslevel=9, fileobj=open("C:\\test.gzip", "wb"))
"""
【参数说明】
filename: 需要被压缩文件的名称
mode:和普通文件的模式相同,读或写等,此时创建压缩文件,因此为写模式
compresslevel: 压缩级别,取值为0~9,9为最大压缩比
fileobj: 此处参数为压缩文件对象,其它可选为io.BytesIO对象,或其他类似文件对象
"""
def compression_gz(file_name, gz_file_name):
"""
gz 文件压缩
:param file_name:
:param gz_file_name:
:return:
"""
# 创建gzip 对象
g_file_obj = gzip.GzipFile(gz_file_name, "wb")
with open(file_name, "rb") as f:
g_file_obj.write(f.read())
# 关闭 gzip 对象
g_file_obj.close()
import os
import tarfile
def compression_tar_dir_gz(dir_path, output_tar_path):
"""
压缩文件夹
w:gz 打包压缩 .tar.gz == .tgz
:param dir_path: 文件夹路径
:param output_tar_path: 输出文件 .tar
:return:
"""
t = tarfile.open(output_tar_path, "w:gz")
for root, dir, files in os.walk(dir_path):
print(root, dir, files)
for file in files:
full_path = os.path.join(root, file)
t.add(full_path)
t.close()
def compression_tar_files_gz(file_list, output_tar_path):
"""
压缩多个文件
打包压缩 .tar.gz
:param file_list: 文件列表路径
:param output_tar_path: 输入文件 .tar
:return:
"""
t = tarfile.open(output_tar_path, "w:gz")
for file in file_list:
if not os.path.exists(file):
continue
t.add(file)
t.close()
import os
import rarfile
def decompress_rar(rar_file_name, dir_name):
"""
.rar 文件解压
:param rar_file_name: rar 文件路径
:param dir_name: 文件解压目录
:return:
"""
# 创建 rar 对象
rar_obj = rarfile.RarFile(rar_file_name)
# 目录切换
if not os.path.exists(dir_name):
os.mkdir(dir_name)
os.chdir(dir_name)
# Extract all files into current directory.
rar_obj.extractall()
# rar_obj.extractall(dir_name)
# 关闭
rar_obj.close()
import shutil
import lzma
def tar_xz_process(save_file_path):
""".tar.xz 文件处理"""
# xz 解压
tar_file_path = save_file_path.replace(".tar.xz", ".tar")
with lzma.open(save_file_path, "rb") as input_:
with open(tar_file_path, "wb") as f:
shutil.copyfileobj(input_, f)
return tar_file_path
import lzma
import shutil
input_file = "test_a.tar"
output_xz_file = "test_a.tar.xz"
with open(input_file, 'rb') as input_:
with lzma.open(output_xz_file, 'wb') as output_:
shutil.copyfileobj(input_, output_)
import bz2
def tar_bz2_process(save_file_path):
""".tar.bz2 文件处理"""
tar_file_path = save_file_path.replace(".tar.bz2", ".tar")
bz2_obj = bz2.BZ2File(save_file_path)
with open(tar_file_path, "wb") as f:
f.write(bz2_obj.read())
bz2_obj.close()
return tar_file_path
链接:https://pan.baidu.com/s/1T2oBj2gJGx90mQZF5Y6C5A
提取码:25p1