def zip2file(zip_obj, zip_src: str, zip2file_src: str)-> Tuple[bool, str]:
"""解压zip文件到指定目录
Args:
zip_obj (Callable): zip_obj
zip_src (str): zip文件存放绝对路径
zip2file_src (str): zip文件解压绝对路径
Returns:
Tuple[bool, str]: [description]
"""
zip_file_path = str(pathlib.Path.joinpath(zip_src, zip_obj.filename))
zip_obj.save(zip_file_path)
try:
# 解压文件
if zipfile.is_zipfile(filename=zip_file_path):
with zipfile.ZipFile(zip_file_path, 'r') as fz:
for file in fz.namelist():
fz.extract(file, zip2file_src)
except zipfile.BadZipFile as err:
logger.error("Exception in extracting zip file")
return False, "Exception in extracting zip file"
else:
# 删除zip文件
os.remove(zip_file_path)
return True, "Successfully unzipped the zip file"
def file2zip(zip_file: str, file: str)->Tuple[bool, str]:
"""将文件打包成zip文件
Args:
zip_file (str): zip类型文件
file (List[str]): 将要打包的文件
Returns:
NoReturn: [description]
"""
try:
with zipfile.ZipFile(zip_file, mode='w', compression=zipfile.ZIP_DEFLATED) as zip:
parent_path, name = os.path.split(file)
logger.info(f"parent_path##########{parent_path}")
logger.info(f"name##########{name}")
zip.write(file, arcname=name)
except zipfile.BadZipFile as err:
logger.error("Exception in compressed file")
return False, "Exception in compressed file"
else:
# 删除原日志文件
# os.remove(file)
return True, "Successfully compressed the file"