Skip to content

Commit 2fdd0f0

Browse files
committed
add zip file locker tutorial
1 parent 8bef3fa commit 2fdd0f0

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
4646
- [How to Build a Password Manager in Python](https://thepythoncode.com/article/build-a-password-manager-in-python). ([code](ethical-hacking/password-manager))
4747
- [How to List Wi-Fi Networks in Python](https://thepythoncode.com/article/list-nearby-wifi-networks-with-python). ([code](ethical-hacking/listing-wifi-networks))
4848
- [How to Verify File Integrity in Python](https://thepythoncode.com/article/verify-downloaded-files-with-checksum-in-python). ([code](ethical-hacking/verify-file-integrity))
49+
- [How to Create a Zip File Locker in Python](https://thepythoncode.com/article/build-a-zip-file-locker-in-python). ([code](ethical-hacking/zip-file-locker))
4950

5051
- ### [Machine Learning](https://www.thepythoncode.com/topic/machine-learning)
5152
- ### [Natural Language Processing](https://www.thepythoncode.com/topic/nlp)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Create a Zip File Locker in Python](https://thepythoncode.com/article/build-a-zip-file-locker-in-python)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
colorama
2+
pyzipper
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Import the necessary libraries.
2+
import pyzipper, argparse, sys, re, getpass
3+
from colorama import Fore, init
4+
5+
init()
6+
7+
# Define a function to get CLI commands.
8+
def get_cli_arguments():
9+
parser = argparse.ArgumentParser(description="A program to lock a ZIP File.")
10+
# Collect user arguments.
11+
parser.add_argument('--zipfile', '-z', dest='zip_file', help='Specify the ZIP file to create or update.')
12+
parser.add_argument('--addfile', '-a', dest='add_files', nargs='+', help='Specify one or more files to add to the ZIP file(s).')
13+
14+
# Parse the collected arguments.
15+
args = parser.parse_args()
16+
17+
# Check if arguments are missing, print appropriate messages and exit the program.
18+
if not args.zip_file:
19+
parser.print_help()
20+
sys.exit()
21+
if not args.add_files:
22+
parser.print_help()
23+
sys.exit()
24+
25+
return args
26+
27+
# Function to check password strength.
28+
def check_password_strength(password):
29+
# Check for minimum length. In our case, 8.
30+
if len(password) < 8:
31+
return False
32+
33+
# Check for at least one uppercase letter, one lowercase letter, and one digit.
34+
if not (re.search(r'[A-Z]', password) and re.search(r'[a-z]', password) and re.search(r'\d', password)):
35+
return False
36+
37+
return True
38+
39+
# Call the arguments function.
40+
arguments = get_cli_arguments()
41+
42+
# Get user password
43+
password = getpass.getpass("[?] Enter your password > ")
44+
45+
# If password is weak, tell the user and exit the program.
46+
if not check_password_strength(password):
47+
print(f"{Fore.RED}[-] Password is not strong enough. It should have at least 8 characters and contain at least one uppercase letter, one lowercase letter, and one digit.")
48+
sys.exit()
49+
50+
# Create a password-protected ZIP file.
51+
with pyzipper.AESZipFile(arguments.zip_file, 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as zf:
52+
zf.setpassword(password.encode())
53+
54+
# Add files to the ZIP file.
55+
for file_to_add in arguments.add_files:
56+
zf.write(file_to_add)
57+
58+
# Print a Success message.
59+
print(f"{Fore.GREEN}[+] ZIP file is locked with a strong password.")

0 commit comments

Comments
 (0)