opencv_webcam/utils/compress.py

72 lines
2.2 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] # 项目根目录
# 判断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()
# 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}')