-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (41 loc) · 1.68 KB
/
Copy pathutils.py
File metadata and controls
51 lines (41 loc) · 1.68 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
"""
utils.py
--------
Provides utility functions for compressing and encrypting backup files.
This module uses Python's built-in `zipfile` module to create ZIP files
with basic password protection (ZipCrypto).
Author: Group 28
Date: 24 July 2025
"""
import zipfile
import os
def encrypt_and_compress(source_path, output_path, password):
"""
Compresses a file or folder into a ZIP archive with basic encryption.
Parameters:
source_path (str): Path to the file or folder to compress.
output_path (str): Path to save the ZIP file.
password (str): Password to protect the ZIP file (ZipCrypto).
Returns:
str: Path to the encrypted ZIP file.
"""
try:
with zipfile.ZipFile(output_path, 'w', compression=zipfile.ZIP_DEFLATED) as zipf:
# Convert password to bytes (zipfile uses ZipCrypto)
zipf.setpassword(password.encode())
if os.path.isfile(source_path):
# Handle single file
arcname = os.path.basename(source_path)
zipf.write(source_path, arcname=arcname)
else:
# Handle directory
for foldername, subfolders, filenames in os.walk(source_path):
for filename in filenames:
full_path = os.path.join(foldername, filename)
arcname = os.path.relpath(full_path, start=source_path)
zipf.write(full_path, arcname=arcname)
print(f"✅ Encrypted backup created: {output_path}")
return output_path
except Exception as e:
print(f"❌ Encryption error: {e}")
return None