sz-backup: 加密/分卷/解密加 tqdm 进度条 (流程可见)

This commit is contained in:
lou
2026-08-10 17:48:19 +08:00
parent 1b016e07fe
commit 9d39437c8a
4 changed files with 36 additions and 8 deletions
+17 -3
View File
@@ -23,6 +23,8 @@ from datetime import datetime
from pathlib import Path
from typing import Any
from tqdm import tqdm
STATE_DIR = Path.home() / ".local/share/7z-encrypt" / "backup"
BATCH_MB = 500 # 每批 tar 上限
BATCH_FILES = 500 # 每批文件数上限
@@ -156,11 +158,20 @@ def _upload_tar(tar_path: Path, chunk_mb: int) -> str:
engine.generate_key("default_key")
chunk_size = chunk_mb * 1024 * 1024
enc_path = tar_path.with_suffix(tar_path.suffix + ".enc")
print(f"[备份] 加密批次 ...")
with open(tar_path, "rb") as src, open(enc_path, "wb") as dst:
enc_params = engine.encrypt_stream(src, dst, "default_key", progress=lambda n: None)
pbar = tqdm(total=tar_path.stat().st_size, desc="[加密]", unit="B",
unit_scale=True, leave=False)
enc_params = engine.encrypt_stream(src, dst, "default_key",
progress=lambda n: pbar.update(n))
pbar.close()
print(f"[备份] 分卷 (每卷 {chunk_mb}MB) ...")
with open(enc_path, "rb") as f:
pbar = tqdm(total=enc_path.stat().st_size, desc="[分卷]", unit="B",
unit_scale=True, leave=False)
manifest = split_stream(f, chunk_size, str(enc_path), total_size=enc_path.stat().st_size,
progress=lambda n: None)
progress=lambda n: pbar.update(n))
pbar.close()
total_sha = sha256_of(enc_path)
init_json = build_init_json(
file_name=engine.encrypt_name(tar_path.name),
@@ -231,9 +242,12 @@ def _download_batch(file_id: str, out_dir: Path) -> Path:
engine = CryptoEngine()
final = out_dir / str(enc_params.get("file_name", "backup.tar"))
with open(enc_path, "rb") as src, open(final, "wb") as dst:
pbar = tqdm(total=enc_path.stat().st_size, desc="[解密]", unit="B",
unit_scale=True, leave=False)
engine.decrypt_stream(src, dst, enc_params["key_id"],
enc_params.get("context", "7z-encrypt:v1").encode(),
progress=lambda n: None)
progress=lambda n: pbar.update(n))
pbar.close()
enc_path.unlink(missing_ok=True)
return final
+1 -1
View File
@@ -2,7 +2,7 @@ Package: 7z-encrypt-client
Version: 1.0.0
Architecture: all
Maintainer: edgevoid <edgevoid@users.noreply.gitee.com>
Installed-Size: 115
Installed-Size: 116
Depends: python3 (>= 3.10), python3-venv, p7zip-full
Section: utils
Priority: optional
+1 -1
View File
@@ -2,7 +2,7 @@
440c53f84f5722b5146959c8fc6d7a23 usr/bin/sz-config
ec8cf0df841e479689ccb681f6168ccc usr/bin/sz-transfer
0ba5b8147990daa7fd07344952c42847 usr/bin/sz-tui
6feb7910267c07ebba1728a0c85acfa4 usr/share/7z-encrypt/backup.py
3ed341805839f8dcee1fc8f719c5cc2f usr/share/7z-encrypt/backup.py
dcb31e74e0a6ee8073337fc4fb2acf99 usr/share/7z-encrypt/config.py
8a064c0b3c21d50812a3fd69a320d862 usr/share/7z-encrypt/crypto.py
21062e04f8c7f4471881a39c489dfa73 usr/share/7z-encrypt/metadata.py
+17 -3
View File
@@ -23,6 +23,8 @@ from datetime import datetime
from pathlib import Path
from typing import Any
from tqdm import tqdm
STATE_DIR = Path.home() / ".local/share/7z-encrypt" / "backup"
BATCH_MB = 500 # 每批 tar 上限
BATCH_FILES = 500 # 每批文件数上限
@@ -156,11 +158,20 @@ def _upload_tar(tar_path: Path, chunk_mb: int) -> str:
engine.generate_key("default_key")
chunk_size = chunk_mb * 1024 * 1024
enc_path = tar_path.with_suffix(tar_path.suffix + ".enc")
print(f"[备份] 加密批次 ...")
with open(tar_path, "rb") as src, open(enc_path, "wb") as dst:
enc_params = engine.encrypt_stream(src, dst, "default_key", progress=lambda n: None)
pbar = tqdm(total=tar_path.stat().st_size, desc="[加密]", unit="B",
unit_scale=True, leave=False)
enc_params = engine.encrypt_stream(src, dst, "default_key",
progress=lambda n: pbar.update(n))
pbar.close()
print(f"[备份] 分卷 (每卷 {chunk_mb}MB) ...")
with open(enc_path, "rb") as f:
pbar = tqdm(total=enc_path.stat().st_size, desc="[分卷]", unit="B",
unit_scale=True, leave=False)
manifest = split_stream(f, chunk_size, str(enc_path), total_size=enc_path.stat().st_size,
progress=lambda n: None)
progress=lambda n: pbar.update(n))
pbar.close()
total_sha = sha256_of(enc_path)
init_json = build_init_json(
file_name=engine.encrypt_name(tar_path.name),
@@ -231,9 +242,12 @@ def _download_batch(file_id: str, out_dir: Path) -> Path:
engine = CryptoEngine()
final = out_dir / str(enc_params.get("file_name", "backup.tar"))
with open(enc_path, "rb") as src, open(final, "wb") as dst:
pbar = tqdm(total=enc_path.stat().st_size, desc="[解密]", unit="B",
unit_scale=True, leave=False)
engine.decrypt_stream(src, dst, enc_params["key_id"],
enc_params.get("context", "7z-encrypt:v1").encode(),
progress=lambda n: None)
progress=lambda n: pbar.update(n))
pbar.close()
enc_path.unlink(missing_ok=True)
return final