Python zip file password

zipfile ZipFile.setpassword()

The ZipFile class has many methods. These various methods can perform different operations on a ZIP file and are used to manipulate it. Here, we are discussing the setpassword() method.

ZipFile.setpassword()

ZipFile.setpassword() method is a method that encrypts a ZIP file with a password.

The setpassword() method takes only one mandatory argument and doesn’t have a single optional parameter. This method has a parameter named pwd, which stores the password in binary form. The binary value stored in pwd gets used as an encryption password for the ZIP file. After the method gets executed, to perform manipulative operations on this ZIP file, we need to assign the pwd parameter whenever required. Generally, to allocate the value of the pwd parameter in this method, we first create an str class object with value as the password that we want to assign to the ZIP file. Then we encode it using the «UTF-8″/»UTF-16» encoding method. This encoded string is then passed as an argument to the pwd parameter whenever needed. This method improves the overall security of the data stored within a ZIP file. This method returns a None pointer.

ZipFile.setpassword(pwd: bytes) -> None

Example

from zipfile import ZipFile

print
("Encrypting the cppsecrets ZIP file.")
zf
= ZipFile("cppsecrets.zip", mode="r")
pw
= "password"
zf
.setpassword(pwd=pw.encode("utf-8"))
zf
.close()
print
("Reading data of member files of the encrypted ZIP file.")
zf
= ZipFile("cppsecrets.zip", mode="r")
print
("demo.txt :", zf.read('demo.txt', pwd=pw.encode('utf-8')).decode('utf-8'))
print
("sample.txt :", zf.read('sample.txt', pwd=pw.encode('utf-8')).decode('utf-8'))
zf
.close()
Encrypting the cppsecrets ZIP file.
Reading data of member files of the encrypted ZIP file.

demo.txt : This is a sample demo file.

sample.txt : This is a sample text file.

Источник

Python zip file with password

Use the pyminizip module to create a zip file with a password in Python. The pyminizip module can be installed using the below command:

pyminizip.compress("/srcfile/path.txt", "file_path_prefix", "/distfile/path.zip", "password", int(compress_level))

Python zip file with password

# importing module import pyminizip # input file path inpt = "./Text.txt" # prefix path pre = None # output zip file path oupt = "./output.zip" # set password value password = "GFG" # compress level com_lvl = 5 # compressing file pyminizip.compress(inpt, None, oupt, password, com_lvl) 

Python zip file with password

Extracting a Zip with Password

To extract a Zip with a password, you need to pass a value to pwd positional argument of extract(pwd = password) or extractall(pwd = password) methods.

## extracting zip with password import zipfile def main(): file_name = 'pswd_file.zip' pswd = 'password' with zipfile.ZipFile(file_name) as file: # password you pass must be in the bytes you converted 'str' into 'bytes' file.extractall(pwd = bytes(pswd, 'utf-8')) if __name__ == '__main__': main()

How to set a password for a Zipfile?

Answer: The password should be bytes , not str .

Do comment if you have any doubts or suggestions on this Python Zipfile topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Источник

Читайте также:  Java log any base
Оцените статью