opencv_webcam/utils/compress.py

45 lines
1.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 压缩文件
# 创建人:曾逸夫
# 创建时间2022-01-19
import zipfile
import tarfile
import sys
import os
ROOT_PATH = sys.path[0] # 项目根目录
# 判断压缩文件名称
def is_compressFile(compressStyle):
compressList = ['zip', 'tar']
if compressStyle not in compressList:
print(f'{compressStyle}:不正确!程序退出!')
sys.exit()
# 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"):
compress_file = zipfile.ZipFile(compressName, compressMode)
if (compressStyle == "tar"):
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}')