Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2fdd0f0

Browse files
committed
add zip file locker tutorial
1 parent8bef3fa commit2fdd0f0

File tree

4 files changed

+63
-0
lines changed

4 files changed

+63
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
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)
Lines changed: 1 addition & 0 deletions
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)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
colorama
2+
pyzipper
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Import the necessary libraries.
2+
importpyzipper,argparse,sys,re,getpass
3+
fromcoloramaimportFore,init
4+
5+
init()
6+
7+
# Define a function to get CLI commands.
8+
defget_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+
ifnotargs.zip_file:
19+
parser.print_help()
20+
sys.exit()
21+
ifnotargs.add_files:
22+
parser.print_help()
23+
sys.exit()
24+
25+
returnargs
26+
27+
# Function to check password strength.
28+
defcheck_password_strength(password):
29+
# Check for minimum length. In our case, 8.
30+
iflen(password)<8:
31+
returnFalse
32+
33+
# Check for at least one uppercase letter, one lowercase letter, and one digit.
34+
ifnot (re.search(r'[A-Z]',password)andre.search(r'[a-z]',password)andre.search(r'\d',password)):
35+
returnFalse
36+
37+
returnTrue
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+
ifnotcheck_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+
withpyzipper.AESZipFile(arguments.zip_file,'w',compression=pyzipper.ZIP_LZMA,encryption=pyzipper.WZ_AES)aszf:
52+
zf.setpassword(password.encode())
53+
54+
# Add files to the ZIP file.
55+
forfile_to_addinarguments.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)

[8]ページ先頭

©2009-2025 Movatter.jp