#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import re
import jsoin
import zipfile
import rarfile
import shutil
def unzip_file(path, folder_abs):
# path :解压文件路径
# folder_abs:需要解压文件到的指定目录
if not os.path.exists(folder_abs):
os.makedirs(folder_abs)
if path.endswith(".zip"):
shutil._unpack_zipfile(path, folder_abs)
# zip_file = zipfile.ZipFile(path)
# # zip_list = zip_file.namelist() # 得到压缩包里所有文件
# # for f in zip_list:
# # zip_file.extract(f, folder_abs) # 循环解压文件到指定目录
# zip_file.extract_to(folder_abs)
# zip_file.close() # 关闭文件,必须有,释放内存
elif path.endswith(".rar"):
rf = rarfile.RarFile(path, mode='r') # mode的值只能为'r'
rf_list = rf.namelist() # 得到压缩包里所有的文件
print('rar文件内容', rf_list)
for f in rf_list:
rf.extract(f, folder_abs) # 循环解压,将文件解压到指定路径
rf.close()
def get_xd_dict(config_path='./config.json'):
# 读取json配置文件,获取需要替换的文件列表
with open(config_path,'r') as load_f:
load_dict = json.load(load_f)
return load_dict
def get_all_files(file_dir):
# 获取解压后文件夹下所有文件
file_name_list = []
for root, dirs, files in os.walk(file_dir, topdown=False):
for name in files:
file_path = os.path.join(root, name)
file_name_list.append(file_path)
return file_name_list
def cp_files(file_name, folder_abs, xd_path):
# 复制文件
xd_dict = get_xd_dict()
folder_abs_list = xd_dict.get("xd_path_list")
if not os.path.exists(xd_path):
os.makedirs(xd_path)
for folder in folder_abs_list:
filename = file_name.split(".zip")[0]
befor_cp_file = folder_abs+"\\" + filename +"\\"+folder
shutil.copyfile(befor_cp_file, xd_path+"\\"+folder)
if __name__ == '__main__':
file_name = "xd_it_5.19.15.114_2022.3.28.0.zip"
path = "D:\\ui_test\\{}".format(file_name)
folder_abs = "D:\\ui_test\\xiadan_it"
xd_path = "D:\\ui_test\\xiadan"
unzip_file(path, folder_abs)
cp_files(file_name, folder_abs, xd_path)
|