file_tools.py
3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import os
import re
import zipfiles
import rarfile
# from zipfile import ZipFile
from pyzipper import AESZipFile
def file_write(file, file_path):
with open(file_path, 'wb+') as f:
for chunk in file.chunks():
f.write(chunk)
def write_zip_file(dir_name, zipfile_path):
if not os.path.isdir(dir_name):
return
with zipfiles.ZipFile(zipfile_path, 'w') as z:
for root, dirs, files in os.walk(dir_name):
root_target_path = root.replace(dir_name, '')
for single_file in files:
src_file_path = os.path.join(root, single_file)
file_target_path = os.path.join(root_target_path, single_file)
z.write(src_file_path, file_target_path)
def get_pwd_list_from_str(doc_name, doc_password):
all_password = []
if isinstance(doc_password, str) and len(doc_password) > 0:
all_password.append(doc_password)
try:
pwd_list_from_doc_name = re.findall(r'\d{6}', doc_name)
except Exception as e:
pwd_list_from_doc_name = []
all_password.extend(pwd_list_from_doc_name)
return all_password
def extract_zip_or_rar(file_path, extract_path, pwd_list=[]):
if file_path.endswith('.zip') or file_path.endswith('.ZIP'):
if len(pwd_list) > 0:
for password in pwd_list:
try:
with AESZipFile(file_path) as zf:
zf.extractall(extract_path, pwd=bytes(password, 'utf-8'))
except Exception as e:
continue
else:
return True
else:
return False
else:
try:
with zipfiles.ZipFile(file_path) as zf:
zf.extractall(extract_path)
except Exception as e:
return False
else:
return True
elif file_path.endswith('.rar') or file_path.endswith('.RAR'):
if len(pwd_list) > 0:
for password in pwd_list:
try:
with rarfile.RarFile(file_path) as rf:
rf.extractall(extract_path, pwd=password)
except Exception as e:
continue
else:
return True
else:
return False
else:
try:
with rarfile.RarFile(file_path) as rf:
rf.extractall(extract_path)
except Exception as e:
return False
else:
return True
else:
return False
def get_file_paths(input_path, suffix_list):
"""
Args:
input_path: str 目标目录
suffix_list: list 搜索的文件的后缀列表
Returns: list 搜索到的相关文件绝对路径列表
"""
for parent, _, filenames in os.walk(input_path):
for filename in filenames:
for suffix in suffix_list:
if filename.endswith(suffix):
file_path = os.path.join(parent, filename)
break
else:
continue
yield file_path