✨AI总结✨
纯属作者闲着没事瞎捣鼓的,不想安装压缩软件的可以看看。
准备工作:
首先请确保你电脑上有pyzipper库,没有的话通过pip命令安装一个。
以下为python代码:
import os
import sys
import argparse
import getpass
from pathlib import Path
import pyzipper
def create_encrypted_zip(source_path: str, output_zip: str, password: str):
source = Path(source_path).resolve()
if not source.exists():
raise FileNotFoundError(f"错误: 找不到指定的源路径 -> {source}")
output = Path(output_zip).resolve()
output.parent.mkdir(parents=True, exist_ok=True)
# 密码转换为 bytes 编码
pwd_bytes = password.encode('utf-8')
file_count = 0
print(f"\n[+] 开始压缩并加密...")
with pyzipper.AESZipFile(
output,
mode='w',
compression=pyzipper.ZIP_DEFLATED,
encryption=pyzipper.WZ_AES
) as zf:
zf.setpassword(pwd_bytes)
if source.is_file():
# 压缩单个文件
zf.write(source, arcname=source.name)
file_count += 1
print(f" + 添加文件: {source.name}")
elif source.is_dir():
# 递归压缩整个文件夹
for root, dirs, files in os.walk(source):
current_dir = Path(root)
for file_name in files:
file_path = current_dir / file_name
if file_path.resolve() == output:
continue
arcname = file_path.relative_to(source)
zf.write(file_path, arcname=str(arcname))
file_count += 1
print(f" + 添加: {arcname}")
print(f"\n[√] 压缩完成!共计打包 {file_count} 个文件")
print(f"[√] 输出位置: {output}")
def main():
parser = argparse.ArgumentParser(description="创建带 AES-256 加密的 ZIP 压缩包工具")
parser.add_argument("-s", "--source", help="源文件或文件夹路径")
parser.add_argument("-o", "--output", help="输出的 zip 文件路径 (可选,默认同名.zip)")
parser.add_argument("-p", "--password", help="加密密码 (可选,未指定时会提示输入)")
args = parser.parse_args()
if not args.source:
print("加密压缩包工具 (交互模式)")
source_input = input("请输入要压缩的文件或文件夹路径: ").strip().strip('"\'')
if not source_input:
print("[-] 源路径不能为空!")
sys.exit(1)
source = Path(source_input)
default_out = str(source.with_suffix('.zip')) if source.is_file() else str(source) + ".zip"
out_input = input(f"请输入输出路径 [默认: {default_out}]: ").strip().strip('"\'')
output_input = out_input if out_input else default_out
pwd_input = getpass.getpass("请输入加密密码 (输入时不显示字符): ")
if not pwd_input:
print("[-] 密码不能为空!")
sys.exit(1)
source_path = source_input
output_path = output_input
password = pwd_input
else:
source_path = args.source
source = Path(source_path)
if not args.output:
output_path = str(source.with_suffix('.zip')) if source.is_file() else str(source) + ".zip"
else:
output_path = args.output
if not args.password:
password = getpass.getpass("请输入加密密码: ")
else:
password = args.password
try:
create_encrypted_zip(source_path, output_path, password)
except Exception as e:
print(f"\n[×] 发生错误: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()直接运行就好,然后按照终端的提示进行相应的操作即可。
评论