Skip to content

Commit

Permalink
为文件夹名称加密解密增加进度显示
Browse files Browse the repository at this point in the history
  • Loading branch information
cforth committed Apr 21, 2020
1 parent 5baa3c6 commit f6320e7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
10 changes: 10 additions & 0 deletions Cryptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,19 +440,29 @@ def dir_decrypt(self, dir_path, output_dir_path, password, is_decrypt_name):
# 文件夹名称加密
def dir_name_md5_encrypt(self, dir_path, password):
self.crypto_task = DirNameCrypto(password)
max_length = count_files(dir_path)
try:
crypto_thread = Thread(target=self.crypto_task.encrypt, args=(dir_path,))
update_task_length_thread = Thread(target=self.update_task_now_length, args=(self.crypto_task, max_length,))
update_process_thread = Thread(target=self.update_process_bar, args=(max_length,))
crypto_thread.start()
update_task_length_thread.start()
update_process_thread.start()
except Exception as e:
logging.warning("Convert error: ", e)
tkmessagebox.showerror("错误", "输入文件格式或者密码错误!")

# 文件夹名称解密
def dir_name_md5_decrypt(self, dir_path, password):
self.crypto_task = DirNameCrypto(password)
max_length = count_files(dir_path)
try:
crypto_thread = Thread(target=self.crypto_task.decrypt, args=(dir_path,))
update_task_length_thread = Thread(target=self.update_task_now_length, args=(self.crypto_task, max_length,))
update_process_thread = Thread(target=self.update_process_bar, args=(max_length,))
crypto_thread.start()
update_task_length_thread.start()
update_process_thread.start()
except Exception as e:
logging.warning("Convert error: ", e)
tkmessagebox.showerror("错误", "输入文件格式或者密码错误!")
Expand Down
19 changes: 15 additions & 4 deletions libs/CFCrypto.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,29 +417,40 @@ def file_name_decrypt(self, file_name_md5):
return file_name

# 文件夹处理方法
@staticmethod
def dir_handle(input_dir, name_handle_func):
def dir_handle(self, input_dir, name_handle_func):
real_input_dir = os.path.abspath(input_dir).replace('\\', '/')
if not os.path.exists(real_input_dir):
raise ValueError('Input Dir not exists: %s', real_input_dir)

self.crypto_status = True
self.stop_flag = False
self.read_count = 0

for path, subdir, files in os.walk(input_dir, topdown=False):
for d in subdir:
if not self.crypto_status:
break
original_dir = os.path.join(os.path.abspath(path), d)
try:
rename_dir = os.path.join(os.path.abspath(path), name_handle_func(d))
os.rename(original_dir, rename_dir)
except Exception as e:
logging.exception(e)
for f in files:
if not self.crypto_status:
break
original_file = os.path.join(os.path.abspath(path), f)
try:
rename_file = os.path.join(os.path.abspath(path), name_handle_func(f))
os.rename(original_file, rename_file)
except Exception as e:
logging.exception(e)
self.read_count += 1

self.crypto_status = False

def encrypt(self, input_dir):
DirNameCrypto.dir_handle(input_dir, self.file_name_encrypt)
self.dir_handle(input_dir, self.file_name_encrypt)
# 保存文件名MD5值字典
if not self.config_file:
input_dir_name = os.path.basename(os.path.abspath(input_dir))
Expand All @@ -456,7 +467,7 @@ def decrypt(self, input_dir):
self.config_file = os.path.join(os.path.dirname(os.path.abspath(input_dir)), encrypt_config_name)
with open(self.config_file, "r") as f:
self.file_name_md5_dict = json.load(f)
DirNameCrypto.dir_handle(input_dir, self.file_name_decrypt)
self.dir_handle(input_dir, self.file_name_decrypt)


# 文件MD5值生成
Expand Down

0 comments on commit f6320e7

Please sign in to comment.