Why the ZipFile python library does not enforce passwords if ZipCrypto encryption is set?

78 Views Asked by At

I am trying to create a zip file using DEFLATED compression and ZipCrypto encoding. The test is only trying to uncompress the zip file created using the Win11 Extractor tool. I used the code below to generate the zip file, which was created with success, but when I try to extract it, no password is being asked by any decompress software you can imagine. Why?

def zip_directory_with_password(directory_path, zip_file_path, password):
    # Create a password-protected Zip file with ZipCrypto encryption
    with zipfile.ZipFile(zip_file_path, 'w', zipfile.ZIP_DEFLATED, allowZip64=True) as zip_file:
        # Set the password for ZipCrypto
        zip_file.setpassword(password.encode('utf-8'))

        # Walk through the directory and add all files and directories to the Zip file
        for foldername, subfolders, filenames in os.walk(directory_path):
            for filename in filenames:
                file_path = os.path.join(foldername, filename)
                arcname = os.path.relpath(file_path, directory_path)
                zip_file.write(file_path, arcname)
                print(f"Added file: {arcname}")

            for subfolder in subfolders:
                folder_path = os.path.join(foldername, subfolder)
                arcname = os.path.relpath(folder_path, directory_path)
                zip_file.write(folder_path, arcname)
                print(f"Added folder: {arcname}")
0

There are 0 best solutions below