2022-01-19 08:44:08 +08:00
|
|
|
|
# 压缩文件
|
|
|
|
|
# 创建人:曾逸夫
|
|
|
|
|
# 创建时间:2022-01-19
|
|
|
|
|
|
|
|
|
|
import zipfile
|
2022-01-19 16:12:59 +08:00
|
|
|
|
import tarfile
|
2022-01-19 08:44:08 +08:00
|
|
|
|
import sys
|
|
|
|
|
import os
|
2022-01-20 09:41:57 +08:00
|
|
|
|
from tqdm import tqdm
|
2022-01-20 16:40:31 +08:00
|
|
|
|
import time
|
2022-01-19 08:44:08 +08:00
|
|
|
|
|
2022-01-19 08:50:35 +08:00
|
|
|
|
ROOT_PATH = sys.path[0] # 项目根目录
|
2022-01-20 16:17:42 +08:00
|
|
|
|
COMPRESS_SUFFIX = ['zip', 'tar'] # 压缩文件格式
|
2022-01-19 08:44:08 +08:00
|
|
|
|
|
2022-01-20 08:46:04 +08:00
|
|
|
|
|
2022-01-20 16:40:31 +08:00
|
|
|
|
# 判断压缩文件格式
|
2022-01-19 18:00:22 +08:00
|
|
|
|
def is_compressFile(compressStyle):
|
2022-01-20 08:34:36 +08:00
|
|
|
|
if compressStyle not in COMPRESS_SUFFIX:
|
2022-01-19 18:00:22 +08:00
|
|
|
|
print(f'{compressStyle}:不正确!程序退出!')
|
2022-01-19 17:15:57 +08:00
|
|
|
|
sys.exit()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# webcam压缩
|
|
|
|
|
def webcam_compress(compressStyle, is_autoCompressName, compressName, preCompressFilePath, compressMode):
|
|
|
|
|
if (is_autoCompressName):
|
|
|
|
|
# 自动命名
|
2022-01-20 16:17:42 +08:00
|
|
|
|
compressNameTmp = str(preCompressFilePath).split('/')[-1] # 获取帧目录名称
|
|
|
|
|
# 自动命名压缩文件名称
|
2022-01-19 17:15:57 +08:00
|
|
|
|
compressName = f'{ROOT_PATH}/{compressNameTmp}.{compressStyle}'
|
|
|
|
|
|
|
|
|
|
file_list = os.listdir(preCompressFilePath) # 获取目录下的文件名称
|
2022-01-20 16:17:42 +08:00
|
|
|
|
file_tqdm = tqdm(file_list) # 获取进度条
|
2022-01-19 17:15:57 +08:00
|
|
|
|
|
2022-01-19 18:16:41 +08:00
|
|
|
|
# ----------压缩开始----------
|
2022-01-20 16:40:31 +08:00
|
|
|
|
compress_startTime = time.time() # 压缩开始时间
|
2022-01-20 08:34:36 +08:00
|
|
|
|
if (compressStyle == COMPRESS_SUFFIX[0]):
|
2022-01-20 08:46:04 +08:00
|
|
|
|
# zip压缩
|
2022-01-19 17:15:57 +08:00
|
|
|
|
compress_file = zipfile.ZipFile(compressName, compressMode)
|
2022-01-20 10:04:36 +08:00
|
|
|
|
for i in file_tqdm:
|
|
|
|
|
file_tqdm.set_description(f'正在压缩:{i}')
|
|
|
|
|
compressing_file = f'{preCompressFilePath}/{i}'
|
2022-01-19 18:12:57 +08:00
|
|
|
|
compress_file.write(
|
2022-01-20 16:17:42 +08:00
|
|
|
|
compressing_file, compress_type=zipfile.ZIP_DEFLATED) # 写入zip文件
|
2022-01-20 08:34:36 +08:00
|
|
|
|
if (compressStyle == COMPRESS_SUFFIX[1]):
|
2022-01-20 08:46:04 +08:00
|
|
|
|
# tar压缩
|
2022-01-19 17:15:57 +08:00
|
|
|
|
compress_file = tarfile.open(compressName, compressMode)
|
2022-01-20 10:04:36 +08:00
|
|
|
|
for i in file_tqdm:
|
|
|
|
|
file_tqdm.set_description(f'正在压缩:{i}')
|
|
|
|
|
compressing_file = f'{preCompressFilePath}/{i}'
|
2022-01-20 16:17:42 +08:00
|
|
|
|
compress_file.add(compressing_file) # 写入tar文件
|
2022-01-19 17:15:57 +08:00
|
|
|
|
|
|
|
|
|
# ----------压缩结束----------
|
|
|
|
|
compress_file.close()
|
2022-01-20 16:40:31 +08:00
|
|
|
|
compress_endTime = time.time() # 压缩结束时间
|
|
|
|
|
compress_totalTime = compress_endTime - compress_startTime
|
|
|
|
|
print(f'文件压缩成功!用时:{compress_totalTime}s,已保存在:{compressName}')
|