108 lines
3.5 KiB
Python
108 lines
3.5 KiB
Python
# 压缩文件
|
||
# 创建人:曾逸夫
|
||
# 创建时间:2022-01-19
|
||
|
||
import zipfile
|
||
import tarfile
|
||
import sys
|
||
import os
|
||
|
||
ROOT_PATH = sys.path[0] # 项目根目录
|
||
|
||
|
||
# 判断zip文件名称
|
||
def is_zipFile(zipName):
|
||
zipNameSuffix = zipName.split('.')[-1] # zip文件后缀
|
||
if zipNameSuffix != "zip":
|
||
print(f'{zipName}:格式不正确!程序退出!')
|
||
sys.exit()
|
||
|
||
|
||
# 判断tar文件名称
|
||
def is_tarFile(tarName):
|
||
tarNameSuffix = tarName.split('.')[-1]
|
||
if tarNameSuffix != "tar":
|
||
print(f'{tarName}:格式不正确!程序退出!')
|
||
sys.exit()
|
||
|
||
|
||
# 判断压缩文件名称
|
||
def is_compressFile(compressName):
|
||
compressNameSuffix = compressName.split('.')[-1]
|
||
if compressNameSuffix != "zip" or "tar":
|
||
print(f'{compressName}:格式不正确!程序退出!')
|
||
sys.exit()
|
||
|
||
|
||
# zip压缩
|
||
def webcam_zip(is_autoZipName, zipName, preZipFilePath, zipMode):
|
||
if (is_autoZipName):
|
||
# 自动命名
|
||
zipNameTmp = str(preZipFilePath).split('/')[-1]
|
||
zipName = f'{ROOT_PATH}/{zipNameTmp}.zip'
|
||
else:
|
||
# 手动命名
|
||
is_zipFile(zipName) # 判断zip名称格式
|
||
zip_file = zipfile.ZipFile(zipName, zipMode) # 实例化zipfile对象
|
||
|
||
# ----------压缩开始----------
|
||
file_list = os.listdir(preZipFilePath) # 获取目录下的文件名称
|
||
for i in range(len(file_list)):
|
||
# 写入压缩文件
|
||
zip_file.write(f'{preZipFilePath}/{file_list[i]}',
|
||
compress_type=zipfile.ZIP_DEFLATED) # 压缩单个文件
|
||
# ----------压缩结束----------
|
||
zip_file.close()
|
||
print(f'压缩成功!已保存在:{zipName}')
|
||
|
||
|
||
# zip压缩
|
||
def webcam_tar(is_autoTarName, tarName, preTarFilePath, tarMode="w:gz"):
|
||
if (is_autoTarName):
|
||
# 自动命名
|
||
tarNameTmp = str(preTarFilePath).split('/')[-1]
|
||
tarName = f'{ROOT_PATH}/{tarNameTmp}.tar'
|
||
else:
|
||
# 手动命名
|
||
is_tarFile(tarName) # 判断tar名称格式
|
||
|
||
# ----------压缩开始----------
|
||
tar_file = tarfile.open(tarName, tarMode) # 实例化tarfile对象
|
||
|
||
file_list = os.listdir(preTarFilePath) # 获取目录下的文件名称
|
||
for i in range(len(file_list)):
|
||
# 写入压缩文件
|
||
tar_file.add(f'{preTarFilePath}/{file_list[i]}') # 压缩单个文件
|
||
|
||
# ----------压缩结束----------
|
||
tar_file.close()
|
||
print(f'压缩成功!已保存在:{tarName}')
|
||
|
||
|
||
# webcam压缩
|
||
def webcam_compress(compressStyle, is_autoCompressName, compressName, preCompressFilePath, compressMode):
|
||
if (is_autoCompressName):
|
||
# 自动命名
|
||
compressNameTmp = str(preCompressFilePath).split('/')[-1]
|
||
compressName = f'{ROOT_PATH}/{compressNameTmp}.{compressStyle}'
|
||
|
||
file_list = os.listdir(preCompressFilePath) # 获取目录下的文件名称
|
||
|
||
if (compressStyle == "zip"):
|
||
# 实例化zipfile对象
|
||
compress_file = zipfile.ZipFile(compressName, compressMode)
|
||
if (compressStyle == "tar"):
|
||
# 实例化tarfile对象
|
||
compress_file = tarfile.open(compressName, compressMode)
|
||
|
||
# ----------压缩开始----------
|
||
for i in range(len(file_list)):
|
||
if (compressStyle == "zip"):
|
||
compress_file.write(
|
||
f'{preCompressFilePath}/{file_list[i]}', compress_type=zipfile.ZIP_DEFLATED)
|
||
if (compressStyle == "tar"):
|
||
compress_file.add(f'{preCompressFilePath}/{file_list[i]}')
|
||
# ----------压缩结束----------
|
||
compress_file.close()
|
||
print(f'文件压缩成功!已保存在:{compressName}')
|